From 4054ea838a660750973e836384ae38bc65a0f769 Mon Sep 17 00:00:00 2001 From: Ibrahim Date: Sun, 1 Sep 2024 19:06:59 +0300 Subject: [PATCH] Update APITestCaseWriter.php --- .../framework/writers/APITestCaseWriter.php | 63 +++++++++++++++++-- 1 file changed, 58 insertions(+), 5 deletions(-) diff --git a/webfiori/framework/writers/APITestCaseWriter.php b/webfiori/framework/writers/APITestCaseWriter.php index b931eea1..834c6d6f 100644 --- a/webfiori/framework/writers/APITestCaseWriter.php +++ b/webfiori/framework/writers/APITestCaseWriter.php @@ -22,6 +22,10 @@ class APITestCaseWriter extends ClassWriter { private $serviceObjName; private $servicesManager; private $servicesManagerName; + private $phpunitV; + public function setPhpUnitVersion(int $num) { + $this->phpunitV = $num; + } /** * Creates new instance of the class. * @@ -30,7 +34,7 @@ public function __construct(WebServicesManager $m, $service = null) { parent::__construct('WebService', ROOT_PATH.'\\tests\\apis', ROOT_PATH.'tests\\apis'); $this->setSuffix('Test'); $this->setServicesManager($m); - + $this->setPhpUnitVersion(9); if (!($service instanceof AbstractWebService)) { if (class_exists($service)) { $s = new $service(); @@ -89,6 +93,7 @@ public function getServicesManagerName() { public function writeClassBody() { $this->writeNotAllowedRequestMethodTestCases(); $this->writeRequiredParametersTestCases(); + $this->writeTestCases(); $this->append('}'); } @@ -96,7 +101,7 @@ public function writeClassComment() { $this->append("/**\n" ." * A unit test class which is used to test the API '".$this->getService()->getName()."'.\n" ); - $this->append(" * \n */"); + $this->append(" */"); } public function writeClassDeclaration() { @@ -114,10 +119,11 @@ private function writeRequiredParametersTestCases() { } if (count($missingArr) !== 0) { $requestMethod = $this->getService()->getRequestMethods()[0]; + $this->addTestAnnotation(); $this->append('public function testRequiredParameters() {', 1); - $this->append('$output = $this->callEntpoint(new '.$this->getServicesManagerName().'(), RequestMethod::'. strtoupper($requestMethod).', '.$this->getServiceName().'::class, []);', 2); + $this->append('$output = $this->callEndpoint(new '.$this->getServicesManagerName().'(), RequestMethod::'. strtoupper($requestMethod).', '.$this->getServiceName().'::class, []);', 2); $this->append("\$this->assertEquals('{'.self::NL", 2); - $this->append(". ' \"message\":\"$responseMessage\'". implode("\',", $missingArr)."\','.self::NL", 2); + $this->append(". ' \"message\":\"$responseMessage\'". implode("\',", $missingArr)."\'.\",'.self::NL", 2); $this->append(". ' \"type\":\"error\",'.self::NL", 2); $this->append(". ' \"http_code\":404,'.self::NL", 2); $this->append(". ' \"more_info\":{'.self::NL", 2); @@ -136,12 +142,58 @@ private function writeRequiredParametersTestCases() { $this->append('}', 1); } } + public function writeTestCases() { + $methods = $this->getService()->getRequestMethods(); + $testCasesCount = 0; + + foreach (RequestMethod::getAll() as $method) { + if (in_array($method, $methods)) { + $this->addTestAnnotation(); + $this->append('public function test'.$method.'Request00() {', 1); + $this->append("//TODO: Write test case for $method request.", 2); + $methodName = $this->getMethName($method); + + if (count($this->getService()->getParameters()) == 0) { + if ($methodName == 'callEndpoint') { + $this->append('$output = $this->'.$methodName.'(new '.$this->getServicesManagerName().'(), RequestMethod::'. strtoupper($method).', '.$this->getServiceName().'::class, []);', 2); + } else { + $this->append('$output = $this->'.$methodName.'(new '.$this->getServicesManagerName().'(), '.$this->getServiceName().'::class, []);', 2); + } + } else { + if ($methodName == 'callEndpoint') { + $this->append('$output = $this->'.$methodName.'(new '.$this->getServicesManagerName().'(), RequestMethod::'. strtoupper($method).', '.$this->getServiceName().'::class, [', 2); + } else { + $this->append('$output = $this->'.$methodName.'(new '.$this->getServicesManagerName().'(), '.$this->getServiceName().'::class, [', 2); + } + foreach ($this->getService()->getParameters() as $reqParam) { + $this->append("'".$reqParam->getName()."' => null,", 3); + } + $this->append(']);', 2); + } + + $this->append("\$this->assertEquals('{'.self::NL", 2); + $this->append(". '}', \$output);", 2); + $this->append('}', 1); + $testCasesCount++; + } + } + } + private function addTestAnnotation() { + if ($this->phpunitV >= 10) { + $this->append('#[Test]', 1); + } else { + $this->append('/**', 1); + $this->append(' * @test', 1); + $this->append(' */', 1); + } + } private function writeNotAllowedRequestMethodTestCases() { $methods = $this->getService()->getRequestMethods(); $testCasesCount = 0; foreach (RequestMethod::getAll() as $method) { if (!in_array($method, $methods)) { + $this->addTestAnnotation(); $this->append('public function testRequestMethodNotAllowed'.($testCasesCount < 10 ? '0'.$testCasesCount : $testCasesCount).'() {', 1); $methodName = $this->getMethName($method); @@ -153,7 +205,7 @@ private function writeNotAllowedRequestMethodTestCases() { $this->append("\$this->assertEquals('{'.self::NL", 2); $this->append(". ' \"message\":\"Method Not Allowed.\",'.self::NL", 2); $this->append(". ' \"type\":\"error\",'.self::NL", 2); - $this->append(". ' \"http_code\":405,'.self::NL", 2); + $this->append(". ' \"http_code\":405'.self::NL", 2); $this->append(". '}', \$output);", 2); $this->append('}', 1); $testCasesCount++; @@ -178,6 +230,7 @@ private function addAllUse() { $this->addUseStatement(RequestMethod::class); $this->addUseStatement(get_class($this->getService())); $this->addUseStatement(get_class($this->getServicesManager())); + $this->addUseStatement('PHPUnit\Framework\Attributes\Test'); } }