33
44use Doctrine \ORM \Mapping \Builder \ClassMetadataBuilder ;
55use Gedmo \Exception \InvalidArgumentException ;
6+ use LaravelDoctrine \Fluent \Builders \Builder ;
67use LaravelDoctrine \Fluent \Builders \Field ;
78use LaravelDoctrine \Fluent \Extensions \ExtensibleClassMetadata ;
89use Gedmo \Sluggable \Mapping \Driver \Fluent as SluggableDriver ;
910use LaravelDoctrine \Fluent \Extensions \Gedmo \Sluggable ;
11+ use Tests \Stubs \Entities \StubEntity ;
1012
1113/**
1214 * @mixin \PHPUnit_Framework_TestCase
1315 */
1416class SluggableTest extends \PHPUnit_Framework_TestCase
1517{
18+ /**
19+ * @var ClassMetadataBuilder
20+ */
21+ private $ classMetadataBuilder ;
22+
1623 /**
1724 * @var string
1825 */
@@ -30,18 +37,16 @@ class SluggableTest extends \PHPUnit_Framework_TestCase
3037
3138 protected function setUp ()
3239 {
33- $ this ->fieldName = 'slug ' ;
34- $ this ->classMetadata = new ExtensibleClassMetadata ('foo ' );
35- Field::make (new ClassMetadataBuilder ($ this ->classMetadata ), 'string ' , 'slug ' )->build ();
36-
37- $ this ->extension = new Sluggable ($ this ->classMetadata , $ this ->fieldName , 'name ' );
40+ $ this ->fieldName = 'slug ' ;
41+ $ this ->classMetadata = new ExtensibleClassMetadata (StubEntity::class);
42+ $ this ->classMetadataBuilder = new ClassMetadataBuilder ($ this ->classMetadata );
3843 }
3944
4045 public function test_it_should_add_itself_as_a_field_macro ()
4146 {
4247 Sluggable::enable ();
4348
44- $ field = Field::make (new ClassMetadataBuilder (new ExtensibleClassMetadata ( ' Foo ' ) ), 'string ' , $ this ->fieldName )->build ();
49+ $ field = Field::make (new ClassMetadataBuilder ($ this -> classMetadata ), 'string ' , $ this ->fieldName )->build ();
4550
4651 $ this ->assertInstanceOf (
4752 Sluggable::class,
@@ -55,13 +60,40 @@ public function test_can_only_make_a_valid_field_sluggable()
5560
5661 Sluggable::enable ();
5762
58- $ field = Field::make (new ClassMetadataBuilder ( new ExtensibleClassMetadata ( ' Foo ' )) , 'smallint ' , $ this ->fieldName )-> build ( );
63+ $ field = Field::make ($ this -> classMetadataBuilder , 'bigint ' , $ this ->fieldName );
5964 call_user_func ([$ field , Sluggable::MACRO_METHOD ], 'name ' );
65+ $ field ->build ();
6066 }
6167
68+ public function test_it_queues_when_used_as_field_macro_so_that_the_field_gets_built_before_the_extension ()
69+ {
70+ Sluggable::enable ();
71+
72+ $ builder = new Builder ($ this ->classMetadataBuilder );
73+
74+ $ builder ->string ('title ' );
75+ $ builder ->string ('slug ' )->sluggable ('title ' );
76+
77+ $ builder ->build ();
78+
79+ $ this ->assertBuildResultIs ([
80+ 'fields ' => ['title ' ],
81+ 'handlers ' => [],
82+ 'slug ' => $ this ->fieldName ,
83+ 'style ' => 'default ' ,
84+ 'dateFormat ' => 'Y-m-d-H:i ' ,
85+ 'updatable ' => true ,
86+ 'unique ' => true ,
87+ 'unique_base ' => null ,
88+ 'separator ' => '- ' ,
89+ 'prefix ' => '' ,
90+ 'suffix ' => ''
91+ ]);
92+ }
6293
6394 public function test_it_should_add_sluggable_to_the_given_field ()
6495 {
96+ $ this ->classMetadataBuilder ->addField ('slug ' , 'string ' );
6597 $ this ->getExtension ()->build ();
6698
6799 $ this ->assertBuildResultIs ([
@@ -81,6 +113,8 @@ public function test_it_should_add_sluggable_to_the_given_field()
81113
82114 public function test_can_set_custom_settingss ()
83115 {
116+ $ this ->classMetadataBuilder ->addField ('slug ' , 'string ' );
117+
84118 $ this ->getExtension ()
85119 ->baseOn ('custom ' )
86120 ->handlers ('Handler ' )
@@ -146,7 +180,7 @@ protected function assertBuildResultIs(array $expected)
146180 */
147181 protected function getExtension ()
148182 {
149- return $ this ->extension ;
183+ return new Sluggable ( $ this ->classMetadata , $ this -> fieldName , ' name ' ) ;
150184 }
151185
152186 /**
0 commit comments