Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
AnourValar committed May 17, 2023
1 parent edcb3d8 commit 3681127
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 3 deletions.
112 changes: 112 additions & 0 deletions src/Tests/ValidationTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace AnourValar\EloquentValidation\Tests;

trait ValidationTrait
{
/**
* Asserts that validation succeeded
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Support\HigherOrderTapProxy<\Illuminate\Database\Eloquent\Model>
*/
protected function assertValidationSuccess(\Illuminate\Database\Eloquent\Model $model)
{
try {
$model->validate();
$this->assertTrue(true);

return tap($model);
} catch (\Illuminate\Validation\ValidationException $e) {
$this->assertFalse(true, 'Validation failed: ' . json_encode($e->validator->errors()->toArray()));
throw $e;
}
}

/**
* Asserts that delete validation succeeded
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Support\HigherOrderTapProxy<\Illuminate\Database\Eloquent\Model>
*/
protected function assertDeleteValidationSuccess(\Illuminate\Database\Eloquent\Model $model)
{
try {
$model->validateDelete();
$this->assertTrue(true);

return tap($model);
} catch (\Illuminate\Validation\ValidationException $e) {
$this->assertFalse(true, 'Validation failed: ' . json_encode($e->validator->errors()->keys()));
throw $e;
}
}

/**
* Asserts that validation failed
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param mixed $keys
* @param mixed $message
* @return void
*/
protected function assertValidationFailed(\Illuminate\Database\Eloquent\Model $model, $keys, $message = true): void
{
try {
$model->validate();
$this->assertFalse(true);
} catch (\Illuminate\Validation\ValidationException $e) {
$errors = $e->validator->errors()->toArray();
foreach ($e->validator->errors()->all() as $error) {
$this->assertStringNotContainsString('models/', $error);
}

foreach ((array) $keys as $key) {
$this->assertArrayHasKey(
$key,
$errors,
'Validation: ' . json_encode($e->validator->errors()->toArray(), JSON_UNESCAPED_UNICODE)
);

if ($message === true) {
continue;
}

$this->assertStringNotContainsString('models/', $message);
$this->assertEquals($message, \Arr::first($errors[$key]));
}
}
}

/**
* Asserts that delete validation failed
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param mixed $keys
* @param mixed $message
* @return void
*/
protected function assertDeleteValidationFailed(\Illuminate\Database\Eloquent\Model $model, $keys, $message = true): void
{
try {
$model->validateDelete();
$this->assertFalse(true);
} catch (\Illuminate\Validation\ValidationException $e) {
$errors = $e->validator->errors()->toArray();
foreach ($e->validator->errors()->all() as $error) {
$this->assertStringNotContainsString('models/', $error);
}

foreach ((array) $keys as $key) {
$this->assertArrayHasKey($key, $errors, 'Validation passed.');

if ($message === true) {
continue;
}

$this->assertStringNotContainsString('models/', $message);
$this->assertEquals($message, \Arr::first($errors[$key]));
}
}
}
}
6 changes: 3 additions & 3 deletions src/resources/model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class DummyClass extends Model
];

/**
* '',[] => null convertation
* '',[] => null convert
*
* @var array
*/
Expand Down Expand Up @@ -98,9 +98,9 @@ class DummyClass extends Model
/**
* Mutators for nested JSON.
* jsonb - sort an array by key
* nullable - '',[] => null convertation (nested)
* nullable - '',[] => null convert (nested)
* purges - remove null elements (nested)
* types - set the type of a value (nested)
* types - set the type of value (nested)
* sorts - sort an array (nested)
* lists - drop array keys (nested)
*
Expand Down

0 comments on commit 3681127

Please sign in to comment.