Skip to content

Commit bfbc628

Browse files
authored
Migrate from unpkg to jsDelivr (#50)
1 parent ebbebfd commit bfbc628

File tree

9 files changed

+199
-31
lines changed

9 files changed

+199
-31
lines changed

.github/workflows/format.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ jobs:
3333
with:
3434
ref: ${{ github.head_ref }}
3535

36-
- uses: creyD/prettier_action@v2.2
36+
- uses: creyD/prettier_action@v4.3
3737
with:
3838
prettier_options: --write --tab-width=2 *.md
39-
branch: ${{ github.head_ref }}
40-
commit_message: Prettify
39+
commit_message: Apply Prettier changes
4140
env:
4241
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4342

.github/workflows/validate.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,64 @@ jobs:
6767
${{ matrix.composer.arg }}
6868
6969
- run: vendor/bin/phpstan analyse --configuration=phpstan.neon
70+
71+
tests:
72+
name: PHPUnit on PHP ${{ matrix.php-version }} with Laravel ${{ matrix.laravel-version }} and ${{ matrix.composer.name }} dependencies
73+
runs-on: ubuntu-latest
74+
75+
strategy:
76+
fail-fast: false
77+
matrix:
78+
php-version:
79+
- "8.0"
80+
- "8.1"
81+
- "8.2"
82+
- "8.3"
83+
- "8.4"
84+
laravel-version:
85+
- "^9"
86+
- "^10"
87+
- "^11"
88+
- "^12"
89+
composer:
90+
- name: lowest
91+
arg: "--prefer-lowest --prefer-stable"
92+
- name: highest
93+
arg: "" # No args added as highest is default
94+
exclude:
95+
- php-version: "8.0"
96+
laravel-version: "^10"
97+
- php-version: "8.0"
98+
laravel-version: "^11"
99+
- php-version: "8.0"
100+
laravel-version: "^12"
101+
- php-version: "8.1"
102+
laravel-version: "^11"
103+
- php-version: "8.1"
104+
laravel-version: "^12"
105+
- php-version: "8.3"
106+
laravel-version: "^9"
107+
- php-version: "8.4"
108+
laravel-version: "^9"
109+
- php-version: "8.4"
110+
laravel-version: "^10"
111+
112+
steps:
113+
- uses: actions/checkout@v4
114+
115+
- uses: shivammathur/setup-php@v2
116+
with:
117+
coverage: none
118+
extensions: ${{ env.REQUIRED_PHP_EXTENSIONS }}
119+
php-version: ${{ matrix.php-version }}
120+
121+
- run: >
122+
composer require
123+
illuminate/contracts:${{ matrix.laravel-version }}
124+
--no-interaction
125+
--prefer-dist
126+
--no-progress
127+
--update-with-all-dependencies
128+
${{ matrix.composer.arg }}
129+
130+
- run: vendor/bin/phpunit

.gitignore

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Generated
2-
vendor
3-
composer.lock
4-
.php-cs-fixer.cache
2+
/vendor
3+
/composer.lock
4+
/.php-cs-fixer.cache
5+
/.phpunit.result.cache
56

67
# IDE
7-
.vscode/
8-
.idea/
8+
/.vscode
9+
/.idea

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.PHONY: it
2-
it: fix stan ## Perform all quality checks
2+
it: fix stan test ## Perform all quality checks
33

44
.PHONY: help
55
help: ## Displays this list of targets with descriptions
@@ -14,8 +14,12 @@ fix: ## Fix the codestyle
1414
vendor/bin/php-cs-fixer fix --allow-risky=yes
1515

1616
.PHONY: stan
17-
stan: ## Run static analysis
17+
stan: ## Run static analysis with PHPStan
1818
vendor/bin/phpstan analyse --configuration=phpstan.neon
1919

20+
.PHONY: test
21+
test: ## Run tests with PHPUnit
22+
vendor/bin/phpunit
23+
2024
vendor: composer.json ## Install dependencies through composer
2125
composer update

composer.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
"require-dev": {
2323
"ergebnis/composer-normalize": "^2.45",
2424
"larastan/larastan": "^2.9.14 || ^3.1",
25+
"laravel/framework": "^9 || ^10 || ^11 || ^12",
2526
"mll-lab/php-cs-fixer-config": "^5.10",
2627
"orchestra/testbench": "^7.52 || ^8.33 || ^9.11 || ^10",
2728
"phpstan/extension-installer": "^1.4.3",
28-
"phpstan/phpstan": "^1.12.20 || ^2.1.7"
29+
"phpstan/phpstan": "^1.12.20 || ^2.1.7",
30+
"phpstan/phpstan-phpunit": "^1.4.2 || ^2.0.6",
31+
"phpunit/phpunit": "^9.6.22 || ^10.5.45 || ^11.5.15 || ^12.0.10"
2932
},
3033
"minimum-stability": "dev",
3134
"prefer-stable": true,
@@ -34,6 +37,11 @@
3437
"MLL\\GraphiQL\\": "src/"
3538
}
3639
},
40+
"autoload-dev": {
41+
"psr-4": {
42+
"MLL\\GraphiQL\\Tests\\": "tests/"
43+
}
44+
},
3745
"config": {
3846
"allow-plugins": {
3947
"ergebnis/composer-normalize": true,

phpunit.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true">
6+
<source>
7+
<include>
8+
<directory>src</directory>
9+
</include>
10+
</source>
11+
<testsuites>
12+
<testsuite name="Tests">
13+
<directory>tests</directory>
14+
</testsuite>
15+
</testsuites>
16+
</phpunit>

src/DownloadAssetsCommand.php

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@
1111
class DownloadAssetsCommand extends Command
1212
{
1313
public const REACT_PATH_LOCAL = 'vendor/graphiql/react.production.min.js';
14-
public const REACT_PATH_CDN = '//unpkg.com/react@17/umd/react.production.min.js';
14+
public const REACT_PATH_CDN = '//cdn.jsdelivr.net/npm/react@17/umd/react.production.min.js';
1515

1616
public const REACT_DOM_PATH_LOCAL = 'vendor/graphiql/react-dom.production.min.js';
17-
public const REACT_DOM_PATH_CDN = '//unpkg.com/react-dom@17/umd/react-dom.production.min.js';
17+
public const REACT_DOM_PATH_CDN = '//cdn.jsdelivr.net/npm/react-dom@17/umd/react-dom.production.min.js';
1818

1919
public const JS_PATH_LOCAL = 'vendor/graphiql/graphiql.min.js';
20-
public const JS_PATH_CDN = '//unpkg.com/graphiql/graphiql.min.js';
20+
public const JS_PATH_CDN = '//cdn.jsdelivr.net/npm/graphiql/graphiql.min.js';
2121

2222
public const PLUGIN_EXPLORER_PATH_LOCAL = 'vendor/graphiql/graphiql-plugin-explorer.umd.js';
2323
/** Pinned because the latest version broke, see https://github.com/mll-lab/laravel-graphiql/issues/25. */
24-
public const PLUGIN_EXPLORER_PATH_CDN = '//unpkg.com/@graphiql/[email protected]/dist/index.umd.js';
24+
public const PLUGIN_EXPLORER_PATH_CDN = '//cdn.jsdelivr.net/npm/@graphiql/[email protected]/dist/index.umd.js';
2525

2626
public const CSS_PATH_LOCAL = 'vendor/graphiql/graphiql.min.css';
27-
public const CSS_PATH_CDN = '//unpkg.com/graphiql/graphiql.min.css';
27+
public const CSS_PATH_CDN = '//cdn.jsdelivr.net/npm/graphiql/graphiql.min.css';
2828

2929
public const FAVICON_PATH_LOCAL = 'vendor/graphiql/favicon.ico';
3030
public const FAVICON_PATH_CDN = '//raw.githubusercontent.com/graphql/graphql.github.io/source/public/favicon.ico';
@@ -64,53 +64,58 @@ protected function downloadFileFromCDN(string $localPath, string $cdnPath): void
6464

6565
public static function reactPath(): string
6666
{
67-
return self::assetPath(self::REACT_PATH_LOCAL, self::REACT_PATH_CDN);
67+
return self::availablePath(self::REACT_PATH_LOCAL, self::REACT_PATH_CDN);
6868
}
6969

7070
public static function reactDOMPath(): string
7171
{
72-
return self::assetPath(self::REACT_DOM_PATH_LOCAL, self::REACT_DOM_PATH_CDN);
72+
return self::availablePath(self::REACT_DOM_PATH_LOCAL, self::REACT_DOM_PATH_CDN);
7373
}
7474

7575
public static function jsPath(): string
7676
{
77-
return self::assetPath(self::JS_PATH_LOCAL, self::JS_PATH_CDN);
77+
return self::availablePath(self::JS_PATH_LOCAL, self::JS_PATH_CDN);
7878
}
7979

8080
public static function pluginExplorerPath(): string
8181
{
82-
return self::assetPath(self::PLUGIN_EXPLORER_PATH_LOCAL, self::PLUGIN_EXPLORER_PATH_CDN);
82+
return self::availablePath(self::PLUGIN_EXPLORER_PATH_LOCAL, self::PLUGIN_EXPLORER_PATH_CDN);
8383
}
8484

8585
public static function cssPath(): string
8686
{
87-
return self::assetPath(self::CSS_PATH_LOCAL, self::CSS_PATH_CDN);
87+
return self::availablePath(self::CSS_PATH_LOCAL, self::CSS_PATH_CDN);
8888
}
8989

9090
public static function faviconPath(): string
9191
{
92-
return self::assetPath(self::FAVICON_PATH_LOCAL, self::FAVICON_PATH_CDN);
92+
return self::availablePath(self::FAVICON_PATH_LOCAL, self::FAVICON_PATH_CDN);
9393
}
9494

95-
protected static function assetPath(string $local, string $cdn): string
95+
public static function availablePath(string $local, string $cdn): string
9696
{
9797
return file_exists(self::publicPath($local))
98-
? self::asset($local)
99-
: $cdn;
98+
? self::localAssetURL($local)
99+
: self::cdnURL($cdn);
100100
}
101101

102-
protected static function asset(string $path): string
102+
public static function publicPath(string $path): string
103+
{
104+
$container = Container::getInstance();
105+
assert($container instanceof LaravelApplication || $container instanceof LumenApplication);
106+
107+
return $container->basePath("public/{$path}");
108+
}
109+
110+
public static function localAssetURL(string $path): string
103111
{
104112
$url = Container::getInstance()->make(UrlGenerator::class);
105113

106114
return $url->asset($path);
107115
}
108116

109-
protected static function publicPath(string $path): string
117+
public static function cdnURL(string $path): string
110118
{
111-
$container = Container::getInstance();
112-
assert($container instanceof LaravelApplication || $container instanceof LumenApplication);
113-
114-
return $container->basePath("public/{$path}");
119+
return str_replace('//', '/', $path);
115120
}
116121
}

tests/DownloadAssetsCommandTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace MLL\GraphiQL\Tests;
4+
5+
use MLL\GraphiQL\DownloadAssetsCommand;
6+
7+
final class DownloadAssetsCommandTest extends TestCase
8+
{
9+
protected function setUp(): void
10+
{
11+
parent::setUp();
12+
13+
foreach (self::paths() as [$localPath, $cdnPath]) {
14+
$publicPath = DownloadAssetsCommand::publicPath($localPath);
15+
if (file_exists($publicPath)) {
16+
unlink($publicPath);
17+
}
18+
}
19+
}
20+
21+
public function testSuccessfulDownload(): void
22+
{
23+
foreach (self::paths() as [$localPath, $cdnPath]) {
24+
$this->assertFileDoesNotExist(DownloadAssetsCommand::publicPath($localPath));
25+
$this->assertSame(DownloadAssetsCommand::cdnURL($cdnPath), DownloadAssetsCommand::availablePath($localPath, $cdnPath));
26+
}
27+
28+
$this->artisan('graphiql:download-assets')
29+
->assertOk();
30+
31+
foreach (self::paths() as [$localPath, $cdnPath]) {
32+
$this->assertFileExists(DownloadAssetsCommand::publicPath($localPath));
33+
$this->assertSame(DownloadAssetsCommand::localAssetURL($localPath), DownloadAssetsCommand::availablePath($localPath, $cdnPath));
34+
}
35+
}
36+
37+
/** @return iterable<array{string, string}> */
38+
private static function paths(): iterable
39+
{
40+
yield [DownloadAssetsCommand::REACT_PATH_LOCAL, DownloadAssetsCommand::REACT_PATH_CDN];
41+
yield [DownloadAssetsCommand::REACT_DOM_PATH_LOCAL, DownloadAssetsCommand::REACT_DOM_PATH_CDN];
42+
yield [DownloadAssetsCommand::JS_PATH_LOCAL, DownloadAssetsCommand::JS_PATH_CDN];
43+
yield [DownloadAssetsCommand::PLUGIN_EXPLORER_PATH_LOCAL, DownloadAssetsCommand::PLUGIN_EXPLORER_PATH_CDN];
44+
yield [DownloadAssetsCommand::CSS_PATH_LOCAL, DownloadAssetsCommand::CSS_PATH_CDN];
45+
yield [DownloadAssetsCommand::FAVICON_PATH_LOCAL, DownloadAssetsCommand::FAVICON_PATH_CDN];
46+
}
47+
}

tests/TestCase.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace MLL\GraphiQL\Tests;
4+
5+
use Illuminate\Foundation\Application;
6+
use Illuminate\Support\ServiceProvider;
7+
use MLL\GraphiQL\GraphiQLServiceProvider;
8+
use Orchestra\Testbench\TestCase as BaseTestCase;
9+
10+
abstract class TestCase extends BaseTestCase
11+
{
12+
/**
13+
* Set when not in setUp.
14+
*
15+
* @var Application
16+
*/
17+
// @phpstan-ignore-next-line
18+
protected $app;
19+
20+
/** @return array<int, class-string<ServiceProvider>> */
21+
protected function getPackageProviders($app): array
22+
{
23+
return [
24+
GraphiQLServiceProvider::class,
25+
];
26+
}
27+
}

0 commit comments

Comments
 (0)