diff --git a/src/Tests/ValidationTrait.php b/src/Tests/ValidationTrait.php new file mode 100644 index 0000000..def9376 --- /dev/null +++ b/src/Tests/ValidationTrait.php @@ -0,0 +1,112 @@ + + */ + 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])); + } + } + } +} diff --git a/src/resources/model.stub b/src/resources/model.stub index c413af6..88790b3 100644 --- a/src/resources/model.stub +++ b/src/resources/model.stub @@ -57,7 +57,7 @@ class DummyClass extends Model ]; /** - * '',[] => null convertation + * '',[] => null convert * * @var array */ @@ -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) *