Optimize test

This commit is contained in:
Pig Fang 2019-03-19 15:30:52 +08:00
parent 749339f331
commit d8295dff33

View File

@ -166,53 +166,28 @@ class SkinlibControllerTest extends TestCase
->create() ->create()
->merge($steves); ->merge($steves);
$skins = $steves->merge($alexs); $skins = $steves->merge($alexs);
$page1 = $skins
->sortByDesc('upload_at')
->values()
->map(function ($skin) {
return $skin->tid;
})
->forPage(1, 20);
$items = $this->getJson('/skinlib/data') $items = $this->getJson('/skinlib/data')
->assertJson([ ->assertJson([
'current_uid' => 0, 'current_uid' => 0,
'total_pages' => 2, 'total_pages' => 2,
]) ])
->decodeResponseJson('items'); ->decodeResponseJson('items');
$items = array_map(function ($item) {
return $item['tid'];
}, $items);
$this->assertCount(20, $items); $this->assertCount(20, $items);
$this->assertArraySubset($page1, $items);
$items = $this->getJson('/skinlib/data?page=-5') $items = $this->getJson('/skinlib/data?page=-5')
->assertJson([ ->assertJson([
'current_uid' => 0, 'current_uid' => 0,
'total_pages' => 2, 'total_pages' => 2,
]) ])
->decodeResponseJson('items'); ->decodeResponseJson('items');
$items = array_map(function ($item) {
return $item['tid'];
}, $items);
$this->assertCount(20, $items); $this->assertCount(20, $items);
$this->assertArraySubset($page1, $items); $page2Count = $skins->forPage(2, 20)->count();
$page2 = $skins
->sortByDesc('upload_at')
->map(function ($skin) {
return $skin->tid;
})
->forPage(2, 20)
->values();
$items = $this->getJson('/skinlib/data?page=2') $items = $this->getJson('/skinlib/data?page=2')
->assertJson([ ->assertJson([
'current_uid' => 0, 'current_uid' => 0,
'total_pages' => 2, 'total_pages' => 2,
]) ])
->decodeResponseJson('items'); ->decodeResponseJson('items');
$items = array_map(function ($item) {
return $item['tid'];
}, $items);
$this->assertCount(5, $items); $this->assertCount(5, $items);
$this->assertArraySubset($page2, $items);
$this->getJson('/skinlib/data?page=8') $this->getJson('/skinlib/data?page=8')
->assertJson([ ->assertJson([
'items' => [], 'items' => [],
@ -225,29 +200,15 @@ class SkinlibControllerTest extends TestCase
'total_pages' => 2, 'total_pages' => 2,
]) ])
->decodeResponseJson('items'); ->decodeResponseJson('items');
$items = array_map(function ($item) { $this->assertCount($page2Count, $items);
return $item['tid']; $page3Count = $skins->forPage(3, 8)->count();
}, $items);
$this->assertCount($page2->count(), $items);
$this->assertArraySubset($page2, $items);
$page3 = $skins
->sortByDesc('upload_at')
->map(function ($skin) {
return $skin->tid;
})
->forPage(3, 8)
->values();
$items = $this->getJson('/skinlib/data?page=3&items_per_page=8') $items = $this->getJson('/skinlib/data?page=3&items_per_page=8')
->assertJson([ ->assertJson([
'current_uid' => 0, 'current_uid' => 0,
'total_pages' => 4, 'total_pages' => 4,
]) ])
->decodeResponseJson('items'); ->decodeResponseJson('items');
$items = array_map(function ($item) { $this->assertCount($page3Count, $items);
return $item['tid'];
}, $items);
$this->assertCount($page3->count(), $items);
$this->assertArraySubset($page3, $items);
// Add some private textures // Add some private textures
$uploader = factory(User::class)->create(); $uploader = factory(User::class)->create();
@ -257,23 +218,15 @@ class SkinlibControllerTest extends TestCase
->create(['public' => false, 'uploader' => $uploader->uid]); ->create(['public' => false, 'uploader' => $uploader->uid]);
// If not logged in, private textures should not be shown // If not logged in, private textures should not be shown
$paged = $skins
->sortByDesc('upload_at')
->map(function ($skin) {
return $skin->tid;
})
->values()
->forPage(1, 20);
$items = $this->getJson('/skinlib/data') $items = $this->getJson('/skinlib/data')
->assertJson([ ->assertJson([
'current_uid' => 0, 'current_uid' => 0,
'total_pages' => 2, 'total_pages' => 2,
]) ])
->decodeResponseJson('items'); ->decodeResponseJson('items');
$items = array_map(function ($item) { $this->assertTrue(collect($items)->every(function ($item) {
return $item['tid']; return $item['public'] == true;
}, $items); }));
$this->assertArraySubset($paged, $items);
// Other users should not see someone's private textures // Other users should not see someone's private textures
$items = $this->actAs($otherUser) $items = $this->actAs($otherUser)
@ -300,14 +253,6 @@ class SkinlibControllerTest extends TestCase
]); ]);
// Uploader can see his private textures // Uploader can see his private textures
$withPrivate = $skins
->merge($private)
->sortByDesc('upload_at')
->map(function ($skin) {
return $skin->tid;
})
->values()
->forPage(1, 20);
$items = $this->actAs($uploader) $items = $this->actAs($uploader)
->getJson('/skinlib/data') ->getJson('/skinlib/data')
->assertJson([ ->assertJson([
@ -315,10 +260,9 @@ class SkinlibControllerTest extends TestCase
'total_pages' => 2, 'total_pages' => 2,
]) ])
->decodeResponseJson('items'); ->decodeResponseJson('items');
$items = array_map(function ($item) { $this->assertTrue(collect($items)->contains(function ($item) {
return $item['tid']; return $item['public'] == false;
}, $items); }));
$this->assertArraySubset($withPrivate, $items);
// Administrators can see private textures // Administrators can see private textures
$admin = factory(User::class, 'admin')->create(); $admin = factory(User::class, 'admin')->create();
@ -329,10 +273,9 @@ class SkinlibControllerTest extends TestCase
'total_pages' => 2, 'total_pages' => 2,
]) ])
->decodeResponseJson('items'); ->decodeResponseJson('items');
$items = array_map(function ($item) { $this->assertTrue(collect($items)->contains(function ($item) {
return $item['tid']; return $item['public'] == false;
}, $items); }));
$this->assertArraySubset($withPrivate, $items);
} }
public function testShow() public function testShow()