Skip to content

Commit

Permalink
Enhance Symfony generator Tests
Browse files Browse the repository at this point in the history
  - Skip risky tests
  - Fix type hint error
  - Fix class exists tests
  - Fix broken doc block
  - Enhance annotations
  - Update phpunit.xml.dist
  - Fix test config resource location
  • Loading branch information
dmetzner committed Jun 25, 2022
1 parent 9d44dcf commit 4df930b
Show file tree
Hide file tree
Showing 19 changed files with 330 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
/**
* @inheritDoc
*/
public function registerBundles(): iterable
{
$bundles = array(
new FrameworkBundle()
);
return $bundles;
return [
new FrameworkBundle(),
];
}

/**
* @param LoaderInterface $loader
* @return mixed
* @inheritDoc
* @throws \Exception
*/
public function registerContainerConfiguration(LoaderInterface $loader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,16 @@ use Symfony\Component\HttpFoundation\Request;
*/
class ControllerTest extends TestCase
{
/**
* Tests isContentTypeAllowed static method.
*
* @param string $contentType
* @param array $consumes
* @param bool $expectedReturn
*
* @covers ::isContentTypeAllowed
* @dataProvider provideArgumentsForIsContentTypeAllowed
* @dataProvider dataProviderIsContentTypeAllowed
*/
public function testIsContentTypeAllowed(string $contentType, array $consumes, bool $expectedReturn): void
public function testIsContentTypeAllowed(?string $contentType, array $consumes, bool $expectedReturn): void
{
$request = new Request();
$request->headers->set('CONTENT_TYPE', $contentType, true);// last one argument overrides header
$request->headers->set('CONTENT_TYPE', $contentType);// last one argument overrides header
$this->assertSame(
$expectedReturn,
Controller::isContentTypeAllowed($request, $consumes),
Expand All @@ -60,7 +55,7 @@ class ControllerTest extends TestCase
);
}

public function provideArgumentsForIsContentTypeAllowed(): array
public function dataProviderIsContentTypeAllowed(): array
{
return [
'usual JSON content type' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
* @package {{apiTestsPackage}}
* @author openapi-generator contributors
* @link https://github.com/openapitools/openapi-generator
* @coversDefaultClass \{{apiPackage}}\{{classname}}
*/
{{#operations}}class {{classname}}Test extends WebTestCase
{
Expand Down Expand Up @@ -102,6 +103,7 @@ use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
{{/pathParams}}

$crawler = $client->request('{{httpMethod}}', $path{{#hasBodyParam}}, [], [], ['CONTENT_TYPE' => 'application/json']{{/hasBodyParam}});
$this->markTestSkipped('Test for {{operationId}} not implemented');
}
{{/operation}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@

namespace {{modelPackage}};

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* {{classname}}Test Class Doc Comment
*
* @category Class */
// * @description {{description}}{{^description}}{{classname}}{{/description}}
/**
* @category Class
* @description {{description}}{{^description}}{{classname}}{{/description}}
* @package {{modelTestsPackage}}
* @author openapi-generator contributors
* @link https://github.com/openapitools/openapi-generator
* @coversDefaultClass \{{modelPackage}}\{{classname}}
*/
class {{classname}}Test extends TestCase
{
protected {{classname}}|MockObject $object;

/**
* Setup before running any test case
Expand All @@ -48,6 +50,7 @@ class {{classname}}Test extends TestCase
*/
public function setUp(): void
{
$this->object = $this->getMockBuilder({{classname}}::class)->getMockForAbstractClass();
}

/**
Expand All @@ -65,19 +68,25 @@ class {{classname}}Test extends TestCase
}

/**
* Test "{{classname}}"
* @group integration
* @small
*/
public function test{{classname}}(): void
public function testTestClassExists(): void
{
$test{{classname}} = new {{classname}}();
$this->assertTrue(class_exists({{classname}}::class));
$this->assertInstanceOf({{classname}}::class, $this->object);
}
{{#vars}}

/**
* Test attribute "{{name}}"
*
* @group unit
* @small
*/
public function testProperty{{nameInCamelCase}}(): void
{
$this->markTestSkipped('Test for property {{name}} not implemented');
}
{{/vars}}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="./vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false">
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">{{apiSrcPath}}</directory>
<directory suffix=".php">{{modelSrcPath}}</directory>
<directory suffix=".php">{{controllerSrcPath}}</directory>
</include>
</coverage>
<testsuites>
<testsuite>
<testsuite name="Default test suite">
<directory>{{apiTestPath}}</directory>
<directory>{{modelTestPath}}</directory>
<directory>{{controllerTestPath}}</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">{{apiSrcPath}}</directory>
<directory suffix=".php">{{modelSrcPath}}</directory>
<directory suffix=".php">{{controllerSrcPath}}</directory>
</whitelist>
</filter>
<php>
<ini name="error_reporting" value="E_ALL" />
<server name="KERNEL_CLASS" value="{{testsPackage}}\AppKernel" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ framework:
secret: "testsecret"
test: ~
router:
resource: "%kernel.project_dir%/../Resources/config/routing.yml"
resource: "%kernel.project_dir%/Resources/config/routing.yml"
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@

namespace OpenAPI\Server\Tests\Api;

use OpenAPI\Server\Configuration;
use OpenAPI\Server\ApiClient;
use OpenAPI\Server\ApiException;
use OpenAPI\Server\ObjectSerializer;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

Expand All @@ -42,6 +38,7 @@
* @package OpenAPI\Server\Tests\Api
* @author openapi-generator contributors
* @link https://github.com/openapitools/openapi-generator
* @coversDefaultClass \OpenAPI\Server\Api\PetApiInterface
*/
class PetApiInterfaceTest extends WebTestCase
{
Expand Down Expand Up @@ -85,13 +82,14 @@ public static function tearDownAfterClass(): void
* Add a new pet to the store.
*
*/
public function testAddPet()
public function testAddPet(): void
{
$client = self::$client;

$path = '/pet';

$crawler = $client->request('POST', $path, [], [], ['CONTENT_TYPE' => 'application/json']);
$this->markTestSkipped('Test for addPet not implemented');
}

/**
Expand All @@ -100,7 +98,7 @@ public function testAddPet()
* Deletes a pet.
*
*/
public function testDeletePet()
public function testDeletePet(): void
{
$client = self::$client;

Expand All @@ -110,6 +108,7 @@ public function testDeletePet()
$path = str_replace($pattern, $data, $path);

$crawler = $client->request('DELETE', $path);
$this->markTestSkipped('Test for deletePet not implemented');
}

/**
Expand All @@ -118,13 +117,14 @@ public function testDeletePet()
* Finds Pets by status.
*
*/
public function testFindPetsByStatus()
public function testFindPetsByStatus(): void
{
$client = self::$client;

$path = '/pet/findByStatus';

$crawler = $client->request('GET', $path);
$this->markTestSkipped('Test for findPetsByStatus not implemented');
}

/**
Expand All @@ -133,13 +133,14 @@ public function testFindPetsByStatus()
* Finds Pets by tags.
*
*/
public function testFindPetsByTags()
public function testFindPetsByTags(): void
{
$client = self::$client;

$path = '/pet/findByTags';

$crawler = $client->request('GET', $path);
$this->markTestSkipped('Test for findPetsByTags not implemented');
}

/**
Expand All @@ -148,7 +149,7 @@ public function testFindPetsByTags()
* Find pet by ID.
*
*/
public function testGetPetById()
public function testGetPetById(): void
{
$client = self::$client;

Expand All @@ -158,6 +159,7 @@ public function testGetPetById()
$path = str_replace($pattern, $data, $path);

$crawler = $client->request('GET', $path);
$this->markTestSkipped('Test for getPetById not implemented');
}

/**
Expand All @@ -166,13 +168,14 @@ public function testGetPetById()
* Update an existing pet.
*
*/
public function testUpdatePet()
public function testUpdatePet(): void
{
$client = self::$client;

$path = '/pet';

$crawler = $client->request('PUT', $path, [], [], ['CONTENT_TYPE' => 'application/json']);
$this->markTestSkipped('Test for updatePet not implemented');
}

/**
Expand All @@ -181,7 +184,7 @@ public function testUpdatePet()
* Updates a pet in the store with form data.
*
*/
public function testUpdatePetWithForm()
public function testUpdatePetWithForm(): void
{
$client = self::$client;

Expand All @@ -191,6 +194,7 @@ public function testUpdatePetWithForm()
$path = str_replace($pattern, $data, $path);

$crawler = $client->request('POST', $path);
$this->markTestSkipped('Test for updatePetWithForm not implemented');
}

/**
Expand All @@ -199,7 +203,7 @@ public function testUpdatePetWithForm()
* uploads an image.
*
*/
public function testUploadFile()
public function testUploadFile(): void
{
$client = self::$client;

Expand All @@ -209,13 +213,18 @@ public function testUploadFile()
$path = str_replace($pattern, $data, $path);

$crawler = $client->request('POST', $path);
$this->markTestSkipped('Test for uploadFile not implemented');
}

protected function genTestData($regexp)
/**
* @param string $regexp
* @return mixed
*/
protected function genTestData(string $regexp)
{
$grammar = new \Hoa\File\Read('hoa://Library/Regex/Grammar.pp');
$grammar = new \Hoa\File\Read('hoa://Library/Regex/Grammar.pp');
$compiler = \Hoa\Compiler\Llk\Llk::load($grammar);
$ast = $compiler->parse($regexp);
$ast = $compiler->parse($regexp);
$generator = new \Hoa\Regex\Visitor\Isotropic(new \Hoa\Math\Sampler\Random());

return $generator->visit($ast);
Expand Down
Loading

0 comments on commit 4df930b

Please sign in to comment.