Skip to content

Commit 40e07dc

Browse files
Switch to github testing from travisci (#49)
* Switch to github testing * Also remove styleci * use correct name for matrix versions * Adds composer commands * Create laravel 10 stub * Fix import * Fixes internal request closures Fixes warning about abstract test class (just for neatness) * Fixes auth test * Fixes routetest for adapter args * Fix for phpunit10 * Fix regression for morph event * Fix overriding original content
1 parent c84f168 commit 40e07dc

21 files changed

+303
-72
lines changed

.github/workflows/ci.yml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: CI Tests
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- master
8+
- v*
9+
10+
jobs:
11+
tests:
12+
runs-on: ubuntu-22.04
13+
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
stability: [prefer-stable]
18+
#versions: [ { php: 8.1, laravel: 10 }, { php: 8.2, laravel: 10 }, { php: 8.3, laravel: 10 }, { php: 8.2, laravel: 11 }, { php: 8.3, laravel: 11 } ]
19+
versions: [ { php: 8.1, laravel: 10 }, { php: 8.2, laravel: 10 }, { php: 8.3, laravel: 10 } ]
20+
21+
name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}
22+
23+
steps:
24+
- name: Checkout code
25+
uses: actions/checkout@v4
26+
27+
# Docs: https://github.com/shivammathur/setup-php
28+
- name: Setup PHP
29+
uses: shivammathur/setup-php@v2
30+
with:
31+
php-version: ${{ matrix.versions.php }}
32+
extensions: dom, curl, libxml, mbstring, zip
33+
ini-values: error_reporting=E_ALL
34+
tools: composer:v2
35+
# todo: Add
36+
coverage: none
37+
38+
- name: Install dependencies
39+
run: |
40+
composer require "illuminate/contracts=^${{ matrix.versions.laravel }}" --no-update
41+
composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader
42+
43+
- name: Run phpunit tests
44+
run: composer test

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ vendor
77
.php_cs
88
.phpunit.result.cache
99
.phpunit.result.cache/
10+
test-results

.styleci.yml

-8
This file was deleted.

.travis.yml

-37
This file was deleted.

composer.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,13 @@
6868
"sort-packages": true
6969
},
7070
"minimum-stability": "dev",
71-
"prefer-stable": true
71+
"prefer-stable": true,
72+
"scripts": {
73+
"test": [
74+
"vendor/bin/phpunit"
75+
],
76+
"lint": [
77+
"vendor/bin/phpcs"
78+
]
79+
}
7280
}

src/Http/Response.php

+21-14
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ class Response extends IlluminateResponse
3434
*/
3535
protected $binding;
3636

37+
/**
38+
* Intermedia content that we are working on. The result content should be a string (symfony)
39+
* )
40+
* @var mixed Content with which we are working
41+
*/
42+
protected $workingContent;
43+
3744
/**
3845
* Array of registered formatters.
3946
*
@@ -125,12 +132,12 @@ public static function makeFromJson(JsonResponse $json)
125132
*/
126133
public function morph($format = 'json')
127134
{
128-
$content = $this->getOriginalContent() ?? '';
135+
$this->workingContent = $this->getOriginalContent() ?? '';
129136

130137
$this->fireMorphingEvent();
131138

132-
if (isset(static::$transformer) && static::$transformer->transformableResponse($content)) {
133-
$content = static::$transformer->transform($content);
139+
if (isset(static::$transformer) && static::$transformer->transformableResponse($this->workingContent)) {
140+
$this->workingContent = static::$transformer->transform($this->workingContent);
134141
}
135142

136143
$formatter = static::getFormatter($format);
@@ -147,21 +154,21 @@ public function morph($format = 'json')
147154

148155
$this->fireMorphedEvent();
149156

150-
if ($content instanceof EloquentModel) {
151-
$content = $formatter->formatEloquentModel($content);
152-
} elseif ($content instanceof EloquentCollection) {
153-
$content = $formatter->formatEloquentCollection($content);
154-
} elseif (is_array($content) || $content instanceof ArrayObject || $content instanceof Arrayable) {
155-
$content = $formatter->formatArray($content);
156-
} elseif ($content instanceof stdClass) {
157-
$content = $formatter->formatArray((array) $content);
157+
if ($this->workingContent instanceof EloquentModel) {
158+
$this->workingContent = $formatter->formatEloquentModel($this->workingContent);
159+
} elseif ($this->workingContent instanceof EloquentCollection) {
160+
$this->workingContent = $formatter->formatEloquentCollection($this->workingContent);
161+
} elseif (is_array($this->workingContent) || $this->workingContent instanceof ArrayObject || $this->workingContent instanceof Arrayable) {
162+
$this->workingContent = $formatter->formatArray($this->workingContent);
163+
} elseif ($this->workingContent instanceof stdClass) {
164+
$this->workingContent = $formatter->formatArray((array) $this->workingContent);
158165
} else {
159166
if (! empty($defaultContentType)) {
160167
$this->headers->set('Content-Type', $defaultContentType);
161168
}
162169
}
163170

164-
$this->content = $content;
171+
$this->content = $this->workingContent;
165172

166173
return $this;
167174
}
@@ -177,7 +184,7 @@ protected function fireMorphedEvent()
177184
return;
178185
}
179186

180-
static::$events->dispatch(new ResponseWasMorphed($this, $this->content));
187+
static::$events->dispatch(new ResponseWasMorphed($this, $this->workingContent));
181188
}
182189

