8000 [Feature] Add domain method to fluent routing · jpickwell/laravel-json-api@7265589 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7265589

Browse files
committed
[Feature] Add domain method to fluent routing
1 parent 6c84659 commit 7265589

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
All notable changes to this project will be documented in this file. This project adheres to
33
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).
44

5+
## Unreleased
6+
7+
### Added
8+
- Added `domain` method to API fluent routing methods.
9+
510
## [1.1.0] - 2019-04-12
611

712
### Added

src/Routing/ApiRegistration.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ public function middleware(string ...$middleware): self
133133
return $this;
134134
}
135135

136+
/**
137+
* @param string $domain
138+
* @return ApiRegistration
139+
*/
140+
public function domain(string $domain): self
141+
{
142+
$this->attributes['domain'] = $domain;
143+
144+
return $this;
145+
}
146+
136147
/**
137148
* @param string $namespace
138149
* @return $this

tests/lib/Integration/SubDomainTest.php renamed to tests/lib/Integration/Routing/SubDomainTest.php

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
* limitations under the License.
1616
*/
1717

18-
namespace CloudCreativity\LaravelJsonApi\Tests\Integration;
18+
namespace CloudCreativity\LaravelJsonApi\Tests\Integration\Routing;
1919

20+
use CloudCreativity\LaravelJsonApi\Routing\RouteRegistrar;
21+
use CloudCreativity\LaravelJsonApi\Tests\Integration\TestCase;
2022
use DummyApp\Post;
2123
use DummyApp\User;
22-
use Illuminate\Support\Facades\Route;
2324

2425
/**
2526
* Class SubDomainTest
@@ -38,24 +39,43 @@ class SubDomainTest extends TestCase
3839
*/
3940
protected $resourceType = 'posts';
4041

42+
/**
43+
* @var bool
44+
*/
45+
protected $appRoutes = false;
46+
47+
/**
48+
* @return void
49+
*/
50+
protected function setUp(): void
51+
{
52+
parent::setUp();
53+
54+
$this->withFluentRoutes()->domain('{wildcard}.example.com')->routes(function (RouteRegistrar $api) {
55+
$api->resource('posts')->relationships(function ($relations) {
56+
$relations->hasOne('author');
57+
});
58+
});
59+
}
60+
4161
public function testRead()
4262
{
4363
$post = factory(Post::class)->create();
44-
$uri = route('api:v1:posts.read', ['foo', $post]);
64+
$uri = "http://foo.example.com/api/v1/posts/{$post->getRouteKey()}";
4565

4666
$this->getJsonApi($uri)->assertFetchedOne([
4767
'type' => 'posts',
4868
'id' => (string) $post->getRouteKey(),
4969
'links' => [
50-
'self' => "http://foo.example.com/api/v1/posts/{$post->getRouteKey()}",
70+
'self' => $uri,
5171
],
5272
]);
5373
}
5474

5575
public function testUpdate()
5676
{
5777
$post = factory(Post::class)->create();
58-
$uri = route('api:v1:posts.update', ['foo', $post]);
78+
$uri = "http://foo.example.com/api/v1/posts/{$post->getRouteKey()}";
5979

6080
$data = [
6181
'type' => 'posts',
@@ -71,23 +91,23 @@ public function testUpdate()
7191
public function testDelete()
7292
{
7393
$post = factory(Post::class)->create();
74-
$uri = route('api:v1:posts.delete', ['foo', $post]);
94+
$uri = "http://foo.example.com/api/v1/posts/{$post->getRouteKey()}";
7595

7696
$this->deleteJsonApi($uri)->assertStatus(204);
7797
}
7898

7999
public function testReadRelated()
80100
{
81101
$post = factory(Post::class)->create();
82-
$uri = route('api:v1:posts.relationships.author', ['foo', $post]);
102+
$uri = "http://foo.example.com/api/v1/posts/{$post->getRouteKey()}/author";
83103

84104
$this->getJsonApi($uri)->assertStatus(200);
85105
}
86106

87107
public function testReadRelationship()
88108
{
89109
$post = factory(Post::class)->create();
90-
$uri = route('api:v1:posts.relationships.author.read', ['foo', $post]);
110+
$uri = "http://foo.example.com/api/v1/posts/{$post->getRouteKey()}/relationships/author";
91111

92112
$this->getJsonApi($uri)->assertStatus(200);
93113
}
@@ -96,7 +116,7 @@ public function testReplaceRelationship()
96116
{
97117
$post = factory(Post::class)->create();
98118
$user = factory(User::class)->create();
99-
$uri = route('api:v1:posts.relationships.author.replace', ['foo', $post]);
119+
$uri = "http://foo.example.com/api/v1/posts/{$post->getRouteKey()}/relationships/author";
100120

101121
$data = [
102122
'type' => 'users',
@@ -106,15 +126,4 @@ public function testReplaceRelationship()
106126
$this->patchJsonApi($uri, [], compact('data'))->assertStatus(204);
107127
}
108128

109-
/**
110-
* @return $this|void
111-
*/
112-
protected function withAppRoutes()
113-
{
114-
Route::group([
115-
'domain' => '{wildcard}.example.com',
116-
], function () {
117-
parent::withAppRoutes();
118-
});
119-
}
120129
}

0 commit comments

Comments
 (0)
0