예제 #1
0
 public function setUp()
 {
     parent::setUp();
     $serviceManager = $this->getApplication()->getServiceManager();
     $hydratorManager = $serviceManager->get('HydratorManager');
     $this->hydrator = $hydratorManager->get(Hydrator::class);
 }
 /**
  * Dispatch request to API and take care of JSON request/response
  * @param string $method
  * @param array $params
  * @param int $expectedCode
  * @param string $message
  * @return array|null
  */
 protected function dispatchApi($method, array $params = [], $expectedCode = null, $message = null)
 {
     // Always accept JSON
     $headers = $this->getRequest()->getHeaders();
     $headers->addHeaderLine('Accept', 'application/json');
     // Pass parameters in request's content as JSON
     if (in_array($method, [Request::METHOD_PATCH, Request::METHOD_PUT], true)) {
         $headers->addHeaderLine('Content-Type', 'application/json');
         $this->getRequest()->setContent(json_encode($params));
         $params = null;
     }
     if (!$expectedCode) {
         $expectedCode = $this->getDefaultExpectedCode($method);
     }
     $url = $this->getUrl($method);
     if ($method === 'getList') {
         $method = Request::METHOD_GET;
     }
     parent::dispatch($url, $method, $params);
     $actualCode = $this->getResponseStatusCode();
     $content = $this->getResponse()->getContent();
     $message = ($message ? $message . PHP_EOL : '') . $method . ' ' . $url . ' with ' . $this->getCurrentUser() . ' was expected to be ' . $expectedCode . ' but was ' . $actualCode . ' and content is:' . PHP_EOL . $content . PHP_EOL;
     $this->assertEquals($expectedCode, $actualCode, $message);
     if ($expectedCode >= 200 && $expectedCode < 300) {
         return $this->getJsonResponse();
     }
 }