From bfdbe62cd788d948bc4d074d0cd9afcd178edfea Mon Sep 17 00:00:00 2001 From: RamonRobben Date: Wed, 9 Jun 2021 20:53:58 +0200 Subject: [PATCH] General improvements to testing environment and removed clutter (#36) --- .gitignore | 3 ++ BUILDING.md | 21 ++++++++ app/Classes/Pterodactyl.php | 7 ++- app/Console/Commands/MakeUserCommand.php | 22 ++++---- app/Http/Controllers/Admin/UserController.php | 30 +++-------- bin/startdocker.sh | 2 +- bin/test.sh | 1 + composer.json | 2 +- klad.txt | 9 ---- phpunit.xml | 9 ++-- tests/Unit/exampleTest.php | 14 ----- tests/Unit/testUserCommand.php | 52 +++++++++++++++++++ 12 files changed, 109 insertions(+), 63 deletions(-) create mode 100755 bin/test.sh delete mode 100644 klad.txt delete mode 100644 tests/Unit/exampleTest.php create mode 100644 tests/Unit/testUserCommand.php diff --git a/.gitignore b/.gitignore index abc9bc3e..7c47938f 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ Homestead.json Homestead.yaml npm-debug.log yarn-error.log +.gitignore +.env.dev +.env.testing diff --git a/BUILDING.md b/BUILDING.md index 277b44d1..0970630d 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -14,3 +14,24 @@ php artisan key:generate --force php artisan storage:link php artisan migrate --seed --force ``` + +## Setting up testing environment + +Change the .env.testing file to your needs. Then once done you need to go into your phpmyadmin to create a new database named __controlpanel_test__. +Visit http://127.0.0.1:8080/ and create your database. + +Now you're ready to run the following commands which switches to the testing config, migrates the test database and seeds it. +After that you can switch back to your dev environment again. Clear the config from cache so changes will be instantly available. + +```shell +php artisan migrate --env=testing +php artisan db:seed --env=testing +php artisan config:cache --env=dev +php artisan config:clear +``` + +Now when running tests with PHPUnit it will use your testing database and not your local development one. +This is configured in the __phpunit.xml__. You can run your tests by running the command like this. Just type and enter. +`./vendor/bin/phpunit`. If you don't want to type that all the time you can also use my shortcut `bin/test.sh` + + diff --git a/app/Classes/Pterodactyl.php b/app/Classes/Pterodactyl.php index 3d95abaa..30d95681 100644 --- a/app/Classes/Pterodactyl.php +++ b/app/Classes/Pterodactyl.php @@ -25,14 +25,17 @@ class Pterodactyl ])->baseUrl(env('PTERODACTYL_URL') . '/api'); } + //TODO: Extend error handling (maybe logger for more errors when debugging) /** * Get user by pterodactyl id * @param int $pterodactylId * @return mixed */ - public static function getUser(int $pterodactylId){ + public function getUser(int $pterodactylId){ $response = self::client()->get("/application/users/{$pterodactylId}"); - if ($response->failed()) return null; + if ($response->failed()) { + return []; + } return $response->json()['attributes']; } diff --git a/app/Console/Commands/MakeUserCommand.php b/app/Console/Commands/MakeUserCommand.php index 2c971edc..ef3b1372 100644 --- a/app/Console/Commands/MakeUserCommand.php +++ b/app/Console/Commands/MakeUserCommand.php @@ -23,14 +23,17 @@ class MakeUserCommand extends Command */ protected $description = 'Create an admin account with the Artisan Console'; + private Pterodactyl $pterodactyl; + /** * Create a new command instance. * * @return void */ - public function __construct() + public function __construct(Pterodactyl $pterodactyl) { parent::__construct(); + $this->pterodactyl = $pterodactyl; } /** @@ -44,17 +47,16 @@ class MakeUserCommand extends Command $password = $this->option('password') ?? $this->ask('Please specify your password.'); if (strlen($password) < 8) { - print_r('Your password need to be at least 8 characters long'); - - return false; + $this->alert('Your password need to be at least 8 characters long'); + return 0; } - $response = Pterodactyl::getUser($ptero_id); + //TODO: Do something with response (check for status code and give hints based upon that) + $response = $this->pterodactyl->getUser($ptero_id); - if (is_null($response)) { - print_r('It seems that your Pterodactyl ID isnt correct. Rerun the command and input an correct ID'); - - return false; + if ($response === []) { + $this->alert('It seems that your Pterodactyl ID is not correct. Rerun the command and input an correct ID'); + return 0; } $user = User::create([ @@ -73,6 +75,6 @@ class MakeUserCommand extends Command ['Admin', $user->role], ]); - return true; + return 1; } } diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index 9e7336ef..492ea7e4 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -19,6 +19,13 @@ use Illuminate\Validation\ValidationException; class UserController extends Controller { + private Pterodactyl $pterodactyl; + + public function __construct(Pterodactyl $pterodactyl) + { + $this->pterodactyl = $pterodactyl; + } + /** * Display a listing of the resource. * @@ -30,27 +37,6 @@ class UserController extends Controller return view('admin.users.index'); } - /** - * Show the form for creating a new resource. - * - * @return Response - */ - public function create() - { - // - } - - /** - * Store a newly created resource in storage. - * - * @param Request $request - * @return Response - */ - public function store(Request $request) - { - // - } - /** * Display the specified resource. * @@ -96,7 +82,7 @@ class UserController extends Controller "role" => Rule::in(['admin', 'mod', 'client', 'member']), ]); - if (is_null(Pterodactyl::getUser($request->input('pterodactyl_id')))) { + if (is_null($this->pterodactyl->getUser($request->input('pterodactyl_id')))) { throw ValidationException::withMessages([ 'pterodactyl_id' => ["User does not exists on pterodactyl's panel"] ]); diff --git a/bin/startdocker.sh b/bin/startdocker.sh index d9e21110..57a6c515 100644 --- a/bin/startdocker.sh +++ b/bin/startdocker.sh @@ -1,2 +1,2 @@ docker-compose -f docker/docker-compose.yml down -docker-compose -f docker/docker-compose.yml up -d --force-recreate +docker-compose -f docker/docker-compose.yml up -d --force-recreate --remove-orphans diff --git a/bin/test.sh b/bin/test.sh new file mode 100755 index 00000000..934aa561 --- /dev/null +++ b/bin/test.sh @@ -0,0 +1 @@ +./vendor/bin/phpunit diff --git a/composer.json b/composer.json index 3aa6ce68..cc28d042 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "socialiteproviders/discord": "^4.1", "spatie/laravel-activitylog": "^3.16", "yajra/laravel-datatables-oracle": "~9.0", - "ext-intl": "*" + "ext-intl": "*" }, "require-dev": { "facade/ignition": "^2.5", diff --git a/klad.txt b/klad.txt deleted file mode 100644 index e63b14da..00000000 --- a/klad.txt +++ /dev/null @@ -1,9 +0,0 @@ -, -fnDrawCallback: function( oSettings ) { - $('[data-toggle="popover"]').popover(); - } - - - - - data-content="Delete" data-toggle="popover" data-trigger="hover" data-placement="top" diff --git a/phpunit.xml b/phpunit.xml index c7af5191..560bc50e 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -5,8 +5,8 @@ colors="true" > - - tests + + tests/Unit @@ -18,11 +18,12 @@ - - + + + diff --git a/tests/Unit/exampleTest.php b/tests/Unit/exampleTest.php deleted file mode 100644 index c60f7cfd..00000000 --- a/tests/Unit/exampleTest.php +++ /dev/null @@ -1,14 +0,0 @@ -get('/'); - $response->assertStatus(302); - } -} diff --git a/tests/Unit/testUserCommand.php b/tests/Unit/testUserCommand.php new file mode 100644 index 00000000..7abe78d9 --- /dev/null +++ b/tests/Unit/testUserCommand.php @@ -0,0 +1,52 @@ +getMockBuilder(Pterodactyl::class)->getMock(); + $pterodactyl->expects(self::once())->method('getUser')->willReturn($apiResponse); + + $this->app->instance(Pterodactyl::class, $pterodactyl); + + $this->artisan('make:user') + ->expectsQuestion('Please specify your Pterodactyl ID.', 0) + ->expectsQuestion('Please specify your password.', 'password') + ->assertExitCode($expectedExitCode); + } + + public function invalidPteroIdDataProvider(): array + { + return [ + 'Good Response' => [ + 'apiResponse' => [ + 'id' => 12345, + 'first_name' => 'Test', + 'email' => 'test@test.test' + ], + 'expectedExitCode' => 1 + ], + 'Bad Response' => [ + 'apiResponse' => [], + 'expectedExitCode' => 0 + ] + ]; + } +}