2
2
3
3
namespace App \Tests \users ;
4
4
5
- use ApiPlatform \Core \Bridge \Symfony \Bundle \Test \ApiTestCase ;
6
- use ApiPlatform \Core \Bridge \Symfony \Bundle \Test \Client ;
5
+ use App \DataFixtures \UserSeeder ;
6
+ use App \Entity \User ;
7
+ use App \Tests \EtuUTTApiTestCase ;
7
8
use Symfony \Component \HttpFoundation \Response ;
8
9
9
- class GetUsers extends ApiTestCase
10
+ class GetUsers extends EtuUTTApiTestCase
10
11
{
11
12
12
- private $ responseWithNoParameter = array ();
13
- private $ lastPage ;
14
-
15
13
public function testNotConnected () : void
16
14
{
17
15
$ client = static ::createClient ();
@@ -21,83 +19,88 @@ public function testNotConnected() : void
21
19
22
20
public function testNoParameter () : void
23
21
{
24
- // TODO : update this code : we need to fill the database, and then run this test
25
- /* $client = static::createClient();
26
- $client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'admin ' ]]);
22
+ $ this -> loadFixtures ( new UserSeeder ());
23
+ $ client = static ::createClient ();
24
+ $ client ->setDefaultOptions ([ 'headers ' => [ 'CAS-LOGIN ' => 'test ' ]]);
27
25
$ crawler = $ client ->request ('GET ' , '/users ' );
28
26
$ response = json_decode ($ crawler ->getContent ());
29
27
$ this ->assertResponseStatusCodeSame (Response::HTTP_OK );
30
28
$ this ->assertIsArray ($ response ->{'hydra:member ' });
31
29
$ this ->assertNotEmpty ($ response ->{'hydra:member ' });
32
- $this->responseWithNoParameter['member'] = array();
30
+ $ expectedResults = $ this ->em ->createQueryBuilder ()
31
+ ->select ('user.id, user.login, user.firstName, user.lastName, infos.avatar ' )
32
+ ->from (User::class, 'user ' )
33
+ ->innerJoin ('user.infos ' , 'infos ' )
34
+ ->addOrderBy ('user.id ' )
35
+ ->setMaxResults (10 )
36
+ ->getQuery ()
37
+ ->execute ();
33
38
foreach ($ response ->{'hydra:member ' } as $ i => $ member ) {
34
- $this->assertMatchesRegularExpression("/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/" , $member->{'id'});
35
- $this->assertNotEmpty( $member->{'login'});
36
- $this->assertNotEmpty($member->{'firstName'});
37
- $this->assertNotEmpty($member->{'lastName'});
38
- $this->assertMatchesRegularExpression("/^https?:\\/\\/[\w.-]*\\.[\w-].+$/" , $member->{'infos'}->{'avatar'});
39
+ $ this ->assertEquals ( $ expectedResults [ $ i ][ ' id ' ]-> jsonSerialize () , $ member ->{'id ' });
40
+ $ this ->assertEquals ( $ expectedResults [ $ i ][ ' login ' ], $ member ->{'login ' });
41
+ $ this ->assertNotEmpty ($ expectedResults [ $ i ][ ' firstName ' ], $ member ->{'firstName ' });
42
+ $ this ->assertNotEmpty ($ expectedResults [ $ i ][ ' lastName ' ], $ member ->{'lastName ' });
43
+ $ this ->assertEquals ( $ expectedResults [ $ i ][ ' avatar ' ] , $ member ->{'infos ' }->{'avatar ' });
39
44
$ this ->assertIsArray ($ member ->{'mailsPhones ' });
40
- $returnedMember = array();
41
- $returnedMember['login'] = $member->{'login'};
42
- $returnedMember['firstName'] = $member->{'firstName'};
43
- $returnedMember['lastName'] = $member->{'lastName'};
44
- $returnedMember['avatar'] = $member->{'infos'}->{'avatar'};
45
- $this->responseWithNoParameter['member'][$i] = $returnedMember;
46
45
}
47
46
$ this ->assertIsNumeric ($ response ->{'hydra:totalItems ' });
48
47
$ this ->assertTrue ($ response ->{'hydra:totalItems ' } >= 0 );
49
- $this->responseWithNoParameter['totalItems'] = $response->{'hydra:totalItems'};
50
48
$ matches = array ();
51
49
$ this ->assertEquals (1 , preg_match ("/^ \\/users \\?page=(?<id>\d+)+$/ " , $ response ->{'hydra:view ' }->{'@id ' }, $ matches ));
52
50
$ this ->assertArrayHasKey ('id ' , $ matches );
53
- $this->responseWithNoParameter['view:id'] = $response->{'hydra:view'}->{'@id'};
54
51
$ this ->assertEquals (1 , preg_match ("/^ \\/users \\?page=(?<id>\d+)+$/ " , $ response ->{'hydra:view ' }->{'hydra:next ' }, $ matches ));
55
52
$ this ->assertArrayHasKey ('id ' , $ matches );
56
- $this->responseWithNoParameter['view:next'] = $response->{'hydra:view'}->{'hydra:next'};
57
53
$ this ->assertEquals (1 , preg_match ("/^ \\/users \\?page=(?<id>\d+)+$/ " , $ response ->{'hydra:view ' }->{'hydra:last ' }, $ matches ));
58
54
$ this ->assertArrayHasKey ('id ' , $ matches );
59
- $this->responseWithNoParameter['view:last'] = $response->{'hydra:view'}->{'hydra:last'};
60
- $this->lastPage = $matches['id'];*/
61
55
}
62
56
63
- public function testParameter1 () : void
57
+ public function testPageParameter () : void
64
58
{
65
- // TODO : fill the database and then run this test
66
- /*$client = static::createClient();
67
- $client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'admin' ]]);
68
- $crawler = $client->request('GET', '/users?page=1');
69
- $response = json_decode($crawler->getContent());
70
- $this->assertResponseStatusCodeSame(Response::HTTP_OK);
71
- foreach ($this->responseWithNoParameter['member'] as $i => $member) {
72
- $this->assertEquals($member['login'], $response->{'hydra:member'}[$i]->{'login'});
73
- $this->assertEquals($member['firstName'], $response->{'hydra:member'}[$i]->{'firstName'});
74
- $this->assertEquals($member['lastName'], $response->{'hydra:member'}[$i]->{'lastName'});
75
- $this->assertEquals($member['avatar'], $response->{'hydra:member'}[$i]->{'infos'}->{'avatar'});
59
+ $ this ->loadFixtures (new UserSeeder ());
60
+ $ client = static ::createClient ();
61
+ $ client ->setDefaultOptions ([ 'headers ' => [ 'CAS-LOGIN ' => 'test ' ]]);
62
+ $ expectedResults = $ this ->em ->createQueryBuilder ()
63
+ ->select ('user.id, user.login, user.firstName, user.lastName, infos.avatar ' )
64
+ ->from (User::class, 'user ' )
65
+ ->innerJoin ('user.infos ' , 'infos ' )
66
+ ->addOrderBy ('user.id ' )
67
+ ->getQuery ()
68
+ ->execute ();
69
+ $ expectedResultsCount = count ($ expectedResults );
70
+ $ lastPage = (int ) (($ expectedResultsCount - 1 ) / 10 ) + 1 ;
71
+ $ page = 0 ;
72
+ foreach ($ expectedResults as $ i => $ expectedResult ) {
73
+ if ($ i % 10 == 0 ) {
74
+ $ page ++;
75
+ $ crawler = $ client ->request ('GET ' , '/users?page= ' .$ page );
76
+ $ response = json_decode ($ crawler ->getContent ());
77
+ $ this ->assertResponseStatusCodeSame (Response::HTTP_OK );
78
+ $ this ->assertEquals ($ expectedResultsCount , $ response ->{'hydra:totalItems ' });
79
+ $ this ->assertEquals ("/users?page= " .$ page , $ response ->{'hydra:view ' }->{'@id ' });
80
+ if ($ page > 1 ) {
81
+ $ this ->assertEquals ("/users?page= " .($ page - 1 ), $ response ->{'hydra:view ' }->{'hydra:previous ' });
82
+ }
83
+ if ($ page < $ lastPage ) {
84
+ $ this ->assertEquals ("/users?page= " .($ page + 1 ), $ response ->{'hydra:view ' }->{'hydra:next ' });
85
+ }
86
+ $ this ->assertEquals ("/users?page= " .$ lastPage , $ response ->{'hydra:view ' }->{'hydra:last ' });
87
+ }
88
+ $ this ->assertEquals ($ expectedResult ['login ' ], $ response ->{'hydra:member ' }[$ i %10 ]->{'login ' });
89
+ $ this ->assertEquals ($ expectedResult ['id ' ]->jsonSerialize (), $ response ->{'hydra:member ' }[$ i %10 ]->{'id ' });
90
+ $ this ->assertEquals ($ expectedResult ['firstName ' ], $ response ->{'hydra:member ' }[$ i %10 ]->{'firstName ' });
91
+ $ this ->assertEquals ($ expectedResult ['lastName ' ], $ response ->{'hydra:member ' }[$ i %10 ]->{'lastName ' });
92
+ $ this ->assertEquals ($ expectedResult ['avatar ' ], $ response ->{'hydra:member ' }[$ i %10 ]->{'infos ' }->{'avatar ' });
76
93
}
77
- $this->assertEquals($this->responseWithNoParameter['totalItems'], $response->{'hydra:totalItems'});
78
- $this->assertEquals($this->responseWithNoParameter['view:id'], $response->{'hydra:view'}->{'@id'});
79
- $this->assertEquals($this->responseWithNoParameter['view:next'], $response->{'hydra:view'}->{'hydra:next'});
80
- $this->assertEquals($this->responseWithNoParameter['view:last'], $response->{'hydra:view'}->{'hydra:last'});*/
81
- }
82
94
83
- public function testAllParameters () : void
84
- {
85
- // TODO : fill the database and then run this test
86
- /*$client = static::createClient();
87
- $client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'admin' ]]);
88
- for ($i = 1; $i <= $this->lastPage; $i++) {
89
- $client->request('GET', '/users?page='.$i);
90
- $this->assertResponseStatusCodeSame(Response::HTTP_OK);
91
- }*/
92
95
}
93
96
94
97
public function testOutOfRangeParameters () : void
95
98
{
96
99
$ client = static ::createClient ();
97
- $ client ->setDefaultOptions ([ 'headers ' => [ 'CAS-LOGIN ' => 'admin ' ]]);
100
+ $ client ->setDefaultOptions ([ 'headers ' => [ 'CAS-LOGIN ' => 'test ' ]]);
98
101
$ client ->request ('GET ' , '/users?page=0 ' );
99
102
$ this ->assertResponseStatusCodeSame (Response::HTTP_BAD_REQUEST );
100
- $ crawler = $ client ->request ('GET ' , '/users?page=100 ' ); // TODO : use database filling and not hard code the value of parameter 'page'
103
+ $ crawler = $ client ->request ('GET ' , '/users?page=2 ' );
101
104
$ response = json_decode ($ crawler ->getContent ());
102
105
$ this ->assertResponseStatusCodeSame (Response::HTTP_OK );
103
106
$ this ->assertEmpty ($ response ->{'hydra:member ' });
@@ -106,11 +109,11 @@ public function testOutOfRangeParameters() : void
106
109
public function testWrongTypeParameter () : void
107
110
{
108
111
$ client = static ::createClient ();
109
- $ client ->setDefaultOptions ([ 'headers ' => [ 'CAS-LOGIN ' => 'admin ' ]]);
110
- $ crawler = $ client ->request ('GET ' , '/users?page=1 .5 ' );
112
+ $ client ->setDefaultOptions ([ 'headers ' => [ 'CAS-LOGIN ' => 'test ' ]]);
113
+ $ crawler = $ client ->request ('GET ' , '/users?page=2 .5 ' );
111
114
$ response = json_decode ($ crawler ->getContent ());
112
115
$ this ->assertResponseStatusCodeSame (Response::HTTP_OK );
113
- $ this ->assertEquals ( ' /users?page=1 ' , $ response ->{'hydra:view ' }->{ ' @id ' });
116
+ $ this ->assertEmpty ( $ response ->{'hydra:member ' });
114
117
$ client ->request ('GET ' , '/users?page=abc ' );
115
118
$ this ->assertResponseStatusCodeSame (Response::HTTP_BAD_REQUEST );
116
119
}
0 commit comments