Skip to content

Commit

Permalink
adds test for Validator Factory (#42408)
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 authored May 17, 2022
1 parent 465dbee commit 658800d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Validation/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Factory implements FactoryContract
/**
* The IoC container instance.
*
* @var \Illuminate\Contracts\Container\Container
* @var \Illuminate\Contracts\Container\Container|null
*/
protected $container;

Expand Down Expand Up @@ -313,7 +313,7 @@ public function setPresenceVerifier(PresenceVerifierInterface $presenceVerifier)
/**
* Get the container instance used by the validation factory.
*
* @return \Illuminate\Contracts\Container\Container
* @return \Illuminate\Contracts\Container\Container|null
*/
public function getContainer()
{
Expand Down
45 changes: 45 additions & 0 deletions tests/Validation/ValidationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Validation;

use Illuminate\Container\Container;
use Illuminate\Contracts\Translation\Translator as TranslatorInterface;
use Illuminate\Validation\Factory;
use Illuminate\Validation\PresenceVerifierInterface;
Expand Down Expand Up @@ -106,4 +107,48 @@ public function testValidateMethodCanBeCalledPublicly()
$validator = $factory->make(['bar' => ['baz']], ['bar' => 'foo']);
$this->assertTrue($validator->passes());
}

public function testExcludeAndIncludeUnvalidatedArrayKeys()
{
$translator = m::mock(TranslatorInterface::class);

$factory = new Factory($translator);
// check the default behaviour.
$validator1 = $factory->make(['key' => ['val']], ['key' => 'required']);
$this->assertTrue($validator1->excludeUnvalidatedArrayKeys);

$factory->excludeUnvalidatedArrayKeys();
$validator2 = $factory->make(['key' => ['val']], ['key' => 'required']);
$this->assertTrue($validator2->excludeUnvalidatedArrayKeys);

$factory->includeUnvalidatedArrayKeys();
$validator3 = $factory->make(['key' => ['val']], ['key' => 'required']);
$this->assertFalse($validator3->excludeUnvalidatedArrayKeys);

// checks it does not switch behaviour automatically.
$validator4 = $factory->make(['key' => ['val']], ['key' => 'required']);
$this->assertFalse($validator4->excludeUnvalidatedArrayKeys);

// checks it can switch.
$factory->excludeUnvalidatedArrayKeys();
$validator5 = $factory->make(['key' => ['val']], ['key' => 'required']);
$this->assertTrue($validator5->excludeUnvalidatedArrayKeys);

// checks switching does not affect previously created validator objects.
$this->assertTrue($validator1->excludeUnvalidatedArrayKeys);
$this->assertTrue($validator2->excludeUnvalidatedArrayKeys);
$this->assertFalse($validator3->excludeUnvalidatedArrayKeys);
$this->assertFalse($validator4->excludeUnvalidatedArrayKeys);
}

public function testSetContainer()
{
$translator = m::mock(TranslatorInterface::class);
$container = new Container;
$factory = new Factory($translator);

$this->assertNull($factory->getContainer());

$this->assertSame($container, $factory->setContainer($container)->getContainer());
}
}

0 comments on commit 658800d

Please sign in to comment.