Skip to content

Commit

Permalink
Add testing infrastructure and workbench setup for package development
Browse files Browse the repository at this point in the history
  • Loading branch information
waadmawlood committed Jan 31, 2025
1 parent e01bd78 commit 66956f8
Show file tree
Hide file tree
Showing 15 changed files with 421 additions and 1 deletion.
42 changes: 42 additions & 0 deletions .github/workflows/tests.yml
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
20 changes: 19 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
"Waad\\ScrambleSwagger\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/",
"Workbench\\App\\": "workbench/app/"
}
},
"extra": {
"laravel": {
"providers": [
Expand All @@ -42,6 +48,18 @@
"prefer-stable": true,
"scripts": {
"lint": "vendor/bin/pint",
"test": "vendor/bin/pest"
"test": "vendor/bin/pest",
"post-autoload-dump": [
"@clear",
"@prepare"
],
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"build": "@php vendor/bin/testbench workbench:build --ansi",
"serve": [
"Composer\\Config::disableProcessTimeout",
"@build",
"@php vendor/bin/testbench serve --ansi"
]
}
}
28 changes: 28 additions & 0 deletions phpunit.xml
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>
4 changes: 4 additions & 0 deletions tests/Pest.php
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');
34 changes: 34 additions & 0 deletions tests/TestCaseWithVersions.php
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']);
}
}
37 changes: 37 additions & 0 deletions tests/TestCaseWithoutVersions.php
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']);
}
}
64 changes: 64 additions & 0 deletions tests/WithVersions/ApiDocumentationTest.php
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);
});
32 changes: 32 additions & 0 deletions tests/WithVersions/SwaggerTest.php
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);
});
61 changes: 61 additions & 0 deletions tests/WithoutVersions/ApiDocumentationTest.php
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);
});
23 changes: 23 additions & 0 deletions tests/WithoutVersions/SwaggerTest.php
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);
});
Loading

0 comments on commit 66956f8

Please sign in to comment.