<commit_before><?php
/**
 * Symfony REST Edition.
 *
 * @link      https://github.com/Ingewikkeld/symfony-rest-edition
 * @copyright Copyright (c) 2013-2013 Ingewikkeld<<EMAIL>>
 * @license   https://github.com/Ingewikkeld/symfony-rest-edition/blob/master/LICENSE MIT License
 */

namespace Ingewikkeld\Rest\UserBundle\Tests\ResourceMapper;

use Ingewikkeld\Rest\UserBundle\ResourceMapper\User;
use Mockery as m;

/**
 * Tests the resource mapper for the User resource.
 */
class UserTest extends \PHPUnit_Framework_TestCase
{
    /** @var \Mockery\MockInterface|\FOS\UserBundle\Model\UserManagerInterface */
    protected $userManagerMock;

    /** @var \Mockery\MockInterface|\Symfony\Component\Translation\TranslatorInterface */
    protected $translatorMock;

    /** @var \Mockery\MockInterface|\Symfony\Component\Routing\RouterInterface */
    protected $routerMock;

    /** @var User */
    protected $fixture;

    public function setUp()
    {
        $this->userManagerMock = m::mock('FOS\UserBundle\Model\UserManagerInterface');
        $this->translatorMock  = m::mock('Symfony\Component\Translation\TranslatorInterface');
        $this->routerMock      = m::mock('Symfony\Component\Routing\RouterInterface');

        $this->fixture = new User($this->userManagerMock, $this->translatorMock, $this->routerMock);
    }

    /**
     * @covers Ingewikkeld\Rest\UserBundle\ResourceMapper\User::__construct
     */
    public function testCorrectlyRegisterDependencies()
    {
        $userManagerMock = m::mock('FOS\UserBundle\Model\UserManagerInterface');
        $translatorMock  = m::mock('Symfony\Component\Translation\TranslatorInterface');
        $routerMock      = m::mock('Symfony\Component\Routing\RouterInterface');

        $mapper = new User($userManagerMock, $translatorMock, $routerMock);

        $this->assertAttributeSame($userManagerMock, 'userManager', $mapper);
        $this->assertAttributeSame($translatorMock, 'translator', $mapper);
        $this->assertAttributeSame($routerMock, 'router', $mapper);
    }

    /**
     * @covers Ingewikkeld\Rest\UserBundle\ResourceMapper\User::getResource
     * @covers Ingewikkeld\Rest\UserBundle\ResourceMapper\User::createResourceFromUser
     */
    public function testRetrievingAUserResourceByItsUsername()
    {
        $username  = 'mvriel';
        $email     = '<EMAIL>';
        $lastLogin = new \DateTime('2014-01-01');
        $readUrl   = 'mvriel.json';

        $user = m::mock('Ingewikkeld\Rest\UserBundle\Entity\User');
        $user->shouldReceive('getUsernameCanonical')->andReturn($username);
        $user->shouldReceive('getEmail')->andReturn($email);
        $user->shouldReceive('getLastLogin')->andReturn($lastLogin);

        $this->userManagerMock->shouldReceive('findUserByUsername')->with($username)->andReturn($user);
        $this->routerMock->shouldReceive('generate')->andReturn($readUrl);

        /** @var \Hal\Resource $resource */
        $resource = $this->fixture->getResource($username);

        $this->assertInstanceOf('Hal\Resource', $resource);
        $this->assertSame(
            $resource->toArray(),
            array(
                '_links' => array(
                    'self' => array ('href' => 'mvriel.json')
                ),
                'username'   => $username,
                'email'      => $email,
                'last_login' => $lastLogin->format('c'),
            )
        );
    }

    /**
     * @covers Ingewikkeld\Rest\UserBundle\ResourceMapper\User::getResource
     * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
     */
    public function testRetrievingANonExistingUserReturns404Exception()
    {
        $username = 'unknownUser';
        $this->userManagerMock->shouldReceive('findUserByUsername')->with($username)->andReturnNull();
        $this->translatorMock->shouldReceive('trans')->with('error.user_not_found', array('%id%' => $username));

        $this->fixture->getResource($username);
    }

