Skip to content

Commit

Permalink
Merge branch '5.x'
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Mar 16, 2020
2 parents 7925d85 + d5abe01 commit be93d50
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}

- name: Setup PHP
uses: shivammathur/setup-php@v1
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, bcmath, intl
Expand Down
14 changes: 8 additions & 6 deletions src/Support/Concerns/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Validation\Validator as ValidatorContract;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Validator;
use Orchestra\Support\Arr;
use Illuminate\Support\Fluent;
use Orchestra\Support\Str;

Expand Down Expand Up @@ -90,7 +91,9 @@ protected function getBindedRules(): array

if (! empty($this->validationBindings)) {
foreach ($rules as $key => $value) {
$rules[$key] = Str::replace($value, $this->validationBindings);
if (\is_string($value)) {
$rules[$key] = Str::replace($value, $this->validationBindings);
}
}
}

Expand Down Expand Up @@ -137,10 +140,8 @@ final protected function runExtendedScenario(ValidatorContract $validator): void
*/
final protected function runValidationEvents($events, array $phrases): array
{
\is_array($events) || $events = (array) $events;

// Merge all the events.
$events = \array_merge($this->getValidationEvents(), $events);
$events = \array_merge($this->getValidationEvents(), Arr::wrap($events));

// Convert rules array to Fluent, in order to pass it by references
// in all event listening to this validation.
Expand All @@ -152,7 +153,8 @@ final protected function runValidationEvents($events, array $phrases): array
}

return [
$rules->getAttributes(), $phrases->getAttributes(),
$rules->getAttributes(),
$phrases->getAttributes(),
];
}

Expand Down Expand Up @@ -195,7 +197,7 @@ public function getValidationRules(): array
*/
protected function getValidationSchemasName(string $scenario): array
{
$scenario = \ucfirst($scenario);
$scenario = Str::camel($scenario);

$on = "on{$scenario}";
$extend = "extend{$scenario}";
Expand Down
2 changes: 1 addition & 1 deletion tests/Concerns/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Orchestra\Support\Tests\Concerns;

use Orchestra\Support\Concerns\Validation;
use PHPUnit\Framework\TestCase;
use Orchestra\Testbench\TestCase;

class ValidationTest extends TestCase
{
Expand Down
59 changes: 17 additions & 42 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,91 +4,66 @@

use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Validator;
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
use Illuminate\Contracts\Validation\Validator as ValidatorContract;
use Mockery as m;
use Orchestra\Testbench\TestCase;

class ValidatorTest extends TestCase
{
/**
* Setup the test environment.
*/
protected function setUp(): void
{
parent::setUp();

$_SERVER['validator.onFoo'] = null;
$_SERVER['validator.onFoo'] = null;
}

/**
* Teardown the test environment.
*/
protected function tearDown(): void
{
parent::tearDown();

unset($_SERVER['validator.onFoo']);
unset($_SERVER['validator.extendFoo']);
m::close();
}

/** @test */
public function it_can_validate()
{

$rules = ['email' => ['email', 'foo:2'], 'name' => 'any'];
$input = ['email' => 'crynobone@gmail.com'];
$phrases = ['email.required' => 'Email required'];

Event::shouldReceive('dispatch')->once()->with('foo.event', m::any())->andReturn(null);
Validator::shouldReceive('make')->once()->with([], $rules, $phrases)
->andReturn(m::mock('Illuminate\Contracts\Validation\Validator'));
Event::shouldReceive('dispatch')->once()->with('foo.event', m::any())->andReturnNull();

$stub = new FooValidator();
$stub->on('foo', ['orchestra'])->bind(['id' => '2']);
$stub->on('optional-name', ['orchestra'])->bind(['id' => '2']);

$validation = $stub->with([], 'foo.event');
$validation = $stub->with($input, 'foo.event');

$this->assertEquals('orchestra', $_SERVER['validator.onFoo']);
$this->assertEquals($validation, $_SERVER['validator.extendFoo']);
$this->assertInstanceOf('Illuminate\Contracts\Validation\Validator', $validation);
$this->assertTrue($validation->passes());
}

/** @test */
public function it_can_validate_without_scope()
{
$rules = ['email' => ['email', 'foo:2'], 'name' => 'any'];
$phrases = ['email.required' => 'Email required', 'name' => 'Any name'];

Validator::shouldReceive('make')->once()->with([], $rules, $phrases)
->andReturn(m::mock('Illuminate\Contracts\Validation\Validator'));
$input = ['email' => 'crynobone@gmail.com', 'name' => 'Mior Muhammad Zaki'];
$phrases = ['email.required' => 'Email required'];

$stub = new FooValidator();
$stub->bind(['id' => '2']);

$validation = $stub->with([], null, ['name' => 'Any name']);
$validation = $stub->with($input, [], $phrases);

$this->assertInstanceOf('Illuminate\Contracts\Validation\Validator', $validation);
$this->assertTrue($validation->passes());
}
}

class FooValidator extends \Orchestra\Support\Validator
{
protected $rules = [
'email' => ['email', 'foo:{id}'],
'name' => 'any',
'email' => ['email'],
'name' => ['required'],
];

protected $phrases = [
'email.required' => 'Email required',
];

protected function onFoo($value)
protected function onOptionalName($value)
{
$_SERVER['validator.onFoo'] = $value;
$this->rules['name'] = ['sometimes'];
}

protected function extendFoo($validation)
protected function extendOptionalName(ValidatorContract $validator)
{
$_SERVER['validator.extendFoo'] = $validation;
//
}
}

0 comments on commit be93d50

Please sign in to comment.