Ejemplo n.º 1
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));
 }
Ejemplo n.º 2
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;
 }
Ejemplo n.º 3
0
 /**
  * Parse test information from a json file
  * this method will parse the file and prepare the  object
  * use createTest to complete creation of test
  * @param String $filename JSON file containing the test
  * @todo Set $validatorRequest header's subtype
  * @throws \Phramework\Exceptions\NotFoundException When file is not found.
  * @throws \Exception When file contains not valid JSON.
  * @throws \Phramework\Exceptions\MissingParametersException When required test properties are not set.
  * @throws \Phramework\Exceptions\IncorrectParametersException When test properties are not correct.
  */
 public function __construct($filename)
 {
     $this->filename = $filename;
     if (!file_exists($filename)) {
         throw new \Phramework\Exceptions\NotFoundException(sprintf('File "%s" doesn\'t exist', $filename));
     }
     $contents = file_get_contents($filename);
     //Check if contents are a valid JSON file
     if (!Util::isJSON($contents)) {
         throw new \Exception(sprintf('File "%s" isn\'t a valid JSON file', $filename));
     }
     //Decode test file
     $contentsObject = json_decode($contents);
     //Setup validator, to validate and parse the test file
     $validatorRequest = new ObjectValidator(['url' => new StringValidator(1, 2048), 'method' => (new StringValidator())->setDefault('GET'), 'iterators' => (new ObjectValidator([], [], true))->setDefault((object) []), 'headers' => (new ArrayValidator(0, null, new StringValidator()))->setDefault([]), 'body' => new OneOf([new ObjectValidator(), new StringValidator(), new ArrayValidator(0, null, new OneOf([new ObjectValidator(), new StringValidator()]))])], ['url'], false);
     $validatorResponse = new ObjectValidator(['statusCode' => new AnyOf([new UnsignedIntegerValidator(100, 999), new ArrayValidator(1, 10, new UnsignedIntegerValidator(100, 999))]), 'headers' => (new ObjectValidator())->setDefault((object) []), 'timeout' => (new UnsignedIntegerValidator())->setDefault(null), 'ruleObjects' => (new ArrayValidator())->setDefault([]), 'export' => (new ObjectValidator([], [], true))->setDefault((object) [])], ['statusCode'], false);
     //Setup validator for parsed test
     $validator = new ObjectValidator(['meta' => (new ObjectValidator(['order' => (new IntegerValidator(-99999999, 99999999))->setDefault(0), 'ignore' => (new BooleanValidator())->setDefault(false), 'description' => (new StringValidator())->setDefault(null), 'JSONbody' => (new BooleanValidator())->setDefault(true), 'incomplete' => (new OneOf([new BooleanValidator(), new StringValidator(1, 4096)]))->setDefault(false)]))->setDefault((object) ['order' => 0, 'description' => null, 'JSONbody' => true, 'incomplete' => false, 'ignore' => false]), 'request' => $validatorRequest, 'response' => $validatorResponse], ['request', 'response'], false);
     //Parse test file, using validator's rules
     $this->contentsParsed = $contentsParsed = $validator->parse($contentsObject);
     //Set testparser meta
     $this->meta = $contentsParsed->meta;
 }