From 0691e5a1078fe90de1926f04002b648e1701c69c Mon Sep 17 00:00:00 2001 From: Kevin ANATOLE Date: Sat, 31 Oct 2020 19:20:53 +0000 Subject: [PATCH] Game formats - Created the formats tables/migrations - Created the controller to retrieve the formats --- .env.testing | 6 + app/Http/Controllers/FormatsController.php | 19 ++ app/Http/Middleware/CorsMiddleware.php | 82 +++--- app/Http/Middleware/FirebaseMIddleware.php | 134 +++++----- app/Model/Format.php | 9 + bootstrap/app.php | 246 +++++++++--------- database/factories/ModelFactory.php | 7 + ...2020_10_29_131400_create_formats_table.php | 32 +++ database/seeds/DatabaseSeeder.php | 3 + database/seeds/FormatSeeder.php | 17 ++ phpunit.xml | 4 + routes/web.php | 2 + .../Controllers/FormatsControllerTest.php | 20 ++ 13 files changed, 350 insertions(+), 231 deletions(-) create mode 100644 .env.testing create mode 100644 app/Http/Controllers/FormatsController.php create mode 100644 app/Model/Format.php create mode 100644 database/migrations/2020_10_29_131400_create_formats_table.php create mode 100644 database/seeds/FormatSeeder.php create mode 100644 tests/Http/Controllers/FormatsControllerTest.php diff --git a/.env.testing b/.env.testing new file mode 100644 index 0000000..681e283 --- /dev/null +++ b/.env.testing @@ -0,0 +1,6 @@ +DB_CONNECTION=mysql +DB_HOST=127.0.0.1 +DB_PORT=3306 +DB_DATABASE=commanderleagueapi_test +DB_USERNAME=root +DB_PASSWORD= diff --git a/app/Http/Controllers/FormatsController.php b/app/Http/Controllers/FormatsController.php new file mode 100644 index 0000000..c1171c7 --- /dev/null +++ b/app/Http/Controllers/FormatsController.php @@ -0,0 +1,19 @@ +json(['data' => $formats->toArray()], 200); + } +} diff --git a/app/Http/Middleware/CorsMiddleware.php b/app/Http/Middleware/CorsMiddleware.php index 2431413..e1e0400 100644 --- a/app/Http/Middleware/CorsMiddleware.php +++ b/app/Http/Middleware/CorsMiddleware.php @@ -1,41 +1,41 @@ - '*', - 'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, DELETE', - 'Access-Control-Allow-Credentials' => 'true', - 'Access-Control-Max-Age' => '86400', - 'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With' - ]; - - if ($request->isMethod('OPTIONS')) - { - return response()->json('{"method":"OPTIONS"}', 200, $headers); - } - - $response = $next($request); - foreach($headers as $key => $value) - { - $response->header($key, $value); - } - - return $response; - } -} + '*', + 'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, DELETE', + 'Access-Control-Allow-Credentials' => 'true', + 'Access-Control-Max-Age' => '86400', + 'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With' + ]; + + if ($request->isMethod('OPTIONS')) + { + return response()->json('{"method":"OPTIONS"}', 200, $headers); + } + + $response = $next($request); + foreach($headers as $key => $value) + { + $response->header($key, $value); + } + + return $response; + } +} diff --git a/app/Http/Middleware/FirebaseMIddleware.php b/app/Http/Middleware/FirebaseMIddleware.php index 178a207..1fd1d7f 100644 --- a/app/Http/Middleware/FirebaseMIddleware.php +++ b/app/Http/Middleware/FirebaseMIddleware.php @@ -1,67 +1,67 @@ -bearerToken(); - if (!$token) { - return response()->json('No token provided', 403); - } - - $user = $this->validateToken($token); - - if ($user === null) { - return response()->json('User not found', 403); - } - $request->request->add(['user' => $user]); - - return $next($request); - } - - public function validateToken($token) - { - try { - $decoded = FirebaseAuth::verifyIdToken($token); - $uid = $decoded->getClaim('sub'); - $user = User::where('provider_id', $uid)->get(); - $user = $user[0] ?? null; - - if (!$user) { - /** @var \Kreait\Firebase\Auth\UserRecord $data */ - $data = FirebaseAuth::getUser($uid); - $user = new User(); - $user->provider_id = $data->uid; - $user->email = $data->email; - $user->name = $data->displayName ?? 'No Name User'; - $user->image = $data->photoUrl ?? ''; - $user->save(); - } - - return $user; - } catch (\Exception $e) { - return null; - }; - } -} +bearerToken(); + if (!$token) { + return response()->json('No token provided', 403); + } + + $user = $this->validateToken($token); + + if ($user === null) { + return response()->json('User not found', 403); + } + $request->request->add(['user' => $user]); + + return $next($request); + } + + public function validateToken($token) + { + try { + $decoded = FirebaseAuth::verifyIdToken($token); + $uid = $decoded->getClaim('sub'); + $user = User::where('provider_id', $uid)->get(); + $user = $user[0] ?? null; + + if (!$user) { + /** @var \Kreait\Firebase\Auth\UserRecord $data */ + $data = FirebaseAuth::getUser($uid); + $user = new User(); + $user->provider_id = $data->uid; + $user->email = $data->email; + $user->name = $data->displayName ?? 'No Name User'; + $user->image = $data->photoUrl ?? ''; + $user->save(); + } + + return $user; + } catch (\Exception $e) { + return null; + }; + } +} diff --git a/app/Model/Format.php b/app/Model/Format.php new file mode 100644 index 0000000..f581492 --- /dev/null +++ b/app/Model/Format.php @@ -0,0 +1,9 @@ +bootstrap(); - -date_default_timezone_set(env('APP_TIMEZONE', 'UTC')); - -/* -|-------------------------------------------------------------------------- -| Create The Application -|-------------------------------------------------------------------------- -| -| Here we will load the environment and create the application instance -| that serves as the central piece of this framework. We'll use this -| application as an "IoC" container and router for this framework. -| -*/ - -$app = new Laravel\Lumen\Application( - dirname(__DIR__) -); - -$app->withFacades(); -$app->withEloquent(); - -/* -|-------------------------------------------------------------------------- -| Register Container Bindings -|-------------------------------------------------------------------------- -| -| Now we will register a few bindings in the service container. We will -| register the exception handler and the console kernel. You may add -| your own bindings here if you like or you can make another file. -| -*/ - -$app->singleton( - Illuminate\Contracts\Debug\ExceptionHandler::class, - App\Exceptions\Handler::class -); - -$app->singleton( - Illuminate\Contracts\Console\Kernel::class, - App\Console\Kernel::class -); - -/* -|-------------------------------------------------------------------------- -| Register Config Files -|-------------------------------------------------------------------------- -| -| Now we will register the "app" configuration file. If the file exists in -| your configuration directory it will be loaded; otherwise, we'll load -| the default version. You may register other files below as needed. -| -*/ - -$app->configure('app'); - -/* -|-------------------------------------------------------------------------- -| Register Middleware -|-------------------------------------------------------------------------- -| -| Next, we will register the middleware with the application. These can -| be global middleware that run before and after each request into a -| route or middleware that'll be assigned to some specific routes. -| -*/ - -// $app->middleware([ -// App\Http\Middleware\ExampleMiddleware::class -// ]); - -// $app->routeMiddleware([ -// 'auth' => App\Http\Middleware\Authenticate::class, -// ]); - -$app->middleware([ - \App\Http\Middleware\CorsMiddleware::class -]); - -$app->routeMiddleware([ - 'auth' => App\Http\Middleware\FirebaseMiddleware::class, -]); - -/* -|-------------------------------------------------------------------------- -| Register Service Providers -|-------------------------------------------------------------------------- -| -| Here we will register all of the application's service providers which -| are used to bind services into the container. Service providers are -| totally optional, so you are not required to uncomment this line. -| -*/ - -// $app->register(App\Providers\AppServiceProvider::class); -// $app->register(App\Providers\AuthServiceProvider::class); -// $app->register(App\Providers\EventServiceProvider::class); -$app->register(Kreait\Laravel\Firebase\ServiceProvider::class); - -/* -|-------------------------------------------------------------------------- -| Load The Application Routes -|-------------------------------------------------------------------------- -| -| Next we will include the routes file so that they can all be added to -| the application. This will provide all of the URLs the application -| can respond to, as well as the controllers that may handle them. -| -*/ - -$app->router->group([ - 'namespace' => 'App\Http\Controllers', -], function ($router) { - require __DIR__.'/../routes/web.php'; -}); - -return $app; +bootstrap(); + +date_default_timezone_set(env('APP_TIMEZONE', 'UTC')); + +/* +|-------------------------------------------------------------------------- +| Create The Application +|-------------------------------------------------------------------------- +| +| Here we will load the environment and create the application instance +| that serves as the central piece of this framework. We'll use this +| application as an "IoC" container and router for this framework. +| +*/ + +$app = new Laravel\Lumen\Application( + dirname(__DIR__) +); + +$app->withFacades(); +$app->withEloquent(); + +/* +|-------------------------------------------------------------------------- +| Register Container Bindings +|-------------------------------------------------------------------------- +| +| Now we will register a few bindings in the service container. We will +| register the exception handler and the console kernel. You may add +| your own bindings here if you like or you can make another file. +| +*/ + +$app->singleton( + Illuminate\Contracts\Debug\ExceptionHandler::class, + App\Exceptions\Handler::class +); + +$app->singleton( + Illuminate\Contracts\Console\Kernel::class, + App\Console\Kernel::class +); + +/* +|-------------------------------------------------------------------------- +| Register Config Files +|-------------------------------------------------------------------------- +| +| Now we will register the "app" configuration file. If the file exists in +| your configuration directory it will be loaded; otherwise, we'll load +| the default version. You may register other files below as needed. +| +*/ + +$app->configure('app'); + +/* +|-------------------------------------------------------------------------- +| Register Middleware +|-------------------------------------------------------------------------- +| +| Next, we will register the middleware with the application. These can +| be global middleware that run before and after each request into a +| route or middleware that'll be assigned to some specific routes. +| +*/ + +// $app->middleware([ +// App\Http\Middleware\ExampleMiddleware::class +// ]); + +// $app->routeMiddleware([ +// 'auth' => App\Http\Middleware\Authenticate::class, +// ]); + +$app->middleware([ + \App\Http\Middleware\CorsMiddleware::class +]); + +$app->routeMiddleware([ + 'auth' => App\Http\Middleware\FirebaseMiddleware::class, +]); + +/* +|-------------------------------------------------------------------------- +| Register Service Providers +|-------------------------------------------------------------------------- +| +| Here we will register all of the application's service providers which +| are used to bind services into the container. Service providers are +| totally optional, so you are not required to uncomment this line. +| +*/ + +// $app->register(App\Providers\AppServiceProvider::class); +// $app->register(App\Providers\AuthServiceProvider::class); +// $app->register(App\Providers\EventServiceProvider::class); +$app->register(Kreait\Laravel\Firebase\ServiceProvider::class); + +/* +|-------------------------------------------------------------------------- +| Load The Application Routes +|-------------------------------------------------------------------------- +| +| Next we will include the routes file so that they can all be added to +| the application. This will provide all of the URLs the application +| can respond to, as well as the controllers that may handle them. +| +*/ + +$app->router->group([ + 'namespace' => 'App\Http\Controllers', +], function ($router) { + require __DIR__.'/../routes/web.php'; +}); + +return $app; diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index 953a6a1..0ba2a55 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -2,6 +2,7 @@ /** @var \Illuminate\Database\Eloquent\Factory $factory */ +use App\Model\Format; use App\User; use Faker\Generator as Faker; @@ -22,3 +23,9 @@ $factory->define(User::class, function (Faker $faker) { 'email' => $faker->email, ]; }); + +$factory->define(Format::class, function (Faker $faker){ + return [ + 'name' => $faker->name + ]; +}); diff --git a/database/migrations/2020_10_29_131400_create_formats_table.php b/database/migrations/2020_10_29_131400_create_formats_table.php new file mode 100644 index 0000000..6644852 --- /dev/null +++ b/database/migrations/2020_10_29_131400_create_formats_table.php @@ -0,0 +1,32 @@ +id(); + $table->string('name')->unique(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('formats'); + } +} diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index 23526c9..782b5b5 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -12,5 +12,8 @@ class DatabaseSeeder extends Seeder public function run() { // $this->call('UsersTableSeeder'); + $this->call([ + FormatSeeder::class + ]); } } diff --git a/database/seeds/FormatSeeder.php b/database/seeds/FormatSeeder.php new file mode 100644 index 0000000..f6737ac --- /dev/null +++ b/database/seeds/FormatSeeder.php @@ -0,0 +1,17 @@ + 'Commander']); + \App\Model\Format::create(['name' => 'Brawl']); + } +} diff --git a/phpunit.xml b/phpunit.xml index e94653d..6493221 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -17,5 +17,9 @@ + + + + diff --git a/routes/web.php b/routes/web.php index 1c81a67..66157fc 100644 --- a/routes/web.php +++ b/routes/web.php @@ -15,3 +15,5 @@ $router->get('/', function () use ($router) { return $router->app->version(); }); + +$router->get('/formats', ['uses' => 'FormatsController@index', 'as' => 'formats.list']); diff --git a/tests/Http/Controllers/FormatsControllerTest.php b/tests/Http/Controllers/FormatsControllerTest.php new file mode 100644 index 0000000..944c9a6 --- /dev/null +++ b/tests/Http/Controllers/FormatsControllerTest.php @@ -0,0 +1,20 @@ +create(); + $this->json('GET', route('formats.list'))->seeStatusCode(200)->seeJson($formats->all()); + } +} +