|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | use Illuminate\Support\Facades\Event; |
| 4 | +use StevenFox\LaravelModelValidation\Exceptions\ModelValidationException; |
4 | 5 | use StevenFox\LaravelModelValidation\Listeners\ValidateModel; |
5 | 6 | use StevenFox\LaravelModelValidation\Tests\Fixtures\ValidatesWhenSavingModel; |
6 | 7 | use StevenFox\LaravelModelValidation\Tests\Fixtures\ValidatingModel; |
|
21 | 22 | Event::forget('*'); |
22 | 23 | Event::fake(); |
23 | 24 |
|
24 | | - $m = new ValidatingModel(); |
| 25 | + $m = new ValidatesWhenSavingModel(); |
25 | 26 | $modelClassName = $m::class; |
26 | 27 |
|
27 | 28 | expect($m::shouldValidateWhenSaving())->toBeTrue(); |
28 | 29 |
|
29 | | - Event::assertListening("eloquent.updating: {$modelClassName}", Closure::class); |
| 30 | + Event::assertListening("eloquent.updating: {$modelClassName}", ValidateModel::class); |
30 | 31 | }); |
31 | 32 |
|
32 | 33 | it('will not register the model event listeners when shouldValidateOnSaving() is false', function () { |
|
44 | 45 | ->and(Event::hasListeners("eloquent.creating: {$modelClassName}")) |
45 | 46 | ->toBeFalse(); |
46 | 47 | }); |
47 | | -it('will re-register the model event listeners when reactivateValidationWhenSaving() is invoked', function () { |
| 48 | + |
| 49 | +it('the model event listeners will be registered even if validation is disabled during boot', function () { |
48 | 50 | Event::forget('*'); |
49 | 51 | Event::fake(); |
50 | 52 |
|
51 | 53 | ValidatingModel::disableValidationWhenSaving(); |
52 | 54 |
|
53 | | - $m = new ValidatingModel(); |
| 55 | + $m = new ValidatesWhenSavingModel(); |
54 | 56 | $modelClassName = $m::class; |
55 | 57 |
|
56 | 58 | ValidatingModel::reactivateValidationWhenSaving(); |
|
63 | 65 | }); |
64 | 66 |
|
65 | 67 | it('shouldNotValidateWhenSaving() will return the opposite of shouldValidateWhenSaving()', function () { |
66 | | - expect(ValidatingModel::shouldNotValidateWhenSaving()) |
| 68 | + expect(ValidatesWhenSavingModel::shouldNotValidateWhenSaving()) |
67 | 69 | ->toBeFalse() |
68 | | - ->toBe(! ValidatingModel::shouldValidateWhenSaving()); |
| 70 | + ->toBe(! ValidatesWhenSavingModel::shouldValidateWhenSaving()); |
69 | 71 |
|
70 | | - ValidatingModel::disableValidationWhenSaving(); |
| 72 | + ValidatesWhenSavingModel::disableValidationWhenSaving(); |
71 | 73 |
|
72 | | - expect(ValidatingModel::shouldNotValidateWhenSaving()) |
| 74 | + expect(ValidatesWhenSavingModel::shouldNotValidateWhenSaving()) |
73 | 75 | ->toBeTrue() |
74 | | - ->toBe(! ValidatingModel::shouldValidateWhenSaving()); |
| 76 | + ->toBe(! ValidatesWhenSavingModel::shouldValidateWhenSaving()); |
75 | 77 | }); |
76 | 78 |
|
77 | 79 | it('provides a whileValidatingDisabled() function to run a callback while validation is disabled', function () { |
|
89 | 91 |
|
90 | 92 | it('will use database-prepared attribute values for validation by default', function () { |
91 | 93 | $m = new ValidatingModel([ |
| 94 | + 'stringable' => str('foo'), |
| 95 | + 'datetime' => \Illuminate\Support\Facades\Date::create(2000, 1, 1), |
92 | 96 | 'json' => ['foo' => 'bar'], |
| 97 | + 'array_object' => ['foo' => 'bar'], |
| 98 | + 'collection' => collect(['foo' => 'bar']), |
93 | 99 | ]); |
94 | 100 |
|
95 | 101 | expect($m->validationData())->toBe([ |
96 | | - 'json' => \Illuminate\Database\Eloquent\Casts\Json::encode(['foo' => 'bar']), |
| 102 | + 'stringable' => 'foo', |
| 103 | + 'datetime' => '2000-01-01 00:00:00', |
| 104 | + 'json' => '{"foo":"bar"}', |
| 105 | + 'array_object' => '{"foo":"bar"}', |
| 106 | + 'collection' => '{"foo":"bar"}', |
97 | 107 | ]); |
98 | 108 | }); |
99 | 109 |
|
100 | | -it('will stop the validation process and return false if a "validating" event listener returns false', function () { |
101 | | - Event::listen( |
102 | | - 'eloquent.validating: '.ValidatingModel::class, |
103 | | - function (ValidatingModel $model, \Illuminate\Validation\Validator $validator) { |
104 | | - return false; |
105 | | - }); |
106 | | - |
107 | | - $m = new ValidatingModel(); |
108 | | - |
109 | | - expect($m->validate())->toBeFalse(); |
110 | | -}); |
111 | | - |
112 | | -it('will stop the validation process and return false if a "validated" event listener returns false', function () { |
113 | | - Event::listen( |
114 | | - 'eloquent.validated: '.ValidatingModel::class, |
115 | | - function (ValidatingModel $model, \Illuminate\Validation\Validator $validator) { |
116 | | - return false; |
117 | | - }); |
118 | | - |
119 | | - $m = new ValidatingModel(); |
120 | | - |
121 | | - expect($m->validate())->toBeFalse(); |
122 | | -}); |
123 | | - |
124 | | -it('can use temporary validation rules', function () { |
| 110 | +it('can use superseding validation rules', function () { |
125 | 111 | $m = new ValidatingModel([ |
126 | 112 | 'required_string' => null, |
127 | 113 | ]); |
128 | 114 |
|
129 | | - $m->setTemporaryValidationRules([ |
| 115 | + $m->setSupersedingValidationRules([ |
130 | 116 | 'required_string' => 'nullable|string', |
131 | 117 | ]); |
132 | 118 |
|
133 | | - expect($m->getTemporaryValidationRules()) |
| 119 | + expect($m->getSupersedingValidationRules()) |
134 | 120 | ->toBe(['required_string' => 'nullable|string']) |
135 | 121 | ->and($m->makeValidator()->getRules()) |
136 | 122 | ->toBe(['required_string' => ['nullable', 'string']]) |
137 | 123 | ->and($m->validate()->fails()) |
138 | 124 | ->toBeFalse(); |
139 | 125 | }); |
140 | 126 |
|
141 | | -it('can clear the temporary validation rules', function () { |
| 127 | +it('can clear the superseding validation rules', function () { |
142 | 128 | $m = new ValidatingModel(); |
143 | 129 |
|
144 | | - $m->setTemporaryValidationRules([ |
| 130 | + $m->setSupersedingValidationRules([ |
145 | 131 | 'required_string' => 'nullable|string', |
146 | 132 | ]); |
147 | 133 |
|
148 | | - expect($m->getTemporaryValidationRules())->not()->toBeEmpty(); |
| 134 | + expect($m->getSupersedingValidationRules())->not()->toBeEmpty() |
| 135 | + ->and($m->makeValidator()->getRules())->toBe(['required_string' => ['nullable', 'string']]); |
149 | 136 |
|
150 | | - $m->clearTemporaryValidationRules(); |
| 137 | + $m->clearSupersedingValidationRules(); |
151 | 138 |
|
152 | | - expect($m->getTemporaryValidationRules())->toBeEmpty(); |
| 139 | + expect($m->getSupersedingValidationRules())->toBeEmpty() |
| 140 | + ->and($m->makeValidator()->getRules())->not()->toBe(['required_string' => ['nullable', 'string']]); |
153 | 141 | }); |
154 | 142 |
|
155 | 143 | it('uses independent rules for updating vs creating', function () { |
156 | 144 | $m = new ValidatingModel(); |
157 | 145 |
|
158 | 146 | expect($m->exists)->toBeFalse() |
159 | | - ->and($m->makeValidator()->getRules()['unique']) |
160 | | - ->toBe(['unique:short_urls,url_key,NULL,id']); |
| 147 | + ->and($m->makeValidator()->getRules()['unique_column']) |
| 148 | + ->toBe(['unique:validating_models,NULL,NULL,id']); |
161 | 149 |
|
162 | 150 | $m->id = 127; |
163 | 151 | $m->exists = true; |
164 | 152 |
|
165 | 153 | expect($m->exists)->toBeTrue() |
166 | | - ->and($m->makeValidator()->getRules()['unique']) |
167 | | - ->toBe(['unique:short_urls,url_key,"127",id']); // The model key is now a part of the rule for ignoring |
| 154 | + ->and($m->makeValidator()->getRules()['unique_column']) |
| 155 | + ->toBe(['unique:validating_models,NULL,"127",id']); // The model key is now a part of the rule for ignoring |
168 | 156 | }); |
0 commit comments