183190
/**
@@ -191,7 +198,7 @@ protected function fireMorphingEvent()
191198
return;
192199
}
193200

194-
static::$events->dispatch(new ResponseIsMorphing($this, $this->content));
201+
static::$events->dispatch(new ResponseIsMorphing($this, $this->workingContent));
195202
}
196203

197204
/**

src/Provider/DingoServiceProvider.php

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Dingo\Api\Provider;
44

5+
use Illuminate\Routing\CallableDispatcher;
6+
use Illuminate\Routing\Contracts\CallableDispatcher as CallableDispatcherContract;
57
use RuntimeException;
68
use Dingo\Api\Auth\Auth;
79
use Dingo\Api\Dispatcher;
@@ -55,6 +57,7 @@ public function register()
5557
$this->registerExceptionHandler();
5658

5759
$this->registerDispatcher();
60+
$this->registerCallableDispatcher();
5861

5962
$this->registerAuth();
6063

@@ -145,6 +148,13 @@ public function registerDispatcher()
145148
});
146149
}
147150

151+
public function registerCallableDispatcher()
152+
{
153+
$this->app->singleton(CallableDispatcherContract::class, function ($app) {
154+
return new CallableDispatcher($app);
155+
});
156+
}
157+
148158
/**
149159
* Register the auth.
150160
*

src/Provider/LaravelServiceProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Illuminate\Contracts\Http\Kernel;
1010
use Dingo\Api\Event\RequestWasMatched;
1111
use Dingo\Api\Http\Middleware\Request;
12-
use Illuminate\Foundation\Application;
12+
use Illuminate\Contracts\Foundation\Application;
1313
use Dingo\Api\Http\Middleware\RateLimit;
1414
use Illuminate\Routing\ControllerDispatcher;
1515
use Dingo\Api\Http\Middleware\PrepareController;

tests/ChecksLaravelVersionTrait.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Dingo\Api\Tests;
44

5+
use Dingo\Api\Tests\Stubs\Application10Stub;
56
use Dingo\Api\Tests\Stubs\Application9Stub;
67
use Dingo\Api\Tests\Stubs\ApplicationStub;
78
use Dingo\Api\Tests\Stubs\Application8Stub;
@@ -55,7 +56,9 @@ private function getApplicationStub()
5556
$version = str_replace('v', '', $version);
5657

5758
// Return the version stub for the right version
58-
if (version_compare($version, '9.0.0', '>=')) {
59+
if (version_compare($version, '10.0.0', '>=')) {
60+
return new Application10Stub;
61+
} else if (version_compare($version, '9.0.0', '>=')) {
5962
return new Application9Stub;
6063
} elseif (version_compare($version, '8.0.0', '>=')) {
6164
return new Application8Stub;

tests/DispatcherTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use Illuminate\Http\JsonResponse;
2222
use Illuminate\Http\RedirectResponse;
2323
use Illuminate\Http\Request;
24+
use Illuminate\Routing\CallableDispatcher;
25+
use Illuminate\Routing\Contracts\CallableDispatcher as CallableDispatcherContract;
2426
use Illuminate\Support\Facades\Request as RequestFacade;
2527
use Mockery as m;
2628
use Symfony\Component\HttpKernel\Exception\GoneHttpException;
@@ -67,14 +69,15 @@ public function setUp(): void
6769

6870
$this->transformerFactory = new TransformerFactory($this->container, new TransformerStub);
6971

70-
$this->adapter = new RoutingAdapterStub;
72+
$this->adapter = new RoutingAdapterStub($this->container);
7173
$this->exception = m::mock(Handler::class);
7274
$this->router = new Router($this->adapter, $this->exception, $this->container, null, null);
7375

7476
$this->auth = new Auth($this->router, $this->container, []);
7577
$this->dispatcher = new Dispatcher($this->container, new Filesystem, $this->router, $this->auth);
7678

7779
app()->instance(\Illuminate\Routing\Router::class, $this->adapter);
80+
$this->container->singleton(CallableDispatcherContract::class, CallableDispatcher::class);
7881

7982
$this->dispatcher->setSubtype('api');
8083
$this->dispatcher->setStandardsTree('vnd');

tests/Exception/HandlerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function testExceptionTraceIncludedInResponse()
162162

163163
$object = json_decode($response->getContent());
164164

165-
$this->assertObjectHasAttribute('debug', $object);
165+
$this->assertObjectHasProperty('debug', $object);
166166
}
167167

168168
public function testHttpExceptionsWithNoMessageUseStatusCodeMessage()

tests/Http/Middleware/AuthTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class AuthTest extends BaseTestCase
4040
public function setUp(): void
4141
{
4242
$this->container = new Container;
43-
$this->adapter = new RoutingAdapterStub;
43+
$this->adapter = new RoutingAdapterStub($this->container);
4444
$this->router = m::mock(Router::class);
4545
$this->auth = m::mock(Auth::class);
4646
$this->middleware = new AuthMiddleware($this->router, $this->auth);

tests/Http/ResponseTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function testBuildingWithCustomStatusCodeAndHeaders()
6868
public function testChangingContentWithEvents()
6969
{
7070
$this->events->listen(ResponseWasMorphed::class, function ($event) {
71-
$event->content['foo'] = 'bam!';
71+
$event->content = '{"foo":"bam!"}';
7272
});
7373

7474
Response::addFormatter('json', new Json);

tests/Routing/Adapter/BaseAdapterTest.php renamed to tests/Routing/Adapter/BaseAdapterTestAbstract.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Laravel\Lumen\Application;
1414
use Mockery as m;
1515

16-
abstract class BaseAdapterTest extends BaseTestCase
16+
abstract class BaseAdapterTestAbstract extends BaseTestCase
1717
{
1818
/**
1919
* @var Container|Application

tests/Routing/Adapter/LaravelTest.php renamed to tests/Routing/Adapter/LaravelTestAbstract.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Illuminate\Events\Dispatcher;
88
use Illuminate\Routing\Router;
99

10-
class LaravelTest extends BaseAdapterTest
10+
class LaravelTestAbstract extends BaseAdapterTestAbstract
1111
{
1212
public function getAdapterInstance()
1313
{

tests/Routing/Adapter/LumenTest.php renamed to tests/Routing/Adapter/LumenTestAbstract.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Illuminate\Http\Request;
1010
use Laravel\Lumen\Application;
1111

12-
class LumenTest extends BaseAdapterTest
12+
class LumenTestAbstract extends BaseAdapterTestAbstract
1313
{
1414
public function getAdapterInstance()
1515
{

tests/Routing/RouteTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class RouteTest extends BaseTestCase
2525

2626
public function setUp(): void
2727
{
28-
$this->adapter = new RoutingAdapterStub;
2928
$this->container = new Container;
29+
$this->adapter = new RoutingAdapterStub($this->container);
3030
}
3131

3232
public function testCreatingNewRoute()

tests/Routing/RouterTest.php renamed to tests/Routing/RouterTestAbstract.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Mockery as m;
1414
use Symfony\Component\HttpKernel\Exception\HttpException;
1515

16-
class RouterTest extends Adapter\BaseAdapterTest
16+
class RouterTestAbstract extends Adapter\BaseAdapterTestAbstract
1717
{
1818
public function getAdapterInstance()
1919
{

tests/Routing/UrlGeneratorTest.php renamed to tests/Routing/UrlGeneratorTestAbstract.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Dingo\Api\Tests\Stubs\RoutingControllerStub;
99
use Illuminate\Container\Container;
1010

11-
class UrlGeneratorTest extends Adapter\BaseAdapterTest
11+
class UrlGeneratorTestAbstract extends Adapter\BaseAdapterTestAbstract
1212
{
1313
public function getAdapterInstance()
1414
{

0 commit comments

Comments
 (0)