diff --git a/tests/Fakes/Filter.php b/tests/Fakes/Filter.php new file mode 100644 index 00000000..79961adc --- /dev/null +++ b/tests/Fakes/Filter.php @@ -0,0 +1,42 @@ +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." + ); + } + } +} diff --git a/tests/HttpTest/ControllersTest/AdminControllerTest.php b/tests/HttpTest/ControllersTest/AdminControllerTest.php index 5280d4a8..672ccb85 100644 --- a/tests/HttpTest/ControllersTest/AdminControllerTest.php +++ b/tests/HttpTest/ControllersTest/AdminControllerTest.php @@ -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() diff --git a/tests/HttpTest/ControllersTest/ClosetControllerTest.php b/tests/HttpTest/ControllersTest/ClosetControllerTest.php index 50fcf585..790572c0 100644 --- a/tests/HttpTest/ControllersTest/ClosetControllerTest.php +++ b/tests/HttpTest/ControllersTest/ClosetControllerTest.php @@ -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() diff --git a/tests/HttpTest/ControllersTest/PlayerControllerTest.php b/tests/HttpTest/ControllersTest/PlayerControllerTest.php index 8845aff1..f29202b9 100644 --- a/tests/HttpTest/ControllersTest/PlayerControllerTest.php +++ b/tests/HttpTest/ControllersTest/PlayerControllerTest.php @@ -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() diff --git a/tests/HttpTest/ControllersTest/SkinlibControllerTest.php b/tests/HttpTest/ControllersTest/SkinlibControllerTest.php index 302253d3..c3770ce4 100644 --- a/tests/HttpTest/ControllersTest/SkinlibControllerTest.php +++ b/tests/HttpTest/ControllersTest/SkinlibControllerTest.php @@ -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'); diff --git a/tests/HttpTest/ControllersTest/UserControllerTest.php b/tests/HttpTest/ControllersTest/UserControllerTest.php index 9c240d61..793e8ccb 100644 --- a/tests/HttpTest/ControllersTest/UserControllerTest.php +++ b/tests/HttpTest/ControllersTest/UserControllerTest.php @@ -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()