<commit_before><?php

use Felixkiss\UniqueWithValidator\ValidatorExtension;

class ValidatorExtensionTest extends PHPUnit_Framework_TestCase
{
    protected $translator;
    protected $data;
    protected $rules;
    protected $messages;
    protected $presenceVerifier;
    protected $testDefaultErrorMessage = 'This is a test error message with :fields.';

    public function setUp()
    {
        $this->translator = Mockery::mock('Symfony\Component\Translation\TranslatorInterface');
        $this->translator->shouldReceive('get')
            ->with('uniquewith-validator::validation.unique_with')
            ->andReturn($this->testDefaultErrorMessage);
        $this->translator->shouldReceive('trans')
            ->andReturnUsing(function($arg) { return $arg; });

        $this->rules = array(
            'first_name' => 'unique_with:users,last_name',
        );

        $this->presenceVerifier = Mockery::mock('Illuminate\Validation\PresenceVerifierInterface');

        $this->messages = array();
    }

    public function tearDown()
    {
        Mockery::close();
    }

    public function testValidatesNewCombination()
    {
        $this->data = array(
            'first_name' => 'Foo',
            'last_name' => 'Bar',
        );
        $validator = new ValidatorExtension(
            $this->translator,
            $this->data,
            $this->rules,
            $this->messages
        );
        $validator->setPresenceVerifier($this->presenceVerifier);

        // No existing Object with this parameter set
        $this->presenceVerifier
             ->shouldReceive('getCount')
             ->with(
                   'users',
                   'first_name',
                   'Foo',
                   null,
                   null,
                   array('last_name' => 'Bar')
               )
             ->once()
             ->andReturn(0);

        $this->assertFalse($validator->fails());
    }

    public function testValidatesExistingCombination()
    {
        $this->data = array(
            'first_name' => 'Foo',
            'last_name' => 'Bar',
        );
        $validator = new ValidatorExtension(
            $this->translator,
            $this->data,
            $this->rules,
            $this->messages
        );
        $validator->setPresenceVerifier($this->presenceVerifier);

        // One existing Object with this parameter set
        $this->presenceVerifier
             ->shouldReceive('getCount')
             ->with(
                   'users',
                   'first_name',
                   'Foo',
                   null,
                   null,
                   array('last_name' => 'Bar')
               )
             ->once()
             ->andReturn(1);

        $this->assertTrue($validator->fails());
    }

    public function testDefaultErrorMessageWorks()
    {
        $this->setUpFailCaseTest();

        $validator = new ValidatorExtension(
            $this->translator,
            $this->data,
            $this->rules,
            $this->messages
        );

        $custom_messages = $validator->getCustomMessages();
        $this->assertArrayHasKey('unique_with', $custom_messages);
        $this->assertEquals($custom_messages['unique_with'], $this->testDefaultErrorMessage);

        $validator->setPresenceVerifier($this->presenceVerifier);

        // One existing Object with this parameter set
        $this->presenceVerifier
             ->shouldReceive('getCount')
             ->with(
                   'users',
                   'first_name',
                   'Foo',
                   null,
                   null,
                   array('last_name' => 'Bar')
               )
             ->once()
             ->andReturn(1);

        $errors = $validator->messages();
        $this->assertTrue(is_object($errors), 'Asserting that $validator->messages() returns an object.');
        $this->assertInstanceOf('Illuminate\Support\MessageBag', $errors);

        $errors = $errors->toArray();
        $this->assertArrayHasKey('first_name', $errors);
        $this->assertNotEmpty($errors['first_name']);
    }

