<commit_before><?php

use \Mockery as m;

use Coinon\Validation\BaseModel;
use Coinon\Validation\FactoryInterface;
use Coinon\Validation\ValidatorInterface;


class BaseModelTest extends PHPUnit_Framework_TestCase {

    /**
     * Validator mock instance.
     *
     * @var Coinon\Validation\ValidatorInterface
     */
    protected $validator;


    public function setUp()
    {
        $this->validatorFactory = m::mock('Coinon\Validation\FactoryInterface');
        BaseModel::setValidatorFactory($this->validatorFactory);
    }

    /**
     * @test
     */
    public function it_is_initializable()
    {
        $model = new BaseModel;

        $this->assertInstanceOf('Coinon\Validation\BaseModel', $model);
    }

    /**
     * @test
     */
    public function it_makes_validated_fields_fillable()
    {
        $model = new Book;

        $this->assertSame(['title', 'author'], $model->fillable);
    }

    /**
     * @test
     */
    public function it_fills_model_with_given_fields_having_a_validation_rule()
    {
        $bookFields = [
            'title'  => 'Gone with the wind',
            'author' => '<NAME>',
        ];
        $book = new Book($bookFields);

        $this->assertSame($bookFields, $book->toArray());
    }

    /**
     * @test
     */
    public function it_does_not_fill_fields_without_a_validation_rule()
    {
        $book = new Book([
            'price'  => 5.99,
        ]);
        $this->assertObjectNotHasAttribute('price', $book);
    }

    /**
     * @test
     */
    public function it_passes_validation()
    {
        $validator = m::mock(ValidatorInterface::class);
        $validator->shouldReceive('fails')->once()->andReturn(false);
        $validator->shouldReceive('errors')->never();
        $this->validatorFactory
            ->shouldReceive('make')->once()
            ->andReturn($validator);

        $book = new Book;

        $validates = $book->validate();

        $this->assertTrue($validates);
        $this->assertEmpty($book->errors());

    }

    /**
     * @test
     */
    public function it_returns_errors_on_failed_validation()
    {
        $errors = ['attribute' => 'Attribute is missing'];
        $validator = m::mock(ValidatorInterface::class);
        $validator->shouldReceive('fails')->once()->andReturn(true);
        $validator->shouldReceive('errors')->once()->andReturn($errors);
        $this->validatorFactory
            ->shouldReceive('make')->once()
            ->andReturn($validator);

        $book = new Book;

        $validates = $book->validate();

        $this->assertFalse($validates);
        $this->assertSame($errors, $book->errors());

    }

}
<commit_msg>Fix php 5.4 errors in tests<commit_after><?php

use \Mockery as m;

use Coinon\Validation\BaseModel;
use Coinon\Validation\FactoryInterface;
use Coinon\Validation\ValidatorInterface;


class BaseModelTest extends PHPUnit_Framework_TestCase {

    /**
     * Validator mock instance.
     *
     * @var Coinon\Validation\ValidatorInterface
     */
    protected $validator;


    public function setUp()
    {
        $this->validatorFactory = m::mock('Coinon\Validation\FactoryInterface');
        BaseModel::setValidatorFactory($this->validatorFactory);
    }

    /**
     * @test
     */
    public function it_is_initializable()
    {
        $model = new BaseModel;

        $this->assertInstanceOf('Coinon\Validation\BaseModel', $model);
    }

    /**
     * @test
     */
    public function it_makes_validated_fields_fillable()
    {
        $model = new Book;

        $this->assertSame(['title', 'author'], $model->fillable);
    }

    /**
     * @test
     */
    public function it_fills_model_with_given_fields_having_a_validation_rule()
    {
        $bookFields = [
            'title'  => 'Gone with the wind',
            'author' => '<NAME>',
        ];
        $book = new Book($bookFields);

        $this->assertSame($bookFields, $book->toArray());
    }

    /**
     * @test
     */
    public function it_does_not_fill_fields_without_a_validation_rule()
    {
        $book = new Book([
            'price'  => 5.99,
        ]);
        $this->assertObjectNotHasAttribute('price', $book);
    }

    /**
     * @test
     */
    public function it_passes_validation()
    {
        $validator = m::mock('Coinon\Validation\ValidatorInterface');
        $validator->shouldReceive('fails')->once()->andReturn(false);
        $validator->shouldReceive('errors')->never();
        $this->validatorFactory
            ->shouldReceive('make')->once()
            ->andReturn($validator);

        $book = new Book;

        $validates = $book->validate();

        $this->assertTrue($validates);
        $this->assertEmpty($book->errors());

    }

    /**
     * @test
     */
    public function it_returns_errors_on_failed_validation()
    {
        $errors = ['attribute' => 'Attribute is missing'];
        $validator = m::mock('Coinon\Validation\ValidatorInterface');
        $validator->shouldReceive('fails')->once()->andReturn(true);
        $validator->shouldReceive('errors')->once()->andReturn($errors);
        $this->validatorFactory
            ->shouldReceive('make')->once()
            ->andReturn($validator);

        $book = new Book;

        $validates = $book->validate();

        $this->assertFalse($validates);
        $this->assertSame($errors, $book->errors());

    }

}

{'fraction_non_alphanumeric': 0.1524074074074074, 'fraction_numerical': 0.0014814814814814814, 'mean_word_length': 2.407570977917981, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 83, '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': '83289', 'n_tokens_mistral': 1594, 'n_tokens_neox': 1549, 'n_words': 239}