    /**
     * @covers Ingewikkeld\Rest\UserBundle\ResourceMapper\User::getCollection
     * @covers Ingewikkeld\Rest\UserBundle\ResourceMapper\User::createResourceFromUser
     */
    public function testRetrievingACollectionOfUserResources()
    {
        $username  = 'mvriel';
        $email     = '<EMAIL>';
        $lastLogin = new \DateTime('2014-01-01');
        $readUrl   = 'mvriel.json';

        $user = m::mock('Ingewikkeld\Rest\UserBundle\Entity\User');
        $user->shouldReceive('getUsernameCanonical')->andReturn($username);
        $user->shouldReceive('getEmail')->andReturn($email);
        $user->shouldReceive('getLastLogin')->andReturn($lastLogin);

        $this->userManagerMock->shouldReceive('findUsers')->andReturn(array($user));
        $this->routerMock->shouldReceive('generate')->andReturn($readUrl);

        /** @var \Hal\Resource $resource */
        $resource = $this->fixture->getCollection();

        $this->assertInstanceOf('Hal\Resource', $resource);
        $this->assertSame(
            $resource->toArray(),
            array(
                '_links'    => array(
                    'self' => array ('href' => 'mvriel.json')
                ),
                'count'     => 1,
                '_embedded' => array(
                    'user' => array(
                        array(
                            '_links' => array(
                                'self' => array ('href' => 'mvriel.json')
                            ),
                            'username'   => $username,
                            'email'      => $email,
                            'last_login' => $lastLogin->format('c'),
                        )
                    )
                )
            )
        );
    }
}<commit_msg>Add tests for the User ResourceMapper and refactor test class<commit_after><?php
/**
 * Symfony REST Edition.
 *
 * @link      https://github.com/Ingewikkeld/symfony-rest-edition
 * @copyright Copyright (c) 2013-2013 Ingewikkeld<<EMAIL>>
 * @license   https://github.com/Ingewikkeld/symfony-rest-edition/blob/master/LICENSE MIT License
 */

namespace Ingewikkeld\Rest\UserBundle\Tests\ResourceMapper;

use Ingewikkeld\Rest\UserBundle\ResourceMapper\User;
use Mockery as m;

/**
 * Tests the resource mapper for the User resource.
 */
class UserTest extends \PHPUnit_Framework_TestCase
{
    const TEST_USERNAME = 'mvriel';
    const TEST_EMAIL    = '<EMAIL>';
    const TEST_SELF_URL = 'mvriel.json';

    /** @var \Mockery\MockInterface|\FOS\UserBundle\Model\UserManagerInterface */
    protected $userManagerMock;

    /** @var \Mockery\MockInterface|\Symfony\Component\Translation\TranslatorInterface */
    protected $translatorMock;

    /** @var \Mockery\MockInterface|\Symfony\Component\Routing\RouterInterface */
    protected $routerMock;

    /** @var User */
    protected $fixture;

    public function setUp()
    {
        $this->userManagerMock = m::mock('FOS\UserBundle\Model\UserManagerInterface');
        $this->translatorMock  = m::mock('Symfony\Component\Translation\TranslatorInterface');
        $this->routerMock      = m::mock('Symfony\Component\Routing\RouterInterface');

        $this->fixture = new User($this->userManagerMock, $this->translatorMock, $this->routerMock);
    }

    /**
     * @covers Ingewikkeld\Rest\UserBundle\ResourceMapper\User::__construct
     */
    public function testCorrectlyRegisterDependencies()
    {
        $userManagerMock = m::mock('FOS\UserBundle\Model\UserManagerInterface');
        $translatorMock  = m::mock('Symfony\Component\Translation\TranslatorInterface');
        $routerMock      = m::mock('Symfony\Component\Routing\RouterInterface');

        $mapper = new User($userManagerMock, $translatorMock, $routerMock);

        $this->assertAttributeSame($userManagerMock, 'userManager', $mapper);
        $this->assertAttributeSame($translatorMock, 'translator', $mapper);
        $this->assertAttributeSame($routerMock, 'router', $mapper);
    }

    /**
     * @covers Ingewikkeld\Rest\UserBundle\ResourceMapper\User::getResource
     * @covers Ingewikkeld\Rest\UserBundle\ResourceMapper\User::createResourceFromUser
     */
    public function testRetrievingAUserResourceByItsUsername()
    {
        $lastLogin = new \DateTime('2014-01-01');

        $user = $this->createUserMock(self::TEST_USERNAME, self::TEST_EMAIL, $lastLogin);
        $this->userManagerMock->shouldReceive('findUserByUsername')->with(self::TEST_USERNAME)->andReturn($user);
        $this->routerMock->shouldReceive('generate')->andReturn(self::TEST_SELF_URL);

        /** @var \Hal\Resource $resource */
        $resource = $this->fixture->getResource(self::TEST_USERNAME);

        $this->assertInstanceOf('Hal\Resource', $resource);
        $this->assertSame($resource->toArray(), $this->createHalArrayForUser($lastLogin));
    }

    /**
     * @covers Ingewikkeld\Rest\UserBundle\ResourceMapper\User::getResource
     * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
     */
    public function testRetrievingANonExistingUserReturns404Exception()
    {
        $this->userManagerMock->shouldReceive('findUserByUsername')->with(self::TEST_USERNAME)->andReturnNull();
        $this->translatorMock
            ->shouldReceive('trans')->with('error.user_not_found', array('%id%' => self::TEST_USERNAME));

        $this->fixture->getResource(self::TEST_USERNAME);
    }

