This repository was archived by the owner on Dec 10, 2018. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +82
-21
lines changed
Expand file tree Collapse file tree 5 files changed +82
-21
lines changed Original file line number Diff line number Diff line change @@ -50,19 +50,21 @@ class TestModel extends Model
5050}
5151```
5252
53- If don't like the primary key named ` uuid ` you can leave off the ` HasUuidPrimaryKey ` trait and manually specify ` $primaryKey ` . Don't forget set ` $incrementing ` to false .
53+ If don't like the primary key named ` uuid ` you can overwrite the ` getKeyName ` method to manually specify the primary key name .
5454
5555``` php
5656use Illuminate\Database\Eloquent\Model;
5757use Spatie\BinaryUuid\HasBinaryUuid;
5858
5959class TestModel extends Model
6060{
61- use HasBinaryUuid;
61+ use HasBinaryUuid,
62+ HasUuidPrimaryKey;
6263
63- public $incrementing = false;
64-
65- public $primaryKey = 'uuid';
64+ public function getKeyName()
65+ {
66+ return 'uuid';
67+ }
6668}
6769```
6870
Original file line number Diff line number Diff line change @@ -13,4 +13,9 @@ public function getIncrementing()
1313 {
1414 return false ;
1515 }
16+
17+ public function resolveRouteBinding ($ value )
18+ {
19+ return $ this ->withUuid ($ value )->first ();
20+ }
1621}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Spatie \BinaryUuid \Test \Feature ;
4+
5+ use Spatie \BinaryUuid \Test \TestModel ;
6+
7+ trait CreatesModel
8+ {
9+ private function createModel (string $ uuid , $ relationUuid = null ): TestModel
10+ {
11+ $ model = new TestModel ();
12+
13+ $ model ->uuid_text = $ uuid ;
14+
15+ if ($ relationUuid ) {
16+ $ model ->relation_uuid = TestModel::encodeUuid ($ relationUuid );
17+ }
18+
19+ $ model ->save ();
20+
21+ return $ model ;
22+ }
23+ }
Original file line number Diff line number Diff line change 11<?php
22
3- namespace Spatie \BinaryUuid \Test \Unit ;
3+ namespace Spatie \BinaryUuid \Test \Feature ;
44
55use Ramsey \Uuid \Uuid ;
66use Spatie \BinaryUuid \Test \TestCase ;
77use Spatie \BinaryUuid \Test \TestModel ;
88
99class HasBinaryUuidTest extends TestCase
1010{
11+ use CreatesModel;
12+
1113 /** @test */
1214 public function it_generates_the_uuid_on_save ()
1315 {
@@ -153,19 +155,4 @@ public function it_serialises_the_model_correctly_with_json_encode()
153155 $ this ->assertContains ($ model ->uuid_text , $ json );
154156 $ this ->assertNotContains ($ model ->uuid , $ json );
155157 }
156-
157- private function createModel (string $ uuid , $ relationUuid = null ): TestModel
158- {
159- $ model = new TestModel ();
160-
161- $ model ->uuid_text = $ uuid ;
162-
163- if ($ relationUuid ) {
164- $ model ->relation_uuid = TestModel::encodeUuid ($ relationUuid );
165- }
166-
167- $ model ->save ();
168-
169- return $ model ;
170- }
171158}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Spatie \BinaryUuid \Test \Feature ;
4+
5+ use Ramsey \Uuid \Uuid ;
6+ use Spatie \BinaryUuid \Test \TestCase ;
7+ use Spatie \BinaryUuid \Test \TestModel ;
8+ use Illuminate \Routing \Middleware \SubstituteBindings ;
9+
10+ class HasUuidPrimaryKeyTest extends TestCase
11+ {
12+ use CreatesModel;
13+
14+ /** @test */
15+ public function it_resolves_route_binding ()
16+ {
17+ $ uuid = Uuid::uuid1 ();
18+ $ this ->createModel ($ uuid );
19+
20+ $ resolvedModel = (new TestModel ())->resolveRouteBinding ($ uuid );
21+
22+ $ this ->assertEquals ((string ) $ uuid , $ resolvedModel ->uuid_text );
23+ }
24+
25+ /** @test */
26+ public function laravel_resolves_route_binding_correctly ()
27+ {
28+ $ uuid = Uuid::uuid1 ();
29+ $ this ->createModel ($ uuid );
30+
31+ app ('router ' )
32+ ->middleware (SubstituteBindings::class)
33+ ->group (function () {
34+ app ('router ' )->get ('uuid-test/{model} ' , function (TestModel $ model ) {
35+ return $ model ;
36+ });
37+ });
38+
39+ $ this ->get ("uuid-test/ {$ uuid ->toString ()}" )
40+ ->assertJson ([
41+ 'uuid ' => $ uuid ,
42+ ]);
43+ }
44+ }
You can’t perform that action at this time.
0 commit comments