55use Cake \Controller \ComponentRegistry ;
66use Cake \Controller \Controller ;
77use Cake \Core \Configure ;
8+ use Cake \Core \Plugin ;
89use Cake \Event \Event ;
910use Cake \Http \ServerRequest ;
1011use Cake \TestSuite \TestCase ;
@@ -26,6 +27,11 @@ class AuthUserComponentTest extends TestCase {
2627 */
2728 protected $ AuthUser ;
2829
30+ /**
31+ * @var \Cake\Controller\Controller
32+ */
33+ protected $ controller ;
34+
2935 /**
3036 * @return void
3137 */
@@ -34,10 +40,13 @@ public function setUp(): void {
3440 'allowFilePath ' => ROOT . DS . 'tests ' . DS . 'test_files ' . DS ,
3541 ];
3642
37- $ controller = new Controller (new ServerRequest ());
38- $ componentRegistry = new ComponentRegistry ($ controller );
43+ $ this -> controller = new Controller (new ServerRequest ());
44+ $ componentRegistry = new ComponentRegistry ($ this -> controller );
3945 $ this ->AuthUser = new TestAuthUserComponent ($ componentRegistry );
40- $ this ->AuthUser ->Auth = $ this ->getMockBuilder (AuthComponent::class)->onlyMethods (['user ' ])->setConstructorArgs ([$ componentRegistry , $ config ])->getMock ();
46+ $ this ->controller ->loadComponent ('TinyAuth.Auth ' , [
47+ 'allowFilePath ' => Plugin::path ('TinyAuth ' ) . 'tests ' . DS . 'test_files ' . DS ,
48+ ]);
49+ $ this ->controller ->Auth = $ this ->getMockBuilder (AuthComponent::class)->onlyMethods (['user ' ])->setConstructorArgs ([$ componentRegistry , $ config ])->getMock ();
4150
4251 Configure::write ('Roles ' , [
4352 'user ' => 1 ,
@@ -55,7 +64,7 @@ public function testIsAuthorizedValid() {
5564 'id ' => 1 ,
5665 'role_id ' => 1 ,
5766 ];
58- $ this ->AuthUser ->Auth ->expects ($ this ->once ())
67+ $ this ->controller ->Auth ->expects ($ this ->once ())
5968 ->method ('user ' )
6069 ->with (null )
6170 ->will ($ this ->returnValue ($ user ));
@@ -76,7 +85,7 @@ public function testIsAuthorizedInvalid() {
7685 'id ' => 1 ,
7786 'role_id ' => 1 ,
7887 ];
79- $ this ->AuthUser ->Auth ->expects ($ this ->once ())
88+ $ this ->controller ->Auth ->expects ($ this ->once ())
8089 ->method ('user ' )
8190 ->with (null )
8291 ->will ($ this ->returnValue ($ user ));
@@ -95,7 +104,7 @@ public function testIsAuthorizedInvalid() {
95104 public function testIsAuthorizedNotLoggedIn () {
96105 $ user = [
97106 ];
98- $ this ->AuthUser ->Auth ->expects ($ this ->once ())
107+ $ this ->controller ->Auth ->expects ($ this ->once ())
99108 ->method ('user ' )
100109 ->with (null )
101110 ->will ($ this ->returnValue ($ user ));
@@ -180,7 +189,7 @@ public function testEmptyAuthSession() {
180189 * @return void
181190 */
182191 public function testId () {
183- $ this ->AuthUser ->Auth ->expects ($ this ->once ())
192+ $ this ->controller ->Auth ->expects ($ this ->once ())
184193 ->method ('user ' )
185194 ->with (null )
186195 ->will ($ this ->returnValue (['id ' => '1 ' ]));
@@ -192,7 +201,7 @@ public function testId() {
192201 * @return void
193202 */
194203 public function testIsMe () {
195- $ this ->AuthUser ->Auth ->expects ($ this ->any ())
204+ $ this ->controller ->Auth ->expects ($ this ->any ())
196205 ->method ('user ' )
197206 ->with (null )
198207 ->will ($ this ->returnValue (['id ' => '1 ' ]));
@@ -209,7 +218,7 @@ public function testIsMe() {
209218 * @return void
210219 */
211220 public function testUser () {
212- $ this ->AuthUser ->Auth ->expects ($ this ->any ())
221+ $ this ->controller ->Auth ->expects ($ this ->any ())
213222 ->method ('user ' )
214223 ->with (null )
215224 ->will ($ this ->returnValue (['id ' => '1 ' , 'username ' => 'foo ' ]));
@@ -225,7 +234,7 @@ public function testUser() {
225234 public function testRoles () {
226235 $ this ->AuthUser ->setConfig ('multiRole ' , true );
227236
228- $ this ->AuthUser ->Auth ->expects ($ this ->once ())
237+ $ this ->controller ->Auth ->expects ($ this ->once ())
229238 ->method ('user ' )
230239 ->will ($ this ->returnValue (['id ' => '1 ' , 'Roles ' => ['1 ' , '2 ' ]]));
231240
@@ -238,7 +247,7 @@ public function testRoles() {
238247 public function testRolesDeep () {
239248 $ this ->AuthUser ->setConfig ('multiRole ' , true );
240249
241- $ this ->AuthUser ->Auth ->expects ($ this ->once ())
250+ $ this ->controller ->Auth ->expects ($ this ->once ())
242251 ->method ('user ' )
243252 ->with (null )
244253 ->will ($ this ->returnValue (['id ' => '1 ' , 'Roles ' => [['id ' => '1 ' ], ['id ' => '2 ' ]]]));
@@ -252,7 +261,7 @@ public function testRolesDeep() {
252261 public function testHasRole () {
253262 $ this ->AuthUser ->setConfig ('multiRole ' , true );
254263
255- $ this ->AuthUser ->Auth ->expects ($ this ->exactly (3 ))
264+ $ this ->controller ->Auth ->expects ($ this ->exactly (3 ))
256265 ->method ('user ' )
257266 ->with (null )
258267 ->will ($ this ->returnValue (['id ' => '1 ' , 'Roles ' => [['id ' => '1 ' ], ['id ' => '2 ' ]]]));
@@ -271,7 +280,7 @@ public function testHasRole() {
271280 public function testHasRoles () {
272281 $ this ->AuthUser ->setConfig ('multiRole ' , true );
273282
274- $ this ->AuthUser ->Auth ->expects ($ this ->exactly (6 ))
283+ $ this ->controller ->Auth ->expects ($ this ->exactly (6 ))
275284 ->method ('user ' )
276285 ->with (null )
277286 ->will ($ this ->returnValue (['id ' => '1 ' , 'Roles ' => [['id ' => '1 ' ], ['id ' => '2 ' ]]]));
0 commit comments