generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1160788
commit 0db6fe4
Showing
4 changed files
with
138 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ testbench.yaml | |
vendor | ||
node_modules | ||
*-test.json | ||
.phpunit.cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters