Example #1
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;
 }