diff --git a/database/factories/VideoFactory.php b/database/factories/VideoFactory.php new file mode 100644 index 0000000..36d311e --- /dev/null +++ b/database/factories/VideoFactory.php @@ -0,0 +1,11 @@ +define(Video::class, function (Faker $faker) { + return [ + 'title' => ucfirst($faker->words(3, true)) + ]; +}); diff --git a/database/migrations/2019_07_14_124511_create_videos_table.php b/database/migrations/2019_07_14_124511_create_videos_table.php new file mode 100644 index 0000000..d9f98e7 --- /dev/null +++ b/database/migrations/2019_07_14_124511_create_videos_table.php @@ -0,0 +1,32 @@ +bigIncrements('id'); + $table->string('title'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('videos'); + } +} diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index fe941e6..620d771 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -12,5 +12,6 @@ class DatabaseSeeder extends Seeder public function run() { $this->call(ClientsTableSeeder::class); + $this->call(VideosTableSeeder::class); } } diff --git a/database/seeds/VideosTableSeeder.php b/database/seeds/VideosTableSeeder.php new file mode 100644 index 0000000..0c3d696 --- /dev/null +++ b/database/seeds/VideosTableSeeder.php @@ -0,0 +1,17 @@ +create(); + } +} diff --git a/domain/Videos/Http/Controllers/VideoController.php b/domain/Videos/Http/Controllers/VideoController.php new file mode 100644 index 0000000..3ddd51d --- /dev/null +++ b/domain/Videos/Http/Controllers/VideoController.php @@ -0,0 +1,69 @@ +all()); + } + + /** + * Display the specified resource. + * + * @param Video $video + * @return \Illuminate\Http\Response + */ + public function show(Video $video) + { + return $video; + } + + /** + * Update the specified resource in storage. + * + * @param VideoUpdateRequest $request + * @param Video $video + * @return \Illuminate\Http\Response + */ + public function update(VideoUpdateRequest $request, Video $video) + { + $video->update($request->all()); + return $video; + } + + /** + * Remove the specified resource from storage. + * + * @param Video $video + * @return \Illuminate\Http\Response + */ + public function destroy(Video $video) + { + $video->delete(); + return new JsonResponse('', 204); + } +} diff --git a/domain/Videos/Http/Requests/VideoCreateRequest.php b/domain/Videos/Http/Requests/VideoCreateRequest.php new file mode 100644 index 0000000..7238c64 --- /dev/null +++ b/domain/Videos/Http/Requests/VideoCreateRequest.php @@ -0,0 +1,30 @@ + 'string' + ]; + } +} diff --git a/domain/Videos/Http/Requests/VideoUpdateRequest.php b/domain/Videos/Http/Requests/VideoUpdateRequest.php new file mode 100644 index 0000000..ee65c35 --- /dev/null +++ b/domain/Videos/Http/Requests/VideoUpdateRequest.php @@ -0,0 +1,30 @@ + 'string' + ]; + } +} diff --git a/domain/Videos/Video.php b/domain/Videos/Video.php new file mode 100644 index 0000000..fb08dbd --- /dev/null +++ b/domain/Videos/Video.php @@ -0,0 +1,17 @@ +seed('DatabaseSeeder'); + } + + /** + * @test + */ + public function should_return_all_videos() + { + $response = $this->getJson('/api/v1/videos')->assertStatus(200); + + $responseData = $response->json(); + + $this->assertArrayHasKey('id', $responseData[0]); + $this->assertSame(5, count($responseData)); + } + + /** + * @test + */ + public function should_return_needed_video() + { + $response = $this->getJson('/api/v1/videos/1')->assertStatus(200); + $video = Video::find(1); + + $responseData = $response->json(); + + $this->assertArrayHasKey('id', $responseData); + $this->assertSame($video->title, $responseData['title']); + } + + /** + * @test + */ + public function should_return_error_when_video_not_found() + { + $response = $this->getJson('/api/v1/videos/6')->assertStatus(404); + $responseData = $response->json(); + + $this->assertArrayHasKey('data', $responseData); + $this->assertSame("Resource not found", $responseData['data']); + } + + /** + * @test + */ + public function should_delete_video() + { + $videosBefore = Video::all()->count(); + $this->assertSame(5, $videosBefore); + + $this->deleteJson('/api/v1/videos/1')->assertStatus(204); + $videosAfter = Video::all()->count(); + $this->assertSame(4, $videosAfter); + + $this->getJson('/api/v1/videos/1')->assertStatus(404); + } + + /** + * @test + */ + public function should_not_delete_when_video_not_found() + { + $response = $this->deleteJson('/api/v1/videos/6')->assertStatus(404); + $responseData = $response->json(); + + $this->assertArrayHasKey('data', $responseData); + $this->assertSame("Resource not found", $responseData['data']); + } + + /** + * @test + */ + public function should_update_video() + { + $faker = Factory::create(); + $payload = [ + 'title' => ucfirst($faker->words(3, true)) + ]; + + $response = $this->putJson('/api/v1/videos/1', $payload)->assertStatus(200); + $responseData = $response->json(); + + $this->assertSame($payload['title'], $responseData['title']); + } + + /** + * @test + */ + public function should_not_update_video_with_wrong_data() + { + $faker = Factory::create(); + $payload = [ + 'title' => 1111 + ]; + + $response = $this->putJson('/api/v1/videos/1', $payload)->assertStatus(422); + $responseData = $response->json(); + + $this->assertArrayHasKey('message', $responseData); + $this->assertSame('The title must be a string.', $responseData['errors']['title'][0]); + } + + /** + * @test + */ + public function should_create_video() + { + $videosCountBefore = Video::all()->count(); + $faker = Factory::create(); + $payload = [ + 'title' => ucfirst($faker->words(3, true)) + ]; + + $this->postJson('/api/v1/videos/', $payload)->assertStatus(201); + $videosCountAfter = Video::all()->count(); + + $this->assertGreaterThan($videosCountBefore, $videosCountAfter); + } +}