From 0ef92a556571494b6dc54fa31602f24dc11aa997 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Fri, 17 Aug 2018 15:25:08 +0800 Subject: [PATCH] Update tests and composer autoload --- app/Http/Controllers/AuthController.php | 2 +- composer.json | 6 +- tests/AdminControllerTest.php | 2 + tests/AuthControllerTest.php | 10 +- tests/BrowserKitTestCase.php | 10 +- tests/ClosetControllerTest.php | 2 + tests/Concerns/InteractsWithCache.php | 117 ++++++++++++++++++ tests/HomeControllerTest.php | 3 + tests/MiddlewareTest.php | 7 +- tests/ModelsTest/ClosetTest.php | 2 + tests/ModelsTest/PlayerTest.php | 2 + tests/ModelsTest/UserTest.php | 2 + tests/PlayerControllerTest.php | 2 + tests/PluginControllerTest.php | 10 +- tests/ServicesTest/HookTest.php | 2 + tests/ServicesTest/MinecraftTest.php | 2 + .../RepositoriesTest/OptionRepositoryTest.php | 2 + .../RepositoriesTest/RepositoryTest.php | 2 + .../RepositoriesTest/UserRepositoryTest.php | 2 + tests/SetupControllerTest.php | 7 ++ tests/SkinlibControllerTest.php | 3 + tests/TestCase.php | 54 ++------ tests/TextureControllerTest.php | 5 + tests/UpdateControllerTest.php | 5 + tests/UserControllerTest.php | 3 + 25 files changed, 207 insertions(+), 57 deletions(-) create mode 100644 tests/Concerns/InteractsWithCache.php diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index ab8fb019..9afd5ff4 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -61,7 +61,7 @@ class AuthController extends Controller return json(trans('auth.login.success'), 0); } else { // Increase the counter - Cache::put($loginFailsCacheKey, ++$loginFails); + Cache::put($loginFailsCacheKey, ++$loginFails, 60); return json(trans('auth.validation.password'), 1, [ 'login_fails' => $loginFails diff --git a/composer.json b/composer.json index 7809924e..ec1199ba 100644 --- a/composer.json +++ b/composer.json @@ -40,9 +40,9 @@ ] }, "autoload-dev": { - "classmap": [ - "tests" - ] + "psr-4": { + "Tests\\": "tests/" + } }, "config": { "preferred-install": "dist" diff --git a/tests/AdminControllerTest.php b/tests/AdminControllerTest.php index 3fdf04db..09488d85 100644 --- a/tests/AdminControllerTest.php +++ b/tests/AdminControllerTest.php @@ -1,5 +1,7 @@ create(); $user->changePassword('12345678'); - $player = factory(App\Models\Player::class)->create( + $player = factory(Player::class)->create( [ 'uid' => $user->uid ] @@ -90,7 +95,8 @@ class AuthControllerTest extends TestCase 'msg' => trans('auth.validation.password'), 'login_fails' => 1 ] - ); // Unable to assert cache content since array driver has unexpected behaviors + ); + $this->assertCacheHas($loginFailsCacheKey); $this->flushSession(); diff --git a/tests/BrowserKitTestCase.php b/tests/BrowserKitTestCase.php index 9fec8810..4f329790 100644 --- a/tests/BrowserKitTestCase.php +++ b/tests/BrowserKitTestCase.php @@ -1,6 +1,12 @@ make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); + $app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap(); Artisan::call('migrate:refresh'); diff --git a/tests/ClosetControllerTest.php b/tests/ClosetControllerTest.php index a2107951..3dbb0122 100644 --- a/tests/ClosetControllerTest.php +++ b/tests/ClosetControllerTest.php @@ -1,5 +1,7 @@ cache($data, $minutes); + + return $this; + } + + /** + * Set the cache to the given array. + * + * @param array $data + * @return void + */ + public function cache(array $data, $minutes = 60) + { + foreach ($data as $key => $value) { + $this->app['cache']->put($key, $value, $minutes); + } + } + + /** + * Flush all of the current cache data. + * + * @return void + */ + public function flushCache() + { + $this->app['cache']->flush(); + } + + /** + * Assert that the cache has a given value. + * + * @param string|array $key + * @param mixed $value + * @return $this + */ + public function seeInCache($key, $value = null) + { + $this->assertCacheHas($key, $value); + + return $this; + } + + /** + * Assert that the cache has a given value. + * + * @param string|array $key + * @param mixed $value + * @return $this + */ + public function assertCacheHas($key, $value = null) + { + if (is_array($key)) { + $this->assertCacheHasAll($key); + return $this; + } + + if (is_null($value)) { + $this->assertTrue($this->app['cache.store']->has($key), "Cache missing key: $key"); + } else { + $this->assertEquals($value, $this->app['cache.store']->get($key)); + } + return $this; + } + + /** + * Assert that the cache has a given list of values. + * + * @param array $bindings + * @return void + */ + public function assertCacheHasAll(array $bindings) + { + foreach ($bindings as $key => $value) { + if (is_int($key)) { + $this->assertCacheHas($value); + } else { + $this->assertCacheHas($key, $value); + } + } + } + + /** + * Assert that the cache does not have a given key. + * + * @param string|array $key + * @return void + */ + public function assertCacheMissing($key) + { + if (is_array($key)) { + foreach ($key as $k) { + $this->assertCacheMissing($k); + } + } else { + $this->assertFalse($this->app['cache.store']->has($key), "Cache has unexpected key: $key"); + } + } +} diff --git a/tests/HomeControllerTest.php b/tests/HomeControllerTest.php index 70d3647d..b46815c4 100644 --- a/tests/HomeControllerTest.php +++ b/tests/HomeControllerTest.php @@ -1,5 +1,8 @@ assertStatus(403); // Binding email - $noEmailUser = factory(App\Models\User::class)->create(['email' => '']); + $noEmailUser = factory(\App\Models\User::class)->create(['email' => '']); $this->actingAs($noEmailUser) ->get('/user') ->assertSee('Bind') @@ -118,7 +121,7 @@ class MiddlewareTest extends TestCase Option::set('return_204_when_notfound', true); $this->getJson('/nope.json')->assertStatus(204); - $player = factory(App\Models\Player::class)->create(); + $player = factory(\App\Models\Player::class)->create(); $this->getJson("/{$player->player_name}.json") ->assertJson(['username' => $player->player_name]); // Default is CSL API diff --git a/tests/ModelsTest/ClosetTest.php b/tests/ModelsTest/ClosetTest.php index 9d128130..8c3bb8de 100644 --- a/tests/ModelsTest/ClosetTest.php +++ b/tests/ModelsTest/ClosetTest.php @@ -1,5 +1,7 @@ getPlugin('avatar-api')->setRequirements([]); - $this->expectsEvents(App\Events\PluginWasEnabled::class); + $this->expectsEvents(Events\PluginWasEnabled::class); $this->postJson('/admin/plugins/manage', [ 'name' => 'avatar-api', 'action' => 'enable' @@ -140,7 +144,7 @@ class PluginControllerTest extends TestCase ['plugin' => plugin('avatar-api')->title] ) ]); - $this->expectsEvents(App\Events\PluginWasDisabled::class); + $this->expectsEvents(Events\PluginWasDisabled::class); // Delete a plugin $this->postJson('/admin/plugins/manage', [ @@ -150,7 +154,7 @@ class PluginControllerTest extends TestCase 'errno' => 0, 'msg' => trans('admin.plugins.operations.deleted') ]); - $this->expectsEvents(App\Events\PluginWasDeleted::class); + $this->expectsEvents(Events\PluginWasDeleted::class); $this->assertFalse(file_exists(base_path('plugins/avatar-api/'))); } diff --git a/tests/ServicesTest/HookTest.php b/tests/ServicesTest/HookTest.php index 51261e5e..3746b2e8 100644 --- a/tests/ServicesTest/HookTest.php +++ b/tests/ServicesTest/HookTest.php @@ -1,5 +1,7 @@ make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); + $app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap(); Artisan::call('migrate:refresh'); @@ -43,48 +51,6 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase return $this->actingAs($role); } - /** - * Set the cache to the given array. - * - * @param array $data - * @param int $minutes - * @return $this - */ - public function withCache(array $data, $minutes = 60) - { - foreach ($data as $key => $value) { - $this->app['cache.store']->put($key, $value, $minutes); - } - return $this; - } - - /** - * Assert that the cache does not have a given key. - * - * @param string|array $key - * @return void - */ - public function assertCacheMissing($key) - { - if (is_array($key)) { - foreach ($key as $k) { - $this->assertCacheMissing($k); - } - } else { - $this->assertFalse($this->app['cache.store']->has($key), "Cache has unexpected key: $key"); - } - } - - /** - * Flush all of the current cache data. - * - * @return void - */ - public function flushCache() - { - $this->app['cache.store']->flush(); - } - protected function tearDown() { $this->beforeApplicationDestroyed(function () { diff --git a/tests/TextureControllerTest.php b/tests/TextureControllerTest.php index 61cfae10..66b8560d 100644 --- a/tests/TextureControllerTest.php +++ b/tests/TextureControllerTest.php @@ -1,8 +1,13 @@