22
33namespace ApiSkeletonsTest \Laravel \Doctrine \ApiKey \Feature \Repository ;
44
5+ use ApiSkeletons \Laravel \Doctrine \ApiKey \Entity \AdminEvent ;
56use ApiSkeletons \Laravel \Doctrine \ApiKey \Entity \ApiKey ;
67use ApiSkeletons \Laravel \Doctrine \ApiKey \Entity \Scope ;
78use ApiSkeletons \Laravel \Doctrine \ApiKey \Exception \ApiKeyDoesNotHaveScope ;
@@ -25,6 +26,14 @@ public function testGenerate(): void
2526 $ this ->assertEquals ('testing ' , $ apiKey ->getName ());
2627 $ this ->assertEquals (64 , strlen ($ apiKey ->getApiKey ()));
2728 $ this ->assertEquals (true , $ apiKey ->getIsActive ());
29+
30+ $ adminEvents = $ entityManager ->getRepository (AdminEvent::class)
31+ ->findAll ();
32+
33+ foreach ($ adminEvents as $ adminEvent ) {
34+ $ this ->assertEquals ('generate ' , $ adminEvent ->getEvent ());
35+ $ this ->assertEquals ($ apiKey , $ adminEvent ->getApiKey ());
36+ }
2837 }
2938
3039 public function testGenerateValidatesName (): void
@@ -50,7 +59,7 @@ public function testGenerateDoesNotCollideNames(): void
5059 $ result = $ repository ->generate ('testing ' );
5160 }
5261
53- public function testSetStatus (): void
62+ public function testDeativate (): void
5463 {
5564 $ entityManager = $ this ->createDatabase (app ('em ' ));
5665 $ repository = $ entityManager ->getRepository (ApiKey::class);
@@ -66,6 +75,49 @@ public function testSetStatus(): void
6675
6776 $ this ->assertGreaterThan ($ beforeSetStatus , $ apiKey ->getStatusAt ());
6877 $ this ->assertEquals (false , $ apiKey ->getIsActive ());
78+
79+ $ adminEvents = $ entityManager ->getRepository (AdminEvent::class)
80+ ->findBy ([
81+ 'apiKey ' => $ apiKey ,
82+ 'event ' => 'deactivate ' ,
83+ ]);
84+
85+ $ this ->assertEquals (1 , count ($ adminEvents ));
86+ foreach ($ adminEvents as $ adminEvent ) {
87+ $ this ->assertEquals ('deactivate ' , $ adminEvent ->getEvent ());
88+ $ this ->assertEquals ($ apiKey , $ adminEvent ->getApiKey ());
89+ }
90+ }
91+
92+ public function testActivate (): void
93+ {
94+ $ entityManager = $ this ->createDatabase (app ('em ' ));
95+ $ repository = $ entityManager ->getRepository (ApiKey::class);
96+ $ apiKey = $ repository ->generate ('testing ' );
97+ $ apiKey ->setIsActive (false );
98+ $ entityManager ->flush ();
99+
100+ $ beforeSetStatus = new DateTime ();
101+
102+ $ this ->assertEquals (false , $ apiKey ->getIsActive ());
103+
104+ $ repository ->updateActive ($ apiKey , true );
105+ $ entityManager ->flush ();
106+
107+ $ this ->assertGreaterThan ($ beforeSetStatus , $ apiKey ->getStatusAt ());
108+ $ this ->assertEquals (true , $ apiKey ->getIsActive ());
109+
110+ $ adminEvents = $ entityManager ->getRepository (AdminEvent::class)
111+ ->findBy ([
112+ 'apiKey ' => $ apiKey ,
113+ 'event ' => 'activate ' ,
114+ ]);
115+
116+ $ this ->assertEquals (1 , count ($ adminEvents ));
117+ foreach ($ adminEvents as $ adminEvent ) {
118+ $ this ->assertEquals ('activate ' , $ adminEvent ->getEvent ());
119+ $ this ->assertEquals ($ apiKey , $ adminEvent ->getApiKey ());
120+ }
69121 }
70122
71123 public function testAddScope (): void
@@ -91,6 +143,18 @@ public function testAddScope(): void
91143 }
92144
93145 $ this ->assertTrue ($ found );
146+
147+ $ adminEvents = $ entityManager ->getRepository (AdminEvent::class)
148+ ->findBy ([
149+ 'apiKey ' => $ apiKey ,
150+ 'event ' => 'add scope: ' . $ scope ->getName (),
151+ ]);
152+
153+ $ this ->assertEquals (1 , count ($ adminEvents ));
154+ foreach ($ adminEvents as $ adminEvent ) {
155+ $ this ->assertEquals ('add scope: ' . $ scope ->getName (), $ adminEvent ->getEvent ());
156+ $ this ->assertEquals ($ apiKey , $ adminEvent ->getApiKey ());
157+ }
94158 }
95159
96160 public function testCannotAddSameScopeTwice (): void
@@ -131,6 +195,18 @@ public function testRemoveScope(): void
131195 $ entityManager ->flush ();
132196
133197 $ this ->assertEquals (0 , sizeof ($ apiKey ->getScopes ()));
198+
199+ $ adminEvents = $ entityManager ->getRepository (AdminEvent::class)
200+ ->findBy ([
201+ 'apiKey ' => $ apiKey ,
202+ 'event ' => 'remove scope: ' . $ scope ->getName (),
203+ ]);
204+
205+ $ this ->assertEquals (1 , count ($ adminEvents ));
206+ foreach ($ adminEvents as $ adminEvent ) {
207+ $ this ->assertEquals ('remove scope: ' . $ scope ->getName (), $ adminEvent ->getEvent ());
208+ $ this ->assertEquals ($ apiKey , $ adminEvent ->getApiKey ());
209+ }
134210 }
135211
136212 public function testRemoveScopeWhichIsNotAssigned (): void
0 commit comments