File tree Expand file tree Collapse file tree 4 files changed +32
-3
lines changed
src/Validator/Relationships
test/Validator/Relationships Expand file tree Collapse file tree 4 files changed +32
-3
lines changed Original file line number Diff line number Diff line change @@ -257,7 +257,7 @@ protected function validate($value)
257257 $ this ->validateIdentifier ($ identifier , $ key );
258258 }
259259
260- if ($ this ->hasCallback ()) {
260+ if ($ this ->getErrors ()-> isEmpty () && $ this -> hasCallback ()) {
261261 $ this ->validateCallback ($ data );
262262 }
263263 }
Original file line number Diff line number Diff line change @@ -265,7 +265,7 @@ protected function validate($value)
265265 }
266266
267267 // check the callback, if one exists.
268- if ($ this ->hasCallback () && false == call_user_func ($ this ->getCallback (), $ data )) {
268+ if ($ this ->getErrors ()-> isEmpty () && $ this -> hasCallback () && false == call_user_func ($ this ->getCallback (), $ data )) {
269269 $ this ->error (static ::ERROR_NOT_FOUND , '/ ' . Relationship::DATA );
270270 }
271271 }
Original file line number Diff line number Diff line change @@ -195,4 +195,18 @@ public function testCallbackInvalidIndexes()
195195 $ this ->assertEquals ('/data/1 ' , $ error ->source ()->getPointer ());
196196 }
197197
198+ /**
199+ * @depends testCallback
200+ */
201+ public function testCallbackNotInvokedIfCollectionIsInvalid ()
202+ {
203+ $ this ->validator ->setTypes (static ::TYPE_A );
204+
205+ $ this ->validator ->setCallback (function () {
206+ $ this ->fail ('Not expecting callback to be invoked. ' );
207+ });
208+
209+ $ this ->assertFalse ($ this ->validator ->isValid ($ this ->input ));
210+ }
211+
198212}
Original file line number Diff line number Diff line change 2222use CloudCreativity \JsonApi \Object \ResourceIdentifier \ResourceIdentifier ;
2323use CloudCreativity \JsonApi \Validator \ValidatorTestCase ;
2424
25- class BelongsToValidatorTest extends ValidatorTestCase
25+ class HasOneValidatorTest extends ValidatorTestCase
2626{
2727
2828 const TYPE = 'foo ' ;
@@ -177,4 +177,19 @@ public function testCallbackInvalid()
177177 $ this ->assertEquals (404 , $ error ->getStatus ());
178178 $ this ->assertEquals ('/data ' , $ error ->source ()->getPointer ());
179179 }
180+
181+ /**
182+ * @depends testCallback
183+ */
184+ public function testCallbackNotInvokedForInvalidIdentifier ()
185+ {
186+ $ callback = function () {
187+ $ this ->fail ('Not expecting callback to be invoked. ' );
188+ };
189+
190+ $ this ->valid ->{Relationship::DATA }->{ResourceIdentifier::TYPE } = static ::INVALID_TYPE ;
191+
192+ $ this ->validator ->setCallback ($ callback );
193+ $ this ->assertFalse ($ this ->validator ->isValid ($ this ->valid ));
194+ }
180195}
You can’t perform that action at this time.
0 commit comments