<commit_before><?php
/* ===========================================================================
 * Copyright 2019 Zindex Software
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============================================================================ */

namespace Opis\Validation\Test\Rules;

use Opis\Validation\Validator;
use PHPUnit\Framework\TestCase;

class RequiredTest extends TestCase
{
    /** @var Validator */
    protected $v;

    /**
     * @inheritDoc
     */
    public function setUp()
    {
        $this->v = new Validator();
    }

    public function testRequiredFail()
    {
        $this->v
            ->field('foo')
            ->required();

        $result = $this->v->validate();
        $this->assertFalse($result->isValid());
        $this->assertEquals('foo is required', $result->getError('foo'));
    }

    public function testRequiredFailCustomMessage()
    {
        $this->v
            ->field('foo')
            ->required()->setError('Error foo');

        $result = $this->v->validate();
        $this->assertFalse($result->isValid());
        $this->assertEquals('Error foo', $result->getError('foo'));
    }

    public function testRequiredFailNullValue()
    {
        $this->v
            ->field('foo')
            ->required();

        $data = [
            'foo' => null,
        ];

        $result = $this->v->validate($data);
        $this->assertFalse($result->isValid());
    }

    public function testRequiredFailCustomTrim()
    {
        $this->v
            ->field('foo')
            ->required('x');

        $data = [
            'foo' => 'xxx',
        ];

        $result = $this->v->validate($data);
        $this->assertFalse($result->isValid());
    }

    public function testRequiredFailCustomName()
    {
        $this->v
            ->field('foo', 'bar')
            ->required();
<commit_msg>Updated test<commit_after><?php
/* ===========================================================================
 * Copyright 2019 Zindex Software
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============================================================================ */

namespace Opis\Validation\Test\Rules;

use Opis\Validation\Validator;
use PHPUnit\Framework\TestCase;

class RequiredTest extends TestCase
{
    /** @var Validator */
    protected $v;

    /**
     * @inheritDoc
     */
    public function setUp()
    {
        $this->v = new Validator();
    }

    public function testRequiredFail()
    {
        $this->v
            ->field('foo')
            ->required();

        $result = $this->v->validate();
        $this->assertFalse($result->isValid());
        $this->assertEquals('foo is required', $result->getError('foo'));
    }

    public function testRequiredFailCustomMessage()
    {
        $this->v
            ->field('foo')
            ->required()->setError('Error foo');

        $result = $this->v->validate();
        $this->assertFalse($result->isValid());
        $this->assertEquals('Error foo', $result->getError('foo'));
    }

    public function testRequiredFailNullValue()
    {
        $this->v
            ->field('foo')
            ->required();

        $data = [
            'foo' => null,
        ];

        $result = $this->v->validate($data);
        $this->assertFalse($result->isValid());
    }

    public function testRequiredFailCustomTrim()
    {
        $this->v
            ->field('foo')
            ->required('x');

        $data = [
            'foo' => 'xxx',
        ];

        $result = $this->v->validate($data);
        $this->assertFalse($result->isValid());
    }

    public function testRequiredFailUseOriginalValue()
    {
        $this->v
            ->field('foo')
            ->required('x')->setError('%value');

        $data = [
            'foo' => 'xxx',
        ];

        $result = $this->v->validate($data);
        $this->assertFalse($result->isValid());
        $this->assertEquals('xxx', $result->getError('foo'));
    }

    public function testRequiredFailCustomName()
    {
        $this->v
            ->field('foo', 'bar')
            ->required();

{'fraction_non_alphanumeric': 0.19957248348231635, 'fraction_numerical': 0.003109211037699184, 'mean_word_length': 2.372870249017038, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 92, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3717839', 'n_tokens_mistral': 1357, 'n_tokens_neox': 1308, 'n_words': 337}