44namespace Josegonzalez \Upload \Test \TestCase \File \Path \Basepath ;
55
66use Cake \TestSuite \TestCase ;
7+ use Josegonzalez \Upload \File \Path \Basepath \DefaultTrait ;
78
89class DefaultTraitTest extends TestCase
910{
11+ private function createTraitMock ()
12+ {
13+ return new class {
14+ use DefaultTrait;
15+
16+ public $ entity ;
17+ public $ table ;
18+ public $ settings ;
19+ public $ data ;
20+ public $ field ;
21+ };
22+ }
23+
1024 public function testNoSpecialConfiguration ()
1125 {
12- $ mock = $ this ->getMockForTrait ( ' Josegonzalez\Upload\File\Path\Basepath\DefaultTrait ' );
13- $ mock ->entity = $ this ->getMockBuilder ('Cake\ORM\Entity ' )-> getMock ( );
26+ $ mock = $ this ->createTraitMock ( );
27+ $ mock ->entity = $ this ->createStub ('Cake\ORM\Entity ' );
1428 $ mock ->table = $ this ->getMockBuilder ('Cake\ORM\Table ' )->getMock ();
1529 $ mock ->settings = [];
1630 $ mock ->data = ['name ' => 'filename ' ];
1731 $ mock ->field = 'field ' ;
18- $ mock ->entity ->expects ($ this ->exactly (0 ))->method ('get ' )->will ($ this ->returnValue (1 ));
19- $ mock ->table ->expects ($ this ->once ())->method ('getAlias ' )->will ($ this ->returnValue ('Table ' ));
20- $ mock ->table ->expects ($ this ->exactly (0 ))->method ('getPrimaryKey ' )->will ($ this ->returnValue ('id ' ));
32+ $ mock ->table ->expects ($ this ->once ())->method ('getAlias ' )->willReturn ('Table ' );
2133 $ this ->assertEquals ('webroot/files/Table/field/ ' , $ mock ->basepath ());
2234 }
2335
2436 public function testCustomPath ()
2537 {
26- $ mock = $ this ->getMockForTrait ( ' Josegonzalez\Upload\File\Path\Basepath\DefaultTrait ' );
27- $ mock ->entity = $ this ->getMockBuilder ('Cake\ORM\Entity ' )-> getMock ( );
38+ $ mock = $ this ->createTraitMock ( );
39+ $ mock ->entity = $ this ->createStub ('Cake\ORM\Entity ' );
2840 $ mock ->table = $ this ->getMockBuilder ('Cake\ORM\Table ' )->getMock ();
2941 $ mock ->settings = ['path ' => 'webroot{DS}files{DS}{model}-{field}{DS} ' ];
3042 $ mock ->data = ['name ' => 'filename ' ];
3143 $ mock ->field = 'field ' ;
32- $ mock ->entity ->expects ($ this ->exactly (0 ))->method ('get ' )->will ($ this ->returnValue (1 ));
33- $ mock ->table ->expects ($ this ->once ())->method ('getAlias ' )->will ($ this ->returnValue ('Table ' ));
34- $ mock ->table ->expects ($ this ->exactly (0 ))->method ('getPrimaryKey ' )->will ($ this ->returnValue ('id ' ));
44+ $ mock ->table ->expects ($ this ->once ())->method ('getAlias ' )->willReturn ('Table ' );
3545 $ this ->assertEquals ('webroot/files/Table-field/ ' , $ mock ->basepath ());
3646 }
3747
3848 public function testExistingEntityWithPrimaryKey ()
3949 {
40- $ mock = $ this ->getMockForTrait ( ' Josegonzalez\Upload\File\Path\Basepath\DefaultTrait ' );
50+ $ mock = $ this ->createTraitMock ( );
4151 $ mock ->entity = $ this ->getMockBuilder ('Cake\ORM\Entity ' )->getMock ();
4252 $ mock ->table = $ this ->getMockBuilder ('Cake\ORM\Table ' )->getMock ();
4353 $ mock ->settings = ['path ' => 'webroot{DS}files{DS}{model}-{field}{DS}{primaryKey}/ ' ];
4454 $ mock ->data = ['name ' => 'filename ' ];
4555 $ mock ->field = 'field ' ;
46- $ mock ->entity ->expects ($ this ->once ())->method ('get ' )->will ( $ this -> returnValue ( 1 ) );
47- $ mock ->table ->expects ($ this ->once ())->method ('getAlias ' )->will ( $ this -> returnValue ( 'Table ' ) );
48- $ mock ->table ->expects ($ this ->exactly (2 ))->method ('getPrimaryKey ' )->will ( $ this -> returnValue ( 'id ' ) );
56+ $ mock ->entity ->expects ($ this ->once ())->method ('get ' )->willReturn ( 1 );
57+ $ mock ->table ->expects ($ this ->once ())->method ('getAlias ' )->willReturn ( 'Table ' );
58+ $ mock ->table ->expects ($ this ->exactly (2 ))->method ('getPrimaryKey ' )->willReturn ( 'id ' );
4959 $ this ->assertEquals ('webroot/files/Table-field/1/ ' , $ mock ->basepath ());
5060 }
5161
5262 public function testNewEntity ()
5363 {
5464 $ this ->expectException ('LogicException ' , '{primaryKey} substitution not allowed for new entities ' );
5565
56- $ mock = $ this ->getMockForTrait ( ' Josegonzalez\Upload\File\Path\Basepath\DefaultTrait ' );
66+ $ mock = $ this ->createTraitMock ( );
5767 $ mock ->entity = $ this ->getMockBuilder ('Cake\ORM\Entity ' )->getMock ();
58- $ mock ->table = $ this ->getMockBuilder ('Cake\ORM\Table ' )-> getMock ( );
68+ $ mock ->table = $ this ->createStub ('Cake\ORM\Table ' );
5969 $ mock ->settings = ['path ' => 'webroot{DS}files{DS}{model}-{field}{DS}{primaryKey}/ ' ];
6070 $ mock ->data = ['name ' => 'filename ' ];
6171 $ mock ->field = 'field ' ;
62- $ mock ->entity ->expects ($ this ->once ())->method ('isNew ' )->will ( $ this -> returnValue ( true ) );
72+ $ mock ->entity ->expects ($ this ->once ())->method ('isNew ' )->willReturn ( true );
6373 $ mock ->basepath ();
6474 }
6575
6676 public function testExitingEntityWithCompositePrimaryKey ()
6777 {
6878 $ this ->expectException ('LogicException ' , '{primaryKey} substitution not valid for composite primary keys ' );
6979
70- $ mock = $ this ->getMockForTrait ( ' Josegonzalez\Upload\File\Path\Basepath\DefaultTrait ' );
80+ $ mock = $ this ->createTraitMock ( );
7181 $ mock ->entity = $ this ->getMockBuilder ('Cake\ORM\Entity ' )->getMock ();
7282 $ mock ->table = $ this ->getMockBuilder ('Cake\ORM\Table ' )->getMock ();
7383 $ mock ->settings = ['path ' => 'webroot{DS}files{DS}{model}-{field}{DS}{primaryKey}/ ' ];
7484 $ mock ->data = ['name ' => 'filename ' ];
7585 $ mock ->field = 'field ' ;
76- $ mock ->entity ->expects ($ this ->once ())->method ('isNew ' )->will ( $ this -> returnValue ( false ) );
77- $ mock ->table ->expects ($ this ->once ())->method ('getPrimaryKey ' )->will ( $ this -> returnValue ( ['id ' , 'other_id ' ]) );
86+ $ mock ->entity ->expects ($ this ->once ())->method ('isNew ' )->willReturn ( false );
87+ $ mock ->table ->expects ($ this ->once ())->method ('getPrimaryKey ' )->willReturn ( ['id ' , 'other_id ' ]);
7888 $ mock ->basepath ();
7989 }
8090
@@ -83,22 +93,21 @@ public function testExitingEntityWithCompositePrimaryKey()
8393 */
8494 public function testPathWithoutPrimaryKey ()
8595 {
86- $ mock = $ this ->getMockForTrait ( ' Josegonzalez\Upload\File\Path\Basepath\DefaultTrait ' );
87- $ mock ->entity = $ this ->getMockBuilder ('Cake\ORM\Entity ' )-> getMock ( );
96+ $ mock = $ this ->createTraitMock ( );
97+ $ mock ->entity = $ this ->createStub ('Cake\ORM\Entity ' );
8898 $ mock ->table = $ this ->getMockBuilder ('Cake\ORM\Table ' )->getMock ();
8999 $ mock ->settings = ['path ' => 'webroot{DS}files{DS}{model}-{field}{DS} ' ];
90100 $ mock ->data = ['name ' => 'filename ' ];
91101 $ mock ->field = 'field ' ;
92- $ mock ->table ->expects ($ this ->exactly (0 ))->method ('getPrimaryKey ' )->will ($ this ->returnValue (['id ' , 'other_id ' ]));
93- $ mock ->table ->expects ($ this ->once ())->method ('getAlias ' )->will ($ this ->returnValue ('Table ' ));
102+ $ mock ->table ->expects ($ this ->once ())->method ('getAlias ' )->willReturn ('Table ' );
94103 $ this ->assertEquals ('webroot/files/Table-field/ ' , $ mock ->basepath ());
95104 }
96105
97106 public function testYearWithMonthPath ()
98107 {
99- $ mock = $ this ->getMockForTrait ( ' Josegonzalez\Upload\File\Path\Basepath\DefaultTrait ' );
100- $ mock ->entity = $ this ->getMockBuilder ('Cake\ORM\Entity ' )-> getMock ( );
101- $ mock ->table = $ this ->getMockBuilder ('Cake\ORM\Table ' )-> getMock ( );
108+ $ mock = $ this ->createTraitMock ( );
109+ $ mock ->entity = $ this ->createStub ('Cake\ORM\Entity ' );
110+ $ mock ->table = $ this ->createStub ('Cake\ORM\Table ' );
102111 $ mock ->settings = ['path ' => 'webroot{DS}files{DS}{year}{DS}{month}{DS} ' ];
103112 $ mock ->data = ['name ' => 'filename ' ];
104113 $ mock ->field = 'field ' ;
@@ -108,9 +117,9 @@ public function testYearWithMonthPath()
108117
109118 public function testYearWithMonthAndDayPath ()
110119 {
111- $ mock = $ this ->getMockForTrait ( ' Josegonzalez\Upload\File\Path\Basepath\DefaultTrait ' );
112- $ mock ->entity = $ this ->getMockBuilder ('Cake\ORM\Entity ' )-> getMock ( );
113- $ mock ->table = $ this ->getMockBuilder ('Cake\ORM\Table ' )-> getMock ( );
120+ $ mock = $ this ->createTraitMock ( );
121+ $ mock ->entity = $ this ->createStub ('Cake\ORM\Entity ' );
122+ $ mock ->table = $ this ->createStub ('Cake\ORM\Table ' );
114123 $ mock ->settings = ['path ' => 'webroot{DS}files{DS}{year}{DS}{month}{DS}{day}{DS} ' ];
115124 $ mock ->data = ['name ' => 'filename ' ];
116125 $ mock ->field = 'field ' ;
@@ -120,15 +129,14 @@ public function testYearWithMonthAndDayPath()
120129
121130 public function testModelFieldYearWithMonthAndDayPath ()
122131 {
123- $ mock = $ this ->getMockForTrait ( ' Josegonzalez\Upload\File\Path\Basepath\DefaultTrait ' );
124- $ mock ->entity = $ this ->getMockBuilder ('Cake\ORM\Entity ' )-> getMock ( );
132+ $ mock = $ this ->createTraitMock ( );
133+ $ mock ->entity = $ this ->createStub ('Cake\ORM\Entity ' );
125134 $ mock ->table = $ this ->getMockBuilder ('Cake\ORM\Table ' )->getMock ();
126135 $ mock ->settings = ['path ' => 'webroot{DS}files{DS}{model}{DS}{field}{DS}{year}{DS}{month}{DS}{day}{DS} ' ];
127136
128137 $ mock ->data = ['name ' => 'filename ' ];
129138 $ mock ->field = 'field ' ;
130- $ mock ->entity ->expects ($ this ->exactly (0 ))->method ('get ' )->will ($ this ->returnValue (1 ));
131- $ mock ->table ->expects ($ this ->once ())->method ('getAlias ' )->will ($ this ->returnValue ('Table ' ));
139+ $ mock ->table ->expects ($ this ->once ())->method ('getAlias ' )->willReturn ('Table ' );
132140
133141 $ this ->assertEquals ('webroot/files/Table/field/ ' . date ('Y ' ) . '/ ' . date ('m ' ) . '/ ' . date ('d ' ) . '/ ' , $ mock ->basepath ());
134142 }
@@ -137,54 +145,59 @@ public function testFieldValueMissing()
137145 {
138146 $ this ->expectException ('LogicException ' , 'Field value for substitution is missing: field ' );
139147
140- $ mock = $ this ->getMockForTrait ('Josegonzalez\Upload\File\Path\Basepath\DefaultTrait ' );
141- $ mock ->entity = $ this ->getMockBuilder ('Cake\ORM\Entity ' )->getMock ();
142- $ mock ->table = $ this ->getMockBuilder ('Cake\ORM\Table ' )->getMock ();
148+ $ mock = $ this ->createTraitMock ();
149+ $ entity = $ this ->createStub ('Cake\ORM\Entity ' );
150+ $ entity ->method ('get ' )->willReturn (null );
151+ $ mock ->entity = $ entity ;
152+ $ mock ->table = $ this ->createStub ('Cake\ORM\Table ' );
143153 $ mock ->settings = ['path ' => 'webroot{DS}files{DS}{model}{DS}{field-value:field}{DS} ' ];
144154 $ mock ->data = ['name ' => 'filename ' ];
145155 $ mock ->field = 'field ' ;
146- $ mock ->entity ->expects ($ this ->any ())->method ('get ' )->will ($ this ->returnValue (null ));
147156 $ mock ->basepath ();
148157 }
149158
150159 public function testFieldValueNonScalar ()
151160 {
152161 $ this ->expectException ('LogicException ' , 'Field value for substitution must be a integer, float, string or boolean: field ' );
153162
154- $ mock = $ this ->getMockForTrait ('Josegonzalez\Upload\File\Path\Basepath\DefaultTrait ' );
155- $ mock ->entity = $ this ->getMockBuilder ('Cake\ORM\Entity ' )->getMock ();
156- $ mock ->table = $ this ->getMockBuilder ('Cake\ORM\Table ' )->getMock ();
163+ $ mock = $ this ->createTraitMock ();
164+ $ entity = $ this ->createStub ('Cake\ORM\Entity ' );
165+ $ entity ->method ('get ' )->willReturn ([]);
166+ $ mock ->entity = $ entity ;
167+ $ mock ->table = $ this ->createStub ('Cake\ORM\Table ' );
157168 $ mock ->settings = ['path ' => 'webroot{DS}files{DS}{model}{DS}{field-value:field}{DS} ' ];
158169 $ mock ->data = ['name ' => 'filename ' ];
159170 $ mock ->field = 'field ' ;
160- $ mock ->entity ->expects ($ this ->any ())->method ('get ' )->will ($ this ->returnValue ([]));
161171 $ mock ->basepath ();
162172 }
163173
164174 public function testFieldValueZeroLength ()
165175 {
166176 $ this ->expectException ('LogicException ' , 'Field value for substitution must be non-zero in length: field ' );
167177
168- $ mock = $ this ->getMockForTrait ('Josegonzalez\Upload\File\Path\Basepath\DefaultTrait ' );
169- $ mock ->entity = $ this ->getMockBuilder ('Cake\ORM\Entity ' )->getMock ();
170- $ mock ->table = $ this ->getMockBuilder ('Cake\ORM\Table ' )->getMock ();
178+ $ mock = $ this ->createTraitMock ();
179+ $ entity = $ this ->createStub ('Cake\ORM\Entity ' );
180+ $ entity ->method ('get ' )->willReturn ('' );
181+ $ mock ->entity = $ entity ;
182+ $ mock ->table = $ this ->createStub ('Cake\ORM\Table ' );
171183 $ mock ->settings = ['path ' => 'webroot{DS}files{DS}{model}{DS}{field-value:field}{DS} ' ];
172184 $ mock ->data = ['name ' => 'filename ' ];
173185 $ mock ->field = 'field ' ;
174- $ mock ->entity ->expects ($ this ->any ())->method ('get ' )->will ($ this ->returnValue ('' ));
175186 $ mock ->basepath ();
176187 }
177188
178189 public function testFieldValue ()
179190 {
180- $ mock = $ this ->getMockForTrait ('Josegonzalez\Upload\File\Path\Basepath\DefaultTrait ' );
181- $ mock ->entity = $ this ->getMockBuilder ('Cake\ORM\Entity ' )->getMock ();
182- $ mock ->table = $ this ->getMockBuilder ('Cake\ORM\Table ' )->getMock ();
191+ $ mock = $ this ->createTraitMock ();
192+ $ entity = $ this ->createStub ('Cake\ORM\Entity ' );
193+ $ entity ->method ('get ' )->willReturn ('value ' );
194+ $ table = $ this ->getMockBuilder ('Cake\ORM\Table ' )->getMock ();
195+ $ table ->expects ($ this ->once ())->method ('getAlias ' )->willReturn ('Table ' );
196+ $ mock ->entity = $ entity ;
197+ $ mock ->table = $ table ;
183198 $ mock ->settings = ['path ' => 'webroot{DS}files{DS}{model}{DS}{field-value:field}{DS} ' ];
184199 $ mock ->data = ['name ' => 'filename ' ];
185200 $ mock ->field = 'field ' ;
186- $ mock ->entity ->expects ($ this ->any ())->method ('get ' )->will ($ this ->returnValue ('value ' ));
187- $ mock ->table ->expects ($ this ->once ())->method ('getAlias ' )->will ($ this ->returnValue ('Table ' ));
188201 $ this ->assertEquals ('webroot/files/Table/value/ ' , $ mock ->basepath ());
189202 }
190203}
0 commit comments