22
33namespace Tests \Jobs ;
44
5- use Illuminate \Foundation \Testing \DatabaseTransactions ;
5+ use Illuminate \Database \DatabaseManager ;
6+ use Illuminate \Foundation \Testing \RefreshDatabase ;
67use Tests \TestCase ;
78use App \User ;
89use App \Wiki ;
1617
1718class PlatformStatsSummaryJobTest extends TestCase
1819{
19- use DatabaseTransactions ;
20+ use RefreshDatabase ;
2021
2122 private $ numWikis = 5 ;
2223 private $ wikis = [];
@@ -30,7 +31,6 @@ protected function setUp(): void {
3031 DB ::connection ('mysql ' )->getPdo ()->exec ("DROP DATABASE IF EXISTS {$ this ->db_name }{$ n }; " );
3132 }
3233 $ this ->seedWikis ();
33- $ this ->manager = $ this ->app ->make ('db ' );
3434 }
3535
3636 protected function tearDown (): void {
@@ -42,15 +42,14 @@ protected function tearDown(): void {
4242 }
4343
4444 private function seedWikis () {
45- $ manager = $ this ->app ->make ('db ' );
4645 for ($ n = 0 ; $ n < $ this ->numWikis ; $ n ++ ) {
4746
4847 $ user = User::factory ()->create (['verified ' => true ]);
4948 $ wiki = Wiki::factory ()->create ( [ 'deleted_at ' => null ] );
5049 WikiManager::factory ()->create (['wiki_id ' => $ wiki ->id , 'user_id ' => $ user ->id ]);
5150
5251 $ job = new ProvisionWikiDbJob ($ this ->db_prefix . $ n , $ this ->db_name . $ n , null );
53- $ job ->handle ($ manager );
52+ $ job ->handle ($ this -> app -> make (DatabaseManager::class) );
5453
5554 $ wikiDb = WikiDb::whereName ($ this ->db_name .$ n )->first ();
5655 $ wikiDb ->update ( ['wiki_id ' => $ wiki ->id ] );
@@ -64,15 +63,13 @@ private function seedWikis() {
6463 }
6564 public function testQueryGetsStats ()
6665 {
67- $ manager = $ this ->app ->make ('db ' );
68-
6966 $ mockJob = $ this ->createMock (Job::class);
7067 $ mockJob ->expects ($ this ->never ())->method ('fail ' );
7168
7269 $ job = new PlatformStatsSummaryJob ();
7370 $ job ->setJob ($ mockJob );
7471
75- $ job ->handle ($ manager );
72+ $ job ->handle ($ this -> app -> make (DatabaseManager::class) );
7673 }
7774
7875 public function testGroupings ()
@@ -84,10 +81,11 @@ public function testGroupings()
8481 $ job ->setJob ($ mockJob );
8582
8683 $ testWikis = [
87- Wiki::factory ()->create ( [ 'deleted_at ' => null , 'domain ' => 'wiki1.com ' , 'created_at ' => Carbon::now ()->subMinutes (4 )->timestamp ] ),
88- Wiki::factory ()->create ( [ 'deleted_at ' => null , 'domain ' => 'wiki2.com ' , 'created_at ' => Carbon::now ()->subHours (72 )->timestamp ] ),
89- Wiki::factory ()->create ( [ 'deleted_at ' => Carbon::now ()->subDays (90 )->timestamp , 'domain ' => 'wiki3.com ' , 'created_at ' => Carbon::now ()->subHours (72 )->timestamp ] ),
90- Wiki::factory ()->create ( [ 'deleted_at ' => null , 'domain ' => 'wiki4.com ' , 'created_at ' => Carbon::now ()->subYears (6 )->timestamp ] )
84+ Wiki::factory ()->create ( [ 'deleted_at ' => null , 'domain ' => 'wiki1.com ' ] ),
85+ Wiki::factory ()->create ( [ 'deleted_at ' => null , 'domain ' => 'wiki2.com ' ] ),
86+ Wiki::factory ()->create ( [ 'deleted_at ' => Carbon::now ()->subDays (90 )->timestamp , 'domain ' => 'wiki3.com ' ] ),
87+ Wiki::factory ()->create ( [ 'deleted_at ' => null , 'domain ' => 'wiki4.com ' ] )
88+
9189 ];
9290
9391 foreach ($ testWikis as $ wiki ) {
@@ -145,13 +143,8 @@ public function testGroupings()
145143 ],
146144 ];
147145
148- $ users = [
149- User::factory ()->create ([ "created_at " => Carbon::now ()->subDays (90 )->timestamp ]),
150- User::factory ()->create ([ "created_at " => Carbon::now ()->subHours (1 )->timestamp ]),
151- User::factory ()->create ([ "created_at " => Carbon::now ()->subHours (48 )->timestamp ]),
152- ];
153146
154- $ groups = $ job ->prepareStats ($ stats , $ testWikis, $ users );
147+ $ groups = $ job ->prepareStats ($ stats , $ testWikis );
155148
156149 $ this ->assertEquals (
157150 [
@@ -164,15 +157,34 @@ public function testGroupings()
164157 "total_non_deleted_active_users " => 1 ,
165158 "total_non_deleted_pages " => 2 ,
166159 "total_non_deleted_edits " => 1 ,
167- "platform_summary_version " => "v1 " ,
168- "wikis_created_PT24H " => 1 ,
169- "wikis_created_P30D " => 2 ,
170- "users_created_PT24H " => 1 ,
171- "users_created_P30D " => 2 ,
160+ "platform_summary_version " => "v1 "
172161 ],
173162 $ groups ,
174163 );
175164 }
176165
166+ function testCreationStats () {
167+ $ mockJob = $ this ->createMock (Job::class);
168+ $ mockJob ->expects ($ this ->never ())->method ('fail ' );
169+
170+ $ job = new PlatformStatsSummaryJob ();
171+ $ job ->setJob ($ mockJob );
172+
173+ $ testWikis = [];
174+ $ testUsers = [];
175+
176+ $ stats = $ job ->getCreationStats ();
177+
178+ $ this ->assertEquals (
179+ [
180+ 'wikis_created_PT24H ' => 0 ,
181+ 'wikis_created_P30D ' => 0 ,
182+ 'users_created_PT24H ' => 0 ,
183+ 'users_created_P30D ' => 0 ,
184+ ],
185+ $ stats ,
186+ );
187+
188+ }
177189
178190}
0 commit comments