Add tests for Filters API

This commit is contained in:
Pig Fang 2019-12-21 10:41:38 +08:00
parent 4c3b9f0cb6
commit ab24dfe5bf
6 changed files with 69 additions and 0 deletions

42
tests/Fakes/Filter.php Normal file
View File

@ -0,0 +1,42 @@
<?php
namespace Tests\Fakes;
use App\Services\Filter as BaseFilter;
use PHPUnit\Framework\Assert;
class Filter extends BaseFilter
{
protected $applied = [];
public function apply(string $hook, $init, $args = [])
{
$this->applied[$hook] = array_merge([$init], $args);
return parent::apply($hook, $init, $args);
}
public static function fake(): Filter
{
$fake = new self();
app()->instance(BaseFilter::class, $fake);
return $fake;
}
public function assertApplied(string $hook, $predicate = null)
{
Assert::assertArrayHasKey(
$hook, $this->applied,
"Expected Filter '$hook' was not applied."
);
if (!empty($predicate)) {
Assert::assertTrue(
call_user_func_array($predicate, $this->applied[$hook]),
"Arguments of Filter '$hook' does not satisfies the predicate."
);
}
}
}

View File

@ -23,6 +23,14 @@ class AdminControllerTest extends TestCase
$this->actAs('admin');
}
public function testIndex()
{
$filter = Fakes\Filter::fake();
$this->get('/admin')->assertSuccessful();
$filter->assertApplied('grid:admin.index');
}
public function testChartData()
{
factory(User::class)->create();
@ -126,6 +134,7 @@ class AdminControllerTest extends TestCase
'a' => new Plugin('', ['title' => 'MyPlugin', 'version' => '0.0.0']),
]));
});
$filter = Fakes\Filter::fake();
$this->get('/admin/status')
->assertSee(PHP_VERSION)
@ -133,6 +142,7 @@ class AdminControllerTest extends TestCase
->assertSee('(1)')
->assertSee('MyPlugin')
->assertSee('0.0.0');
$filter->assertApplied('grid:admin.status');
}
public function testUsers()

View File

@ -24,7 +24,10 @@ class ClosetControllerTest extends TestCase
public function testIndex()
{
$filter = Fakes\Filter::fake();
$this->get('/user/closet')->assertViewIs('user.closet');
$filter->assertApplied('grid:user.closet');
}
public function testGetClosetData()

View File

@ -23,7 +23,10 @@ class PlayerControllerTest extends TestCase
public function testIndex()
{
$filter = Fakes\Filter::fake();
$this->get('/user/player?pid=5')->assertViewIs('user.player');
$filter->assertApplied('grid:user.player');
}
public function testListAll()

View File

@ -266,6 +266,7 @@ class SkinlibControllerTest extends TestCase
public function testShow()
{
Storage::fake('textures');
$filter = Fakes\Filter::fake();
// Cannot find texture
$this->get('/skinlib/show/1')
@ -287,6 +288,7 @@ class SkinlibControllerTest extends TestCase
$texture = factory(Texture::class)->create();
Storage::disk('textures')->put($texture->hash, '');
$this->get('/skinlib/show/'.$texture->tid)->assertViewHas('texture');
$filter->assertApplied('grid:skinlib.show');
// Guest should not see private texture
$uploader = factory(User::class)->create();
@ -360,7 +362,10 @@ class SkinlibControllerTest extends TestCase
public function testUpload()
{
$filter = Fakes\Filter::fake();
$this->actAs('normal')->get('/skinlib/upload');
$filter->assertApplied('grid:skinlib.upload');
option(['texture_name_regexp' => 'abc']);
$this->get('/skinlib/upload')->assertViewHas('extra');

View File

@ -36,6 +36,8 @@ class UserControllerTest extends TestCase
public function testIndex()
{
$filter = Fakes\Filter::fake();
$user = factory(User::class)->create();
factory(\App\Models\Player::class)->create(['uid' => $user->uid]);
@ -44,6 +46,7 @@ class UserControllerTest extends TestCase
->assertViewHas('statistics')
->assertSee((new Parsedown())->text(option_localized('announcement')))
->assertSee((string) $user->score);
$filter->assertApplied('grid:user.index');
$unverified = factory(User::class)->create(['verified' => false]);
$this->actingAs($unverified)
@ -218,7 +221,10 @@ class UserControllerTest extends TestCase
public function testProfile()
{
$filter = Fakes\Filter::fake();
$this->actAs('normal')->get('/user/profile')->assertViewIs('user.profile');
$filter->assertApplied('grid:user.profile');
}
public function testHandleProfile()