Skip to content

Commit

Permalink
added tests for routes-related code
Browse files Browse the repository at this point in the history
  • Loading branch information
romalytvynenko committed May 17, 2024
1 parent 1160788 commit 0db6fe4
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ testbench.yaml
vendor
node_modules
*-test.json
.phpunit.cache/
49 changes: 20 additions & 29 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
executionOrder="random"
failOnWarning="true"
failOnRisky="true"
failOnEmptyTestSuite="true"
>
<testsuites>
<testsuite name="Dedoc Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" executionOrder="random" failOnWarning="true" failOnRisky="true" failOnEmptyTestSuite="true" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Dedoc Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
117 changes: 117 additions & 0 deletions tests/ScrambleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Dedoc\Scramble\Tests;

use Dedoc\Scramble\Generator;
use Dedoc\Scramble\Scramble;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Route;
use Orchestra\Testbench\Attributes\DefineEnvironment;
use Orchestra\Testbench\Attributes\DefineRoute;

/**
* This is not a Pest test, as `DefineEnvironment` attribute cannot be used in Pest.
*/
class ScrambleTest extends TestCase
{
#[DefineEnvironment('withClosureAllRouteResolver')]
/** @test */
public function caches_routes_when_closure_resolver_set()
{
$this->assertCount(2, $routes = Route::getRoutes()->getRoutes());

$this->assertRoutesAreCacheable($routes);
}

/** @test */
public function caches_default_routes()
{
$this->assertCount(2, $routes = Route::getRoutes()->getRoutes());

$this->assertRoutesAreCacheable($routes);
}

/** @test */
public function registers_default_api()
{
expect(Scramble::getGeneratorConfig('default'))->toBeTruthy();
}

/** @test */
public function registers_routes_for_default_api()
{
expect($routes = Route::getRoutes()->getRoutes())
->toHaveCount(2)
->and($routes[0]->uri)->toBe('docs/api')
->and($routes[0]->methods)->toBe(['GET', 'HEAD'])
->and($routes[1]->uri)->toBe('docs/api.json')
->and($routes[1]->methods)->toBe(['GET', 'HEAD']);
}

/** @test */
#[DefineRoute('registerTestConsumerRoutes')]
public function filters_consumer_routes_with_config_file()
{
$generator = app(Generator::class);

$doc = $generator(Scramble::getGeneratorConfig('default'));

$this->assertEquals('http://localhost/api', $doc['servers'][0]['url']);
$this->assertEquals(['/a', '/b', '/c'], array_keys($doc['paths']));
}

/** @test */
#[DefineRoute('registerTestConsumerRoutes')]
#[DefineEnvironment('withEmptyConfigApiPath')]
#[DefineEnvironment('withClosureAllRouteResolver')]
public function filters_consumer_routes_with_redefined_resolver_and_api_path_config_file()
{
$generator = app(Generator::class);

$doc = $generator(Scramble::getGeneratorConfig('default'));

$this->assertEquals('http://localhost', $doc['servers'][0]['url']);
$this->assertEquals(['/api/a', '/api/b', '/api/c', '/second-api/a', '/second-api/b', '/second-api/c'], array_keys($doc['paths']));
}

protected function withClosureAllRouteResolver()
{
Scramble::routes(fn () => true);
}

protected function withEmptyConfigApiPath($app)
{
$app['config']->set('scramble.api_path', '');
}

protected function registerTestConsumerRoutes(Router $router)
{
$router->group(['prefix' => 'api'], function (Router $router) {
$router->get('a', [ScrambleTest_Controller::class, 'test']);
$router->get('b', [ScrambleTest_Controller::class, 'test']);
$router->get('c', [ScrambleTest_Controller::class, 'test']);
});

$router->group(['prefix' => 'second-api'], function (Router $router) {
$router->get('a', [ScrambleTest_Controller::class, 'test']);
$router->get('b', [ScrambleTest_Controller::class, 'test']);
$router->get('c', [ScrambleTest_Controller::class, 'test']);
});
}

private function assertRoutesAreCacheable($routes)
{
foreach ($routes as $route) {
$route->prepareForSerialization();
}

unserialize(serialize($routes));

expect(true)->toBeTrue();
}
}

class ScrambleTest_Controller
{
public function test() {}
}
3 changes: 0 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ protected function getPackageProviders($app)
public function getEnvironmentSetUp($app)
{
config()->set('database.default', 'testing');

// $migration = include __DIR__.'/migrations/create_documentor_table.php.stub';
// $migration->up();
}

protected function defineDatabaseMigrations()
Expand Down

0 comments on commit 0db6fe4

Please sign in to comment.