-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add testing infrastructure and workbench setup for package development
- Loading branch information
1 parent
e01bd78
commit 66956f8
Showing
15 changed files
with
421 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
name: Tests PHP Versions | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
include: | ||
- php: 8.1 | ||
laravel: 10.* | ||
- php: 8.2 | ||
laravel: 11.* | ||
- php: 8.3 | ||
laravel: 11.* | ||
|
||
name: P${{ matrix.php }} - L${{ matrix.laravel }} | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Setup PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: ${{ matrix.php }} | ||
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo | ||
coverage: none | ||
|
||
- name: Install dependencies | ||
run: | | ||
composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update | ||
composer update --prefer-dist --no-interaction --no-progress | ||
- name: Execute tests | ||
run: composer 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
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,28 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
> | ||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory suffix="Test.php">./tests/WithoutVersions</directory> | ||
<directory suffix="Test.php">./tests/WithVersions</directory> | ||
</testsuite> | ||
</testsuites> | ||
<source> | ||
<include> | ||
<directory suffix=".php">./src</directory> | ||
</include> | ||
</source> | ||
<php> | ||
<env name="APP_ENV" value="testing"/> | ||
<env name="APP_KEY" value="base64:2fl+Ktvkfl+Fuz4Qp/A75G2RTiWVA/ZoKZvp6fiiM10="/> | ||
<env name="CACHE_DRIVER" value="array"/> | ||
<env name="SESSION_DRIVER" value="array"/> | ||
<env name="QUEUE_DRIVER" value="sync"/> | ||
<env name="DB_CONNECTION" value="sqlite"/> | ||
<env name="DB_DATABASE" value=":memory:"/> | ||
</php> | ||
</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,4 @@ | ||
<?php | ||
|
||
uses(Tests\TestCaseWithoutVersions::class)->in('WithoutVersions'); | ||
uses(Tests\TestCaseWithVersions::class)->in('WithVersions'); |
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,34 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
use Dedoc\Scramble\ScrambleServiceProvider; | ||
use Waad\ScrambleSwagger\ScrambleSwaggerServiceProvider; | ||
use Workbench\App\Http\Controllers\TestController; | ||
use Workbench\App\Providers\WorkbenchServiceProvider; | ||
|
||
abstract class TestCaseWithVersions extends \Orchestra\Testbench\TestCase | ||
{ | ||
protected function getPackageProviders($app) | ||
{ | ||
return [ | ||
WorkbenchServiceProvider::class, | ||
ScrambleServiceProvider::class, | ||
ScrambleSwaggerServiceProvider::class, | ||
]; | ||
} | ||
|
||
protected function getEnvironmentSetUp($app) | ||
{ | ||
$app['config']->set('scramble-swagger.versions', ['all', 'v1', 'v2']); | ||
$app['config']->set('scramble-swagger.default_version', 'v2'); | ||
} | ||
|
||
public function defineRoutes($router): void | ||
{ | ||
$router->get('api/v1/test', [TestController::class, 'index']); | ||
$router->get('api/v1/test/test', [TestController::class, 'index']); | ||
$router->get('api/v2/test', [TestController::class, 'index']); | ||
$router->get('api/v2/test/test', [TestController::class, 'index']); | ||
} | ||
} |
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,37 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
use Dedoc\Scramble\ScrambleServiceProvider; | ||
use Waad\ScrambleSwagger\ScrambleSwaggerServiceProvider; | ||
use Workbench\App\Http\Controllers\TestController; | ||
use Workbench\App\Providers\WorkbenchServiceProvider; | ||
|
||
abstract class TestCaseWithoutVersions extends \Orchestra\Testbench\TestCase | ||
{ | ||
protected function getPackageProviders($app) | ||
{ | ||
return [ | ||
WorkbenchServiceProvider::class, | ||
ScrambleServiceProvider::class, | ||
ScrambleSwaggerServiceProvider::class, | ||
]; | ||
} | ||
|
||
protected function getEnvironmentSetUp($app) | ||
{ | ||
// $app['config']->set('scramble-swagger', [ | ||
// 'enable' => true, | ||
// 'url' => 'docs/swagger', | ||
// 'versions' => [ | ||
// 'all', | ||
// ], | ||
// 'default_version' => 'all', | ||
// ]); | ||
} | ||
|
||
public function defineRoutes($router): void | ||
{ | ||
$router->get('api/test', [TestController::class, 'index']); | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
|
||
use function Pest\Laravel\get; | ||
|
||
it('api documentation json is accessible', function () { | ||
$response = get('docs/swagger/json'); | ||
|
||
expect($response->status())->toBe(200); | ||
$json = $response->getContent(); | ||
expect($json)->toBeJson(); | ||
|
||
// main | ||
$array = json_decode($json, true); | ||
expect($array)->toHaveKey('openapi'); | ||
expect($array)->toHaveKey('info'); | ||
expect($array)->toHaveKey('servers'); | ||
expect($array)->toHaveKey('paths'); | ||
expect($array)->toHaveKey('components'); | ||
|
||
// sub | ||
expect($array)->toHaveKey('servers.0.url'); | ||
expect($array)->toHaveKey('info.title'); | ||
expect($array)->toHaveKey('info.version'); | ||
expect($array)->toHaveKey('paths./v1/test'); | ||
expect($array)->toHaveKey('paths./v1/test/test'); | ||
expect($array)->toHaveKey('paths./v2/test'); | ||
expect($array)->toHaveKey('paths./v2/test/test'); | ||
expect($array)->toHaveKey('components.responses'); | ||
expect($array)->toHaveKey('components.paths'); | ||
}); | ||
|
||
it('test controller endpoint returns correct data', function () { | ||
$response = get('api/v2/test?per_page=2&page=1'); | ||
|
||
$response->assertStatus(200) | ||
->assertJson([ | ||
['id' => 1, 'name' => 'John Doe'], | ||
['id' => 2, 'name' => 'Jane Ronaldo'], | ||
]); | ||
}); | ||
|
||
it('test controller search returns filtered results', function () { | ||
$response = get('api/v2/test?per_page=5&search=John'); | ||
|
||
$response->assertStatus(200) | ||
->assertJsonCount(2) | ||
->assertJsonFragment(['name' => 'John Doe']) | ||
->assertJsonFragment(['name' => 'John Smith']); | ||
}); | ||
|
||
it('test controller pagination works correctly', function () { | ||
$response = get('api/v2/test?per_page=3&page=2'); | ||
|
||
$response->assertStatus(200) | ||
->assertJsonCount(3) | ||
->assertJsonFragment(['id' => 4]) | ||
->assertJsonFragment(['id' => 5]) | ||
->assertJsonFragment(['id' => 6]); | ||
}); | ||
|
||
it('test controller validates required parameters', function () { | ||
$response = get('api/v2/test'); | ||
expect($response->status())->toBe(422); | ||
}); |
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,32 @@ | ||
<?php | ||
|
||
use function Pest\Laravel\get; | ||
|
||
it('swagger ui endpoint is accessible', function () { | ||
$response = get('docs/swagger'); | ||
expect($response->status())->toBe(200) | ||
->and($response->getContent())->toContain('<div id="scramble-swagger-ui"></div>'); | ||
}); | ||
|
||
it('swagger ui endpoint is accessible and contains all versions', function () { | ||
$response = get('docs/swagger'); | ||
expect($response->status())->toBe(200); | ||
|
||
expect($response->getContent()) | ||
->toContain('<div id="scramble-swagger-ui"></div>') | ||
->toContain("url: 'http://localhost/docs/swagger/json?version=all'") | ||
->toContain("url: 'http://localhost/docs/swagger/json?version=v1'") | ||
->toContain("url: 'http://localhost/docs/swagger/json?version=v2'"); | ||
}); | ||
|
||
it('swagger ui is disabled when config is false', function () { | ||
config(['scramble-swagger.enable' => false]); | ||
|
||
$response = get('docs/swagger'); | ||
expect($response->status())->toBe(404); | ||
}); | ||
|
||
it('swagger ui url can be configured', function () { | ||
$response = get('doc/doc'); | ||
expect($response->status())->toBe(404); | ||
}); |
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,61 @@ | ||
<?php | ||
|
||
use function Pest\Laravel\get; | ||
|
||
it('api documentation json is accessible', function () { | ||
$response = get('docs/swagger/json'); | ||
|
||
expect($response->status())->toBe(200); | ||
$json = $response->getContent(); | ||
expect($json)->toBeJson(); | ||
|
||
// main | ||
$array = json_decode($json, true); | ||
expect($array)->toHaveKey('openapi'); | ||
expect($array)->toHaveKey('info'); | ||
expect($array)->toHaveKey('servers'); | ||
expect($array)->toHaveKey('paths'); | ||
expect($array)->toHaveKey('components'); | ||
|
||
// sub | ||
expect($array)->toHaveKey('servers.0.url'); | ||
expect($array)->toHaveKey('info.title'); | ||
expect($array)->toHaveKey('info.version'); | ||
expect($array)->toHaveKey('paths./test'); | ||
expect($array)->toHaveKey('components.responses'); | ||
expect($array)->toHaveKey('components.paths'); | ||
}); | ||
|
||
it('test controller endpoint returns correct data', function () { | ||
$response = get('api/test?per_page=2&page=1'); | ||
|
||
$response->assertStatus(200) | ||
->assertJson([ | ||
['id' => 1, 'name' => 'John Doe'], | ||
['id' => 2, 'name' => 'Jane Ronaldo'], | ||
]); | ||
}); | ||
|
||
it('test controller search returns filtered results', function () { | ||
$response = get('api/test?per_page=5&search=John'); | ||
|
||
$response->assertStatus(200) | ||
->assertJsonCount(2) | ||
->assertJsonFragment(['name' => 'John Doe']) | ||
->assertJsonFragment(['name' => 'John Smith']); | ||
}); | ||
|
||
it('test controller pagination works correctly', function () { | ||
$response = get('api/test?per_page=3&page=2'); | ||
|
||
$response->assertStatus(200) | ||
->assertJsonCount(3) | ||
->assertJsonFragment(['id' => 4]) | ||
->assertJsonFragment(['id' => 5]) | ||
->assertJsonFragment(['id' => 6]); | ||
}); | ||
|
||
it('test controller validates required parameters', function () { | ||
$response = get('api/test'); | ||
expect($response->status())->toBe(422); | ||
}); |
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,23 @@ | ||
<?php | ||
|
||
use function Pest\Laravel\get; | ||
|
||
it('swagger ui endpoint is accessible', function () { | ||
$response = get('docs/swagger'); | ||
expect($response->status())->toBe(200) | ||
->and($response->getContent()) | ||
->toContain('<div id="scramble-swagger-ui"></div>') | ||
->toContain("url: 'http://localhost/docs/swagger/json?version=all'"); | ||
}); | ||
|
||
it('swagger ui is disabled when config is false', function () { | ||
config(['scramble-swagger.enable' => false]); | ||
|
||
$response = get('docs/swagger'); | ||
expect($response->status())->toBe(404); | ||
}); | ||
|
||
it('swagger ui url can be configured', function () { | ||
$response = get('doc/doc'); | ||
expect($response->status())->toBe(404); | ||
}); |
Oops, something went wrong.