Skip to content

Commit

Permalink
Update APITestCaseWriter.php
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Sep 1, 2024
1 parent 44a8800 commit 4054ea8
Showing 1 changed file with 58 additions and 5 deletions.
63 changes: 58 additions & 5 deletions webfiori/framework/writers/APITestCaseWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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();
Expand Down Expand Up @@ -89,14 +93,15 @@ public function getServicesManagerName() {
public function writeClassBody() {
$this->writeNotAllowedRequestMethodTestCases();
$this->writeRequiredParametersTestCases();
$this->writeTestCases();
$this->append('}');
}

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() {
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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++;
Expand All @@ -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');
}

}

0 comments on commit 4054ea8

Please sign in to comment.