-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c521b2a
commit c2d9a86
Showing
10 changed files
with
353 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
/* @var $factory \Illuminate\Database\Eloquent\Factory */ | ||
use Domain\Videos\Video; | ||
use Faker\Generator as Faker; | ||
|
||
$factory->define(Video::class, function (Faker $faker) { | ||
return [ | ||
'title' => ucfirst($faker->words(3, true)) | ||
]; | ||
}); |
32 changes: 32 additions & 0 deletions
32
database/migrations/2019_07_14_124511_create_videos_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateVideosTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('videos', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->string('title'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('videos'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
use Domain\Videos\Video; | ||
use Illuminate\Database\Seeder; | ||
|
||
class VideosTableSeeder extends Seeder | ||
{ | ||
/** | ||
* Run the database seeds. | ||
* | ||
* @return void | ||
*/ | ||
public function run() | ||
{ | ||
$videos = factory(Video::class, 5)->create(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Domain\Videos\Http\Controllers; | ||
|
||
use App\Core\Http\Controllers\Controller; | ||
use Domain\Videos\Video; | ||
use Domain\Videos\Http\Requests\VideoUpdateRequest; | ||
use Domain\Videos\Http\Requests\VideoCreateRequest; | ||
use Illuminate\Http\JsonResponse; | ||
|
||
class VideoController extends Controller | ||
{ | ||
/** | ||
* Display a listing of the resource. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function index() | ||
{ | ||
return Video::all(); | ||
} | ||
|
||
/** | ||
* Store a newly created resource in storage. | ||
* | ||
* @param VideoCreateRequest $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function store(VideoCreateRequest $request) | ||
{ | ||
return Video::create($request->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Domain\Videos\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class VideoCreateRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'title' => 'string' | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Domain\Videos\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class VideoUpdateRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'title' => 'string' | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Domain\Videos; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Video extends Model | ||
{ | ||
public $timestamps = true; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = ['title']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use Domain\Videos\Video; | ||
use Tests\TestCase; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Faker\Factory; | ||
|
||
|
||
/** | ||
* @group latest | ||
*/ | ||
class VideoTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->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); | ||
} | ||
} |