<?php

namespace Alchemy\Tests\Phrasea\Form\Constraint;

use Alchemy\Phrasea\Form\Constraint\NewLoginValidator;

/**
 * @group functional
 * @group legacy
 */
class NewLoginValidatorTest extends \PhraseanetTestCase
{
    /**
     * @dataProvider provideVoteData
     */
    public function testValidate($value, $alreadyRegistered)
    {
        $context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
        $builder = $context
            ->expects($this->exactly($alreadyRegistered ? 1 : 0))
            ->method('addViolation');

        if ($alreadyRegistered) {
            $builder->with($this->isType('string'));
        }

        $validator = new NewLoginValidator();
        $validator->initialize($context);

        $constraint = $this->getConstraint();
        $constraint
            ->expects($this->once())
            ->method('isAlreadyRegistered')
            ->with($value)
            ->will($this->returnValue($alreadyRegistered));

        $validator->validate($value, $constraint);
    }

    public function provideVoteData()
    {
        return [
            ['romainneutron', true],
            ['romainneutron', false],
            ['', false],
            [null, false],
        ];
    }

    private function getConstraint()
    {
        return $this
            ->getMockBuilder('Alchemy\Phrasea\Form\Constraint\NewEmail')
            ->disableOriginalConstructor()
            ->getMock();
    }
}
