diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..b26a9a99 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +; This file is for unifying the coding style for different editors and IDEs. +; More information at https://editorconfig.org + +root = true + +[*] +charset = utf-8 +indent_size = 4 +indent_style = space +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..0d3c50ea --- /dev/null +++ b/.gitattributes @@ -0,0 +1,11 @@ +# Path-based git attributes +# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html + +# Ignore all test and documentation with "export-ignore". +/.editorconfig export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/.styleci.yml export-ignore +/.travis.yml export-ignore +/phpunit.xml export-ignore +/tests export-ignore diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 00000000..266f3212 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,41 @@ +name: tests + +on: + push: + pull_request: + schedule: + - cron: '0 0 * * *' + +jobs: + tests: + runs-on: ubuntu-latest + strategy: + fail-fast: true + matrix: + php: + - 7.2 + - 7.3 + - 7.4 + composerFlags: + - '--prefer-lowest' + - '' + + name: PHP ${{ matrix.php }} w/ Composer ${{ matrix.composerFlags }} + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: dom, curl, libxml, mbstring, zip + tools: composer:v2 + coverage: none + + - name: Install dependencies + run: | + composer update --prefer-dist --no-interaction --no-progress ${{ matrix.composerFlags }} + + - name: Execute tests + run: vendor/bin/phpunit --verbose diff --git a/.gitignore b/.gitignore index c4886e2e..b61f8d61 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ /vendor .DS_Store +.php_cs +.php_cs.cache .phpunit.result.cache composer.lock diff --git a/.styleci.yml b/.styleci.yml new file mode 100644 index 00000000..0285f179 --- /dev/null +++ b/.styleci.yml @@ -0,0 +1 @@ +preset: laravel diff --git a/composer.json b/composer.json index 745c729b..b77ad458 100644 --- a/composer.json +++ b/composer.json @@ -25,10 +25,11 @@ } }, "require": { - "laravel/framework": "^5.4|^6.0|^7.0|^8.0" + "laravel/framework": "^6.10|^7.0|^8.0" }, "require-dev": { - "orchestra/testbench": "~3.0" + "ext-json": "*", + "orchestra/testbench": "^4.0|^5.0|^6.0" }, "extra": { "laravel": { diff --git a/helpers.php b/helpers.php index cc07de2e..0a72145b 100644 --- a/helpers.php +++ b/helpers.php @@ -1,6 +1,6 @@ - + - - tests + + tests - - src + + src/ diff --git a/src/Middleware.php b/src/Middleware.php index b7b28278..fa7e9f06 100644 --- a/src/Middleware.php +++ b/src/Middleware.php @@ -12,7 +12,7 @@ public function handle($request, Closure $next) { $response = $next($request); - if (!$request->header('X-Inertia')) { + if (! $request->header('X-Inertia')) { return $response; } diff --git a/src/Response.php b/src/Response.php index 5623603d..981bee8c 100644 --- a/src/Response.php +++ b/src/Response.php @@ -3,11 +3,11 @@ namespace Inertia; use Closure; -use Illuminate\Support\Arr; -use Illuminate\Http\JsonResponse; -use Illuminate\Support\Facades\App; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Responsable; +use Illuminate\Http\JsonResponse; +use Illuminate\Support\Arr; +use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Response as ResponseFactory; class Response implements Responsable diff --git a/src/ResponseFactory.php b/src/ResponseFactory.php index 1ccb42eb..10091b66 100644 --- a/src/ResponseFactory.php +++ b/src/ResponseFactory.php @@ -3,10 +3,10 @@ namespace Inertia; use Closure; +use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Arr; use Illuminate\Support\Facades\App; use Illuminate\Support\Traits\Macroable; -use Illuminate\Contracts\Support\Arrayable; class ResponseFactory { diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 20c3e167..9a093955 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -2,10 +2,10 @@ namespace Inertia; +use Illuminate\Contracts\Http\Kernel; use Illuminate\Http\Request; use Illuminate\Routing\Router; use Illuminate\Support\Collection; -use Illuminate\Contracts\Http\Kernel; use Illuminate\Support\Facades\Blade; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Session; diff --git a/tests/ControllerTest.php b/tests/ControllerTest.php index d312fa2b..16fa6134 100644 --- a/tests/ControllerTest.php +++ b/tests/ControllerTest.php @@ -2,10 +2,10 @@ namespace Inertia\Tests; -use Inertia\Response; -use Inertia\Controller; use Illuminate\Http\Request; use Illuminate\Routing\Route; +use Inertia\Controller; +use Inertia\Response; class ControllerTest extends TestCase { diff --git a/tests/MiddlewareTest.php b/tests/MiddlewareTest.php index 9488399d..e94d81be 100644 --- a/tests/MiddlewareTest.php +++ b/tests/MiddlewareTest.php @@ -2,8 +2,9 @@ namespace Inertia\Tests; -use Illuminate\Foundation\Testing\TestResponse; +use Illuminate\Foundation\Testing\TestResponse as LegacyTestResponse; use Illuminate\Http\Request; +use Illuminate\Testing\TestResponse; use Inertia\Inertia; use Inertia\Middleware; @@ -85,6 +86,11 @@ private function makeMockResponse($request) return Inertia::render('User/Edit', ['user' => ['name' => 'Jonathan']])->toResponse($request); }); + // Laravel <= 6.0 + if (class_exists(LegacyTestResponse::class)) { + return LegacyTestResponse::fromBaseResponse($response); + } + return TestResponse::fromBaseResponse($response); } } diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index c4873516..88bffc24 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -2,17 +2,18 @@ namespace Inertia\Tests; +use Arr; use Illuminate\Contracts\Support\Arrayable; -use Inertia\Response; -use Illuminate\View\View; -use Illuminate\Http\Request; -use Illuminate\Support\Fluent; use Illuminate\Http\JsonResponse; -use Illuminate\Support\Collection; -use Illuminate\Http\Response as BaseResponse; -use Illuminate\Pagination\LengthAwarePaginator; +use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\ResourceCollection; +use Illuminate\Http\Response as BaseResponse; +use Illuminate\Pagination\LengthAwarePaginator; +use Illuminate\Support\Collection; +use Illuminate\Support\Fluent; +use Illuminate\View\View; +use Inertia\Response; class ResponseTest extends TestCase { @@ -103,7 +104,7 @@ public function test_lazy_resource_response() $page = $response->getData(); $expected = [ - 'data' => $users->take(2), + 'data' => $users->take(2)->toArray(), 'links' => [ 'first' => '/?page=1', 'last' => '/?page=2', @@ -123,9 +124,20 @@ public function test_lazy_resource_response() $this->assertInstanceOf(JsonResponse::class, $response); $this->assertSame('User/Index', $page->component); - $this->assertSame(json_encode($expected), json_encode($page->props->users)); $this->assertSame('/users?page=1', $page->url); $this->assertSame('123', $page->version); + + tap(json_decode(json_encode($page->props->users), true), function ($actual) use ($expected) { + // On Laravel 8+, new 'links' were added to the 'meta' section for convenience. + // We'll just inject these to our expected array to prevent testing issues + // on earlier version of Laravel. + if (Arr::has($actual, 'meta.links')) { + $expected['meta']['links'] = $actual['meta']['links']; + ksort($expected['meta']); + } + + $this->assertSame($expected, $actual); + }); } public function test_arrayable_prop_response() @@ -136,7 +148,6 @@ public function test_arrayable_prop_response() $user = (object) ['name' => 'Jonathan']; $resource = new class($user) implements Arrayable { - public $user; public function __construct($user) diff --git a/tests/ServiceProviderTest.php b/tests/ServiceProviderTest.php index d5120fe3..2940ba65 100644 --- a/tests/ServiceProviderTest.php +++ b/tests/ServiceProviderTest.php @@ -3,17 +3,16 @@ namespace Inertia\Tests; use Closure; -use Inertia\Inertia; -use Inertia\Middleware; use Illuminate\Http\Request; use Illuminate\Routing\Router; -use Illuminate\Support\MessageBag; use Illuminate\Support\Facades\App; -use Illuminate\Support\ViewErrorBag; -use Illuminate\Contracts\Http\Kernel; use Illuminate\Support\Facades\Blade; use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Session; +use Illuminate\Support\MessageBag; +use Illuminate\Support\ViewErrorBag; +use Inertia\Inertia; +use Inertia\Middleware; class ServiceProviderTest extends TestCase {