    /**
     * @covers Ingewikkeld\Rest\UserBundle\ResourceMapper\User::getCollection
     * @covers Ingewikkeld\Rest\UserBundle\ResourceMapper\User::createResourceFromUser
     */
    public function testRetrievingACollectionOfUserResources()
    {
        $lastLogin = new \DateTime('2014-01-01');
        $user      = $this->createUserMock(self::TEST_USERNAME, self::TEST_EMAIL, $lastLogin);
        $this->userManagerMock->shouldReceive('findUsers')->andReturn(array($user));
        $this->routerMock->shouldReceive('generate')->andReturn(self::TEST_SELF_URL);

        /** @var \Hal\Resource $resource */
        $resource = $this->fixture->getCollection();

        $this->assertInstanceOf('Hal\Resource', $resource);
        $this->assertSame(
            $resource->toArray(),
            array(
                '_links'    => array('self' => array ('href' => self::TEST_SELF_URL)),
                'count'     => 1,
                '_embedded' => array('user' => array($this->createHalArrayForUser($lastLogin)))
            )
        );
    }

    /**
     * @covers Ingewikkeld\Rest\UserBundle\ResourceMapper\User::create
     */
    public function testCreateNewUserAndReturnResource()
    {
        $user = $this->createUserMock(self::TEST_USERNAME, self::TEST_EMAIL, null)->shouldIgnoreMissing();

        $this->userManagerMock
            ->shouldReceive('createUser')->andReturn($user)
            ->shouldReceive('updateUser')->with($user);

        $formMock = m::mock('Symfony\Component\Form\FormInterface');
        $formMock->shouldReceive('getData')->andReturn(
            array(
                 'username' => self::TEST_USERNAME,
                 'email'    => self::TEST_EMAIL,
                 'password' => '<PASSWORD>'
            )
        );
        $this->routerMock->shouldReceive('generate')->andReturn(self::TEST_SELF_URL);

        $resource = $this->fixture->create($formMock);

        $this->assertInstanceOf('Hal\Resource', $resource);
        $this->assertSame($resource->toArray(), $this->createHalArrayForUser(null));
    }

    /**
     * @covers Ingewikkeld\Rest\UserBundle\ResourceMapper\User::update
     */
    public function testUpdateExistingUser()
    {
        $passwordString = '<PASSWORD>';

        $userArray = array_merge($this->createHalArrayForUser(null), array('password' => $passwordString));
        $userMock  = m::mock('Hal\Resource')->shouldReceive('toArray')->andReturn($userArray)->getMock();

        $userEntity = m::mock('Ingewikkeld\Rest\UserBundle\Entity\User')
            ->shouldReceive('setUsername')->with(self::TEST_USERNAME)->getMock()
            ->shouldReceive('setEmail')->with(self::TEST_EMAIL)->getMock()
            ->shouldReceive('setPlainPassword')->with($passwordString)->getMock()
            ->shouldReceive('setEnabled')->with(true)->getMock();

        $this->userManagerMock
            ->shouldReceive('findUserByUsername')->with(self::TEST_USERNAME)->andReturn($userEntity)
            ->shouldReceive('updateUser')->with($userEntity);

        $this->fixture->update($userMock);

        $this->assertTrue(true);
    }

    /**
     * Creates a mock user entity class with pre-filled data.
     *
     * @param string $username
     * @param string $email
     * @param \DateTime $lastLogin
     *
     * @return m\MockInterface
     */
    protected function createUserMock($username, $email, $lastLogin)
    {
        return m::mock('Ingewikkeld\Rest\UserBundle\Entity\User')
                 ->shouldReceive('getUsernameCanonical')->andReturn($username)->getMock()
                 ->shouldReceive('getEmail')->andReturn($email)->getMock()
                 ->shouldReceive('getLastLogin')->andReturn($lastLogin)->getMock();
    }

    /**
     * Creates an array representation to test HAL Resources with.
     *
     * @param \DateTime|null $lastLogin
     *
     * @return array
     */
    protected function createHalArrayForUser($lastLogin)
    {
        return array(
            '_links'     => array(
                'self' => array('href' => self::TEST_SELF_URL)
            ),
            'username'   => self::TEST_USERNAME,
            'email'      => self::TEST_EMAIL,
            'last_login' => $lastLogin ? $lastLogin->format('c') : null,
        );
    }
}
{'fraction_non_alphanumeric': 0.14998174516246807, 'fraction_numerical': 0.004089083607155896, 'mean_word_length': 2.8602029312288613, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 203, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6272076', 'n_tokens_mistral': 3941, 'n_tokens_neox': 3825, 'n_words': 504}