<commit_before><?php
/**
 * YAWIK
 *
 * @filesource
 * @copyright (c) 2013-2015 Cross Solution (http://cross-solution.de)
 * @license       MIT
 */

namespace AuthTest\Service;

use Auth\Entity\User;
use Auth\Service\ForgotPassword;
use Auth\Service\Register;
use AuthTest\Entity\Provider\UserEntityProvider;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

class RegisterTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var ForgotPassword
     */
    private $testedObject;

    /**
     * @var MockObject
     */
    private $userRepositoryMock;

    /**
     * @var MockObject
     */
    private $mailServiceMock;

    /**
     * @var MockObject
     */
    private $mailMock;

    /**
     * @var MockObject
     */
    private $inputFilterMock;

    /**
     * @var MockObject
     */
    private $mailerPluginMock;

    /**
     * @var MockObject
     */
    private $urlPluginMock;

    /**
     * @var MockObject
     */
    public function setUp()
    {
        $this->userRepositoryMock = $this->getMockBuilder('Auth\Repository\User')
            ->disableOriginalConstructor()
            ->getMock();

        $this->mailServiceMock = $this->getMockBuilder('Core\Mail\MailService')
                                         ->disableOriginalConstructor()
                                         ->getMock();

        $this->mailMock = $this->getMockBuilder('\Core\Mail\HTMLTemplateMessage')
            ->disableOriginalConstructor()
            ->getMock();


        $this->testedObject = new Register($this->userRepositoryMock, $this->mailServiceMock);

        $this->inputFilterMock = $this->getMock('Zend\InputFilter\InputFilterInterface');
        $this->mailerPluginMock = $this->getMock('Core\Controller\Plugin\Mailer');
        $this->urlPluginMock = $this->getMock('Zend\Mvc\Controller\Plugin\Url');
    }

    public function testProceed_WhenInputFilterIsInvalid()
    {
        $this->inputFilterMock->expects($this->once())
            ->method('isValid')
            ->willReturn(false);

        $this->setExpectedException('LogicException', 'Form is not valid');

        $this->testedObject->proceed($this->inputFilterMock, $this->mailerPluginMock, $this->urlPluginMock);
    }

    public function testProceed_WhenUserAlreadyExists()
    {
        $name = uniqid('name');
        $email = uniqid('email') . '@' . uniqid('host') . '.com.pl';
        $user = UserEntityProvider::createEntityWithRandomData();

        $this->inputFilterMock->expects($this->once())
            ->method('isValid')
            ->willReturn(true);

        $this->inputFilterMock->expects($this->once())
            ->method('get')
            ->with('register')
            ->willReturnSelf();

        $this->inputFilterMock->expects($this->exactly(2))
            ->method('getValue')
            ->willReturnOnConsecutiveCalls($name, $email);

        $this->userRepositoryMock->expects($this->once())
            ->method('findByLoginOrEmail')
            ->with($email)
            ->willReturn($user);

        $this->setExpectedException('Auth\Service\Exception\UserAlreadyExistsException', 'User already exists');

        $this->testedObject->proceed($this->inputFilterMock, $this->mailerPluginMock, $this->urlPluginMock);<commit_msg>[ci] corrects a detail in the unit-tests<commit_after><?php
/**
 * YAWIK
 *
 * @filesource
 * @copyright (c) 2013-2015 Cross Solution (http://cross-solution.de)
 * @license       MIT
 */

namespace AuthTest\Service;

use Auth\Entity\User;
use Auth\Service\ForgotPassword;
use Auth\Service\Register;
use AuthTest\Entity\Provider\UserEntityProvider;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

class RegisterTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var ForgotPassword
     */
    private $testedObject;

    /**
     * @var MockObject
     */
    private $userRepositoryMock;

    /**
     * @var MockObject
     */
    private $mailServiceMock;

    /**
     * @var MockObject
     */
    private $mailMock;

    /**
     * @var MockObject
     */
    private $inputFilterMock;

    /**
     * @var MockObject
     */
    private $mailerPluginMock;

    /**
     * @var MockObject
     */
    private $urlPluginMock;

    /**
     * @var MockObject
     */
    public function setUp()
    {
        $this->userRepositoryMock = $this->getMockBuilder('Auth\Repository\User')
            ->disableOriginalConstructor()
            ->getMock();

        $this->mailServiceMock = $this->getMockBuilder('Core\Mail\MailService')
                                         ->disableOriginalConstructor()
                                         ->getMock();

        $this->mailMock = $this->getMockBuilder('\Core\Mail\HTMLTemplateMessage')
            ->disableOriginalConstructor()
            ->getMock();


        $this->testedObject = new Register($this->userRepositoryMock, $this->mailServiceMock);

        $this->inputFilterMock = $this->getMock('Zend\InputFilter\InputFilterInterface');
        $this->mailerPluginMock = $this->getMock('Core\Controller\Plugin\Mailer');
        $this->urlPluginMock = $this->getMock('Zend\Mvc\Controller\Plugin\Url');
    }

    public function testProceed_WhenInputFilterIsInvalid()
    {
        $this->inputFilterMock->expects($this->once())
            ->method('isValid')
            ->willReturn(false);

        $this->setExpectedException('LogicException', 'Form is not valid');

        $this->testedObject->proceed($this->inputFilterMock, $this->mailerPluginMock, $this->urlPluginMock);
    }

    /**
     * if the user already exists, there should be no user returned,
     * therefore the program has nothing it can owrk on,
     * the existing entity remains untouched
     */
    public function testProceed_WhenUserAlreadyExists()
    {
        $name = uniqid('name');
        $email = uniqid('email') . '@' . uniqid('host') . '.com.pl';
        $user = UserEntityProvider::createEntityWithRandomData();

        $this->inputFilterMock->expects($this->once())
            ->method('isValid')
            ->willReturn(true);

        $this->inputFilterMock->expects($this->once())
            ->method('get')
            ->with('register')
            ->willReturnSelf();

        $this->inputFilterMock->expects($this->exactly(2))
            ->method('getValue')
            ->willReturnOnConsecutiveCalls($name, $email);

        // this does the trick with providing an already existing user
        $this->userRepositoryMock->expects($this->once())
            ->method('findByLoginOrEmail')
            ->with($email)
            ->willReturn($user);

        $this->assertEmpty($this->testedObject->proceed($this->inputFilterMock, $this->mailerPluginMock, $this->urlPluginMock));
{'fraction_non_alphanumeric': 0.14370458253003116, 'fraction_numerical': 0.002669434969598102, 'mean_word_length': 2.481672689726381, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 123, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6607874', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1838, 'n_words': 298}