    public function testErrorMessageOverrideWorks()
    {
        $test_message = 'This is a test override message with :fields.';

        $this->setUpFailCaseTest();

        $validator = new ValidatorExtension(
            $this->translator,
            $this->data,
            $this->rules,
            ['unique_with' => $test_message]
        );

        $custom_messages = $validator->getCustomMessages();
        $this->assertArrayHasKey('unique_with', $custom_messages);
        $this->assertEquals($custom_messages['unique_with'], $test_message);

        $validator->setPresenceVerifier($this->presenceVerifier);

        // One existing Object with this parameter set
        $this->presenceVerifier
             ->shouldReceive('getCount')
             ->with(
                   'users',
                   'first_name',
                   'Foo',
                   null,<commit_msg>Updated array syntax for compatibility with php 5.3<commit_after><?php

use Felixkiss\UniqueWithValidator\ValidatorExtension;

class ValidatorExtensionTest extends PHPUnit_Framework_TestCase
{
    protected $translator;
    protected $data;
    protected $rules;
    protected $messages;
    protected $presenceVerifier;
    protected $testDefaultErrorMessage = 'This is a test error message with :fields.';

    public function setUp()
    {
        $this->translator = Mockery::mock('Symfony\Component\Translation\TranslatorInterface');
        $this->translator->shouldReceive('get')
            ->with('uniquewith-validator::validation.unique_with')
            ->andReturn($this->testDefaultErrorMessage);
        $this->translator->shouldReceive('trans')
            ->andReturnUsing(function($arg) { return $arg; });

        $this->rules = array(
            'first_name' => 'unique_with:users,last_name',
        );

        $this->presenceVerifier = Mockery::mock('Illuminate\Validation\PresenceVerifierInterface');

        $this->messages = array();
    }

    public function tearDown()
    {
        Mockery::close();
    }

    public function testValidatesNewCombination()
    {
        $this->data = array(
            'first_name' => 'Foo',
            'last_name' => 'Bar',
        );
        $validator = new ValidatorExtension(
            $this->translator,
            $this->data,
            $this->rules,
            $this->messages
        );
        $validator->setPresenceVerifier($this->presenceVerifier);

        // No existing Object with this parameter set
        $this->presenceVerifier
             ->shouldReceive('getCount')
             ->with(
                   'users',
                   'first_name',
                   'Foo',
                   null,
                   null,
                   array('last_name' => 'Bar')
               )
             ->once()
             ->andReturn(0);

        $this->assertFalse($validator->fails());
    }

    public function testValidatesExistingCombination()
    {
        $this->data = array(
            'first_name' => 'Foo',
            'last_name' => 'Bar',
        );
        $validator = new ValidatorExtension(
            $this->translator,
            $this->data,
            $this->rules,
            $this->messages
        );
        $validator->setPresenceVerifier($this->presenceVerifier);

        // One existing Object with this parameter set
        $this->presenceVerifier
             ->shouldReceive('getCount')
             ->with(
                   'users',
                   'first_name',
                   'Foo',
                   null,
                   null,
                   array('last_name' => 'Bar')
               )
             ->once()
             ->andReturn(1);

        $this->assertTrue($validator->fails());
    }

    public function testDefaultErrorMessageWorks()
    {
        $this->setUpFailCaseTest();

        $validator = new ValidatorExtension(
            $this->translator,
            $this->data,
            $this->rules,
            $this->messages
        );

        $custom_messages = $validator->getCustomMessages();
        $this->assertArrayHasKey('unique_with', $custom_messages);
        $this->assertEquals($custom_messages['unique_with'], $this->testDefaultErrorMessage);

        $validator->setPresenceVerifier($this->presenceVerifier);

        // One existing Object with this parameter set
        $this->presenceVerifier
             ->shouldReceive('getCount')
             ->with(
                   'users',
                   'first_name',
                   'Foo',
                   null,
                   null,
                   array('last_name' => 'Bar')
               )
             ->once()
             ->andReturn(1);

        $errors = $validator->messages();
        $this->assertTrue(is_object($errors), 'Asserting that $validator->messages() returns an object.');
        $this->assertInstanceOf('Illuminate\Support\MessageBag', $errors);

        $errors = $errors->toArray();
        $this->assertArrayHasKey('first_name', $errors);
        $this->assertNotEmpty($errors['first_name']);
    }

    public function testErrorMessageOverrideWorks()
    {
        $test_message = 'This is a test override message with :fields.';

        $this->setUpFailCaseTest();

        $validator = new ValidatorExtension(
            $this->translator,
            $this->data,
            $this->rules,
            array('unique_with' => $test_message)
        );

        $custom_messages = $validator->getCustomMessages();
        $this->assertArrayHasKey('unique_with', $custom_messages);
        $this->assertEquals($custom_messages['unique_with'], $test_message);

        $validator->setPresenceVerifier($this->presenceVerifier);

        // One existing Object with this parameter set
        $this->presenceVerifier
             ->shouldReceive('getCount')
             ->with(
                   'users',
                   'first_name',
                   'Foo',
                   null,
{'fraction_non_alphanumeric': 0.12649589555929186, 'fraction_numerical': 0.0007912174859064385, 'mean_word_length': 1.7833746215249104, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 171, 'https://': 0, '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': '5007357', 'n_tokens_mistral': 2635, 'n_tokens_neox': 2558, 'n_words': 446}