public function testErrorSetterMethods() { $o1 = new RestResponse(); $o1->setErrors(['first-error', 'second-error']); $o2 = new RestResponse(); $o2->addError('first-error')->addError('second-error'); $arr1 = $o1->asArray(); $arr2 = $o2->asArray(); $this->assertCount(2, $arr1['errors']); $this->assertCount(2, $arr2['errors']); $this->assertEquals('first-error', $arr1['errors'][0]); $this->assertEquals('second-error', $arr1['errors'][1]); $this->assertEquals('first-error', $arr2['errors'][0]); $this->assertEquals('second-error', $arr2['errors'][1]); $arrWithErrors1 = $o1->getErrors(); $arrWithErrors2 = $o2->getErrors(); $this->assertEquals('first-error', $arrWithErrors1[0]); $this->assertEquals('second-error', $arrWithErrors1[1]); $this->assertEquals('first-error', $arrWithErrors2[0]); $this->assertEquals('second-error', $arrWithErrors2[1]); }
/** * @param RestResponse $restResponse * @param array $serializationGroups * @param int $httpOKStatus при POST запросе задайте этот параметр в 201 * * @return View */ public static function generateViewFromRestResponse(RestResponse $restResponse, $serializationGroups = [], $httpOKStatus = 200) { $HTTPStatusCode = 200; switch ($restResponse->getStatusCode()) { case RestResponse::STATUS_CODE_OK: if (!in_array($httpOKStatus, [200, 201])) { $HTTPStatusCode = 200; } else { $HTTPStatusCode = $httpOKStatus; } break; case RestResponse::STATUS_CODE_ENTITY_NOT_FOUND: $HTTPStatusCode = 404; break; case RestResponse::STATUS_CODE_WRONG_INPUT_DATA: $HTTPStatusCode = 400; break; } $view = View::create($restResponse->asArray(), $HTTPStatusCode)->setSerializationContext(self::createSerializerContext($serializationGroups)); return $view; }