Exemplo n.º 1
0
 /**
  * Handle renspose, test response against provided rules
  * @param  int $responseStatusCode
  * @param  array $responseHeaders
  * @param  string $responseBody
  * @param  callable|null
  * @throws \Exception
  * @return boolean True on success
  */
 private function handle($responseStatusCode, $responseHeaders, $responseBody, $callback)
 {
     if (!in_array($responseStatusCode, $this->ruleStatusCode, true)) {
         throw new \Exception(sprintf('Expected status code "%s" got "%s"', implode(' or ', $this->ruleStatusCode), $responseStatusCode));
     }
     foreach ($this->ruleHeaders as $headerKey => $headerValue) {
         if (!isset($responseHeaders[$headerKey])) {
             throw new \Exception(sprintf('Expected header "%s" is not set', $headerKey));
         }
         if ($headerValue != $responseHeaders[$headerKey]) {
             throw new \Exception(sprintf('Expected header value "%s" for header "%s" got "%s"', $headerValue, $headerKey, $responseHeaders[$headerKey]));
         }
     }
     if ($this->ruleJSON && !Util::isJSON($responseBody)) {
         //Ignore isJSON body on "204 No Content" when it's empty
         if ($responseStatusCode != 204 || !empty($responseBody)) {
             throw new \Exception(sprintf('Expected valid JSON response Body'));
         }
     }
     //Add extra rules ??
     if ($this->ruleJSON) {
         $responseBodyObject = json_decode($responseBody);
         foreach ($this->ruleObjects as $ruleObject) {
             $ruleObject->parse($responseBodyObject);
         }
     }
     if ($callback) {
         if ($this->ruleJSON) {
             call_user_func($callback, $responseStatusCode, $responseHeaders, $responseBody, $responseBodyObject);
         } else {
             call_user_func($callback, $responseStatusCode, $responseHeaders, $responseBody);
         }
     }
     return true;
 }
Exemplo n.º 2
0
 /**
  * @return TestParser[]
  * @throws \Exception
  */
 protected function getTestParserCollection()
 {
     $dir = $this->arguments->dir;
     //Get all .json files in given directory
     $testFiles = array_map(function ($f) {
         return str_replace('//', '/', $f);
     }, Util::directoryToArray($dir, true, false, true, '/^\\.|\\.\\.$/', ['json'], false));
     /**
      * @var TestParser[]
      */
     $testParserCollection = [];
     foreach ($testFiles as $filename) {
         try {
             $testParser = new TestParser($filename);
         } catch (\Exception $e) {
             $message = sprintf('Failed to parse file "%s" %s With message: "%s"', $filename, PHP_EOL, $e->getMessage()) . PHP_EOL;
             if (get_class($e) == IncorrectParametersException::class) {
                 $message .= PHP_EOL . 'Incorrect:' . PHP_EOL . json_encode($e->getParameters(), JSON_PRETTY_PRINT) . PHP_EOL;
             } elseif (get_class($e) == MissingParametersException::class) {
                 $message .= PHP_EOL . 'Missing:' . PHP_EOL . json_encode($e->getParameters(), JSON_PRETTY_PRINT) . PHP_EOL;
             }
             throw new \Exception($message);
         }
         $testParserCollection[] = $testParser;
     }
     //Sort tests by order
     uasort($testParserCollection, [self::class, 'sortTestParser']);
     return $testParserCollection;
 }
Exemplo n.º 3
0
 /**
  * @covers Phramework\Testphase\Testphase::getResponseBody
  */
 public function testGetResponseBody()
 {
     $testphase = (new Testphase('posts/notFound', 'GET'))->expectStatusCode(404);
     $testphase->run();
     $responseBody = $testphase->getResponseBody();
     $this->assertInternalType('string', $responseBody);
     $this->assertTrue(Util::isJSON($responseBody));
 }
Exemplo n.º 4
0
 /**
  * @todo clean up
  */
 public function createTest()
 {
     $testphaseCollection = [];
     if (!empty((array) $this->contentsParsed->request->iterators)) {
         //searchAndReplace in request->iterators first
         $iterators = $this->searchAndReplace($this->contentsParsed->request->iterators);
         //replace back to contentsParsed
         $this->contentsParsed->request->iterators = $iterators;
         //Get combinations of iterator values
         $combinations = Util::cartesian((array) $iterators);
     } else {
         //Add a single test with no combinations
         $combinations = [[]];
     }
     foreach ($combinations as $combination) {
         //Set combination
         foreach ($combination as $combinationKey => $combinationValue) {
             Globals::set($combinationKey, $combinationValue);
         }
         //Recursive search whole object
         $contentsParsed = $this->searchAndReplace($this->contentsParsed);
         $requestBody = null;
         $requestBodies = [];
         if (isset($contentsParsed->request->body) && !empty($contentsParsed->request->body)) {
             if (!is_array($contentsParsed->request->body)) {
                 //Work with array
                 $contentsParsed->request->body = [$contentsParsed->request->body];
             }
             foreach ($contentsParsed->request->body as $body) {
                 //Push to bodies
                 $requestBodies[] = is_object($body) ? json_encode($body) : $body;
             }
         }
         //In case there is no request body, then at least one test must be created
         if (empty($requestBodies)) {
             $requestBodies[] = null;
         }
         foreach ($requestBodies as $requestBody) {
             //Create a Testphase object using parsed rule
             $testphase = (new Testphase($contentsParsed->request->url, $contentsParsed->request->method, $contentsParsed->request->headers, $requestBody))->expectStatusCode($contentsParsed->response->statusCode)->expectResponseHeader($contentsParsed->response->headers);
             $testphase->expectJSON(isset($contentsParsed->meta->JSONbody) ? $contentsParsed->meta->JSONbody : true);
             //Add rule objects to validate body
             foreach ($contentsParsed->response->ruleObjects as $key => $ruleObject) {
                 if (is_string($ruleObject)) {
                     $testphase->expectObject(ObjectValidator::createFromJSON($ruleObject));
                 } elseif (is_subclass_of($ruleObject, \Phramework\Validate\BaseValidator::class)) {
                     $testphase->expectObject($ruleObject);
                 } else {
                     $testphase->expectObject(ObjectValidator::createFromObject($ruleObject));
                 }
             }
             //push test
             $testphaseCollection[] = $testphase;
             //todo set only once
             $this->export = $contentsParsed->response->export;
         }
     }
     $this->testphaseCollection = $testphaseCollection;
 }