From 3ec72654ff77160af75f93d7c2df7034af779e6a Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 23 Mar 2020 01:30:30 +0700 Subject: [PATCH 01/19] adding track detail of album --- phpstan.neon | 1 + src/Config/Routes.php | 14 +++ src/Config/Services.php | 15 +++- src/Controllers/Album.php | 6 +- src/Controllers/Track.php | 87 +++++++++++++++++++ src/Domain/Album/AlbumRepository.php | 8 +- src/Domain/Repository.php | 11 +++ src/Domain/Track/Track.php | 20 +++++ src/Domain/Track/TrackNotFoundException.php | 9 ++ src/Domain/Track/TrackRepository.php | 9 ++ .../Persistence/Track/SQLTrackRepository.php | 70 +++++++++++++++ src/Models/TrackModel.php | 18 ++++ src/Views/{ => album}/add.php | 0 src/Views/{ => album}/edit.php | 0 src/Views/{ => album}/index.php | 0 src/Views/track/add.php | 36 ++++++++ src/Views/track/edit.php | 37 ++++++++ src/Views/track/index.php | 61 +++++++++++++ 18 files changed, 391 insertions(+), 11 deletions(-) create mode 100644 src/Controllers/Track.php create mode 100644 src/Domain/Repository.php create mode 100644 src/Domain/Track/Track.php create mode 100644 src/Domain/Track/TrackNotFoundException.php create mode 100644 src/Domain/Track/TrackRepository.php create mode 100644 src/Infrastructure/Persistence/Track/SQLTrackRepository.php create mode 100644 src/Models/TrackModel.php rename src/Views/{ => album}/add.php (100%) rename src/Views/{ => album}/edit.php (100%) rename src/Views/{ => album}/index.php (100%) create mode 100644 src/Views/track/add.php create mode 100644 src/Views/track/edit.php create mode 100644 src/Views/track/index.php diff --git a/phpstan.neon b/phpstan.neon index c5d3b1d..fdfa8fb 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -13,4 +13,5 @@ parameters: - '#Access to protected property [a-zA-Z0-9\\_]+Model::\$returnType.#' - '#Method ([a-zA-Z0-9\\_]+)(Controllers)([a-zA-Z0-9\\_]+)(::)([a-zA-Z0-9]+)(\(\)) has no return typehint specified.#' - '#Call to an undefined static method Config\\Services::albumRepository()#' + - '#Call to an undefined static method Config\\Services::trackRepository()#' - '#Call to protected method builder\(\) of class CodeIgniter\\Model.#' \ No newline at end of file diff --git a/src/Config/Routes.php b/src/Config/Routes.php index bac7983..a17eb11 100644 --- a/src/Config/Routes.php +++ b/src/Config/Routes.php @@ -13,3 +13,17 @@ // example URI: /album/1 $routes->match(['get', 'post'], 'edit/(:num)', 'Album::edit/$1', ['as' => 'album-edit']); }); + +$routes->group('track', ['namespace' => 'Album\Controllers'], function ($routes) { + // URI: /track/1 + $routes->get('(:num)', 'Track::index', ['as' => 'track-index']); + + // URI: /track/add/(:num) + $routes->match(['get', 'post'], 'add/(:num)', 'Track::add', ['as' => 'track-add']); + + // example URI: /track/delete/1/2 + $routes->get('delete/(:num)/(:num)', 'Track::delete/$1', ['as' => 'track-delete']); + + // example URI: /track/1/2 + $routes->match(['get', 'post'], 'edit/(:num)/(:num)', 'Track::edit/$1/$2', ['as' => 'track-edit']); +}); diff --git a/src/Config/Services.php b/src/Config/Services.php index e9f4992..ea8a6e2 100644 --- a/src/Config/Services.php +++ b/src/Config/Services.php @@ -1,7 +1,8 @@ repository->findPaginatedData($data['keyword']); $data['pager'] = $this->repository->pager(); - return view('Album\Views\index', $data); + return view('Album\Views\album\index', $data); } public function add() @@ -40,7 +40,7 @@ public function add() return redirect()->withInput()->back(); } - return view('Album\Views\add', ['errors' => session()->getFlashData('errors')]); + return view('Album\Views\album\add', ['errors' => session()->getFlashData('errors')]); } public function edit(int $id) @@ -67,7 +67,7 @@ public function edit(int $id) return redirect()->withInput()->back(); } - return view('Album\Views\edit', ['album' => $album, 'errors' => session()->getFlashData('errors')]); + return view('Album\Views\album\edit', ['album' => $album, 'errors' => session()->getFlashData('errors')]); } public function delete(int $id) diff --git a/src/Controllers/Track.php b/src/Controllers/Track.php new file mode 100644 index 0000000..53a190c --- /dev/null +++ b/src/Controllers/Track.php @@ -0,0 +1,87 @@ +repository = Services::trackRepository(); + } + + public function index(int $albumId) + { + $data['keyword'] = $this->request->getGet('keyword') ?? ''; + $data['tracks'] = $this->repository->findPaginatedData($albumId, $data['keyword']); + $data['pager'] = $this->repository->pager(); + + return view('Album\Views\track\index', $data); + } + + public function add(int $albumId) + { + if ($this->request->getMethod() === 'post') + { + $data = $this->request->getPost(); + if ($this->repository->save($data)) + { + session()->setFlashdata('status', 'New album track has been added'); + return redirect()->route('track-index'); + } + + session()->setFlashdata('errors', $this->repository->errors()); + return redirect()->withInput()->back(); + } + + return view('Album\Views\track\add', ['albumId' => $albumId, 'errors' => session()->getFlashData('errors')]); + } + + public function edit(int $trackId) + { + try + { + $track = $this->repository->findTrackOfId($trackId); + } + catch (RecordNotFoundException $e) + { + throw new PageNotFoundException($e->getMessage()); + } + + if ($this->request->getMethod() === 'post') + { + $data = $this->request->getPost(); + if ($this->repository->save($data)) + { + session()->setFlashdata('status', 'Album track has been updated'); + return redirect()->route('track-index'); + } + + session()->setFlashdata('errors', $this->repository->errors()); + return redirect()->withInput()->back(); + } + + return view('Album\Views\track\edit', ['track' => $track, 'errors' => session()->getFlashData('errors')]); + } + + public function delete(int $trackId) + { + try + { + $this->repository->deleteOfId($trackId); + } + catch (RecordNotFoundException $e) + { + throw new PageNotFoundException($e->getMessage()); + } + + session()->setFlashdata('status', 'Album track has been deleted'); + return redirect()->route('track-index'); + } +} diff --git a/src/Domain/Album/AlbumRepository.php b/src/Domain/Album/AlbumRepository.php index 2431730..6cb055e 100644 --- a/src/Domain/Album/AlbumRepository.php +++ b/src/Domain/Album/AlbumRepository.php @@ -1,13 +1,9 @@ attributes['title'] = ucwords($title); + return $this; + } +} diff --git a/src/Domain/Track/TrackNotFoundException.php b/src/Domain/Track/TrackNotFoundException.php new file mode 100644 index 0000000..14e826b --- /dev/null +++ b/src/Domain/Track/TrackNotFoundException.php @@ -0,0 +1,9 @@ +model = $model; + } + + public function findPaginatedData(int $albumId, string $keyword = ''): ?array + { + if ($keyword) + { + $this->model + ->builder() + ->where('album_id', $albumId) + ->groupStart() + ->like('artist', $keyword) + ->orLike('title', $keyword) + ->groupEnd(); + } + + return $this->model->paginate(config('Album')->paginationPerPage); + } + + public function pager(): ?PagerInterface + { + return $this->model->pager; + } + + public function findTrackOfId(int $id): Track + { + $track = $this->model->find($id); + if (! $track instanceof Track) + { + throw new TrackNotFoundException(); + } + + return $track; + } + + public function save(array $data = null): bool + { + return $this->model->save(new $this->model->returnType($data)); + } + + public function errors(): ?array + { + return $this->model->errors(); + } + + public function deleteOfId(int $id) : bool + { + $delete = $this->model->delete($id); + if ($delete->connID->affected_rows === 0) + { + throw new TrackNotFoundException(); + } + + return true; + } +} diff --git a/src/Models/TrackModel.php b/src/Models/TrackModel.php new file mode 100644 index 0000000..a6a4de5 --- /dev/null +++ b/src/Models/TrackModel.php @@ -0,0 +1,18 @@ + 'required|numeric|is_unique[album.id,album_id,{album_id}]', + 'title' => 'required|alpha_numeric_space|min_length[3]|max_length[255]|is_unique[track.title,id,{id}]', + ]; +} diff --git a/src/Views/add.php b/src/Views/album/add.php similarity index 100% rename from src/Views/add.php rename to src/Views/album/add.php diff --git a/src/Views/edit.php b/src/Views/album/edit.php similarity index 100% rename from src/Views/edit.php rename to src/Views/album/edit.php diff --git a/src/Views/index.php b/src/Views/album/index.php similarity index 100% rename from src/Views/index.php rename to src/Views/album/index.php diff --git a/src/Views/track/add.php b/src/Views/track/add.php new file mode 100644 index 0000000..00492c6 --- /dev/null +++ b/src/Views/track/add.php @@ -0,0 +1,36 @@ +setVar('title', $title); + +// extends layout +echo $this->extend('Album\Views\layout'); + +// begin section content +echo $this->section('content'); + +?> + +

+ + + + + +endSection(); +?> \ No newline at end of file diff --git a/src/Views/track/edit.php b/src/Views/track/edit.php new file mode 100644 index 0000000..d488c0b --- /dev/null +++ b/src/Views/track/edit.php @@ -0,0 +1,37 @@ +setVar('title', $title); + +// extends layout +echo $this->extend('Album\Views\layout'); + +// begin section content +echo $this->section('content'); + +?> + +

+ +id)); +echo form_label('Artist', 'artist'); +echo form_hidden('id', set_value('id', $album->id)); +echo form_input('artist', set_value('artist', $album->artist)); +echo $errors['artist'] ?? ''; + +echo form_label('Title', 'title'); +echo form_input('title', set_value('title', $album->title)); +echo $errors['title'] ?? ''; + +echo form_submit('Save', 'Save New Album'); +echo form_close(); +?> + + + +endSection(); +?> \ No newline at end of file diff --git a/src/Views/track/index.php b/src/Views/track/index.php new file mode 100644 index 0000000..362d2d0 --- /dev/null +++ b/src/Views/track/index.php @@ -0,0 +1,61 @@ +setVar('title', $title); + +// extends layout +echo $this->extend('Album\Views\layout'); + +// begin section content +echo $this->section('content'); +?> +

+

+ +

+ + 'get']); +echo form_input('keyword', esc($keyword), ['placeholder' => 'Search keyword']); +echo form_close(); +?> + +
+ getFlashdata('status'); ?> +
+ + + + + + + + + + + + + + + + + + +
TitleArtist 
No album found.
title) ?>artist) ?> + id), 'Edit'); ?> + id), + 'Delete', + ['onclick' => 'return confirm(\'Are you sure?\')'] + ); + ?> +
+links() ?> + +endSection(); +?> \ No newline at end of file From 92c39031bcfaa72ff9357a2be22c565ea39a42a8 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 23 Mar 2020 01:48:39 +0700 Subject: [PATCH 02/19] use of album entity in track repository --- src/Controllers/Track.php | 73 +++++++++++++++---- src/Domain/Album/Album.php | 5 ++ src/Domain/Track/Track.php | 5 ++ src/Domain/Track/TrackRepository.php | 3 +- .../Persistence/Track/SQLTrackRepository.php | 5 +- 5 files changed, 72 insertions(+), 19 deletions(-) diff --git a/src/Controllers/Track.php b/src/Controllers/Track.php index 53a190c..48e983b 100644 --- a/src/Controllers/Track.php +++ b/src/Controllers/Track.php @@ -1,6 +1,7 @@ repository = Services::trackRepository(); + $this->albumRepository = Services::albumRepository(); + $this->trackRepository = Services::trackRepository(); } public function index(int $albumId) { + try + { + $album = $this->albumRepository->findAlbumOfId($albumId); + } + catch (RecordNotFoundException $e) + { + throw new PageNotFoundException($e->getMessage()); + } + $data['keyword'] = $this->request->getGet('keyword') ?? ''; - $data['tracks'] = $this->repository->findPaginatedData($albumId, $data['keyword']); - $data['pager'] = $this->repository->pager(); + $data['tracks'] = $this->trackRepository->findPaginatedData($album, $data['keyword']); + $data['pager'] = $this->trackRepository->pager(); return view('Album\Views\track\index', $data); } public function add(int $albumId) { + try + { + $album = $this->albumRepository->findAlbumOfId($albumId); + } + catch (RecordNotFoundException $e) + { + throw new PageNotFoundException($e->getMessage()); + } + if ($this->request->getMethod() === 'post') { $data = $this->request->getPost(); - if ($this->repository->save($data)) + if ($this->trackRepository->save($data)) { session()->setFlashdata('status', 'New album track has been added'); - return redirect()->route('track-index'); + return redirect()->route('track-index', [$albumId]); } - session()->setFlashdata('errors', $this->repository->errors()); + session()->setFlashdata('errors', $this->trackRepository->errors()); return redirect()->withInput()->back(); } - return view('Album\Views\track\add', ['albumId' => $albumId, 'errors' => session()->getFlashData('errors')]); + return view('Album\Views\track\add', ['albumId' => $album->id, 'errors' => session()->getFlashData('errors')]); } - public function edit(int $trackId) + public function edit(int $albumId, int $trackId) { try { - $track = $this->repository->findTrackOfId($trackId); + $this->albumRepository->findAlbumOfId($albumId); + } + catch (RecordNotFoundException $e) + { + throw new PageNotFoundException($e->getMessage()); + } + + try + { + $track = $this->trackRepository->findTrackOfId($trackId); } catch (RecordNotFoundException $e) { @@ -57,24 +89,33 @@ public function edit(int $trackId) if ($this->request->getMethod() === 'post') { $data = $this->request->getPost(); - if ($this->repository->save($data)) + if ($this->trackRepository->save($data)) { session()->setFlashdata('status', 'Album track has been updated'); - return redirect()->route('track-index'); + return redirect()->route('track-index', [$albumId]); } - session()->setFlashdata('errors', $this->repository->errors()); + session()->setFlashdata('errors', $this->trackRepository->errors()); return redirect()->withInput()->back(); } return view('Album\Views\track\edit', ['track' => $track, 'errors' => session()->getFlashData('errors')]); } - public function delete(int $trackId) + public function delete(int $albumId, int $trackId) { try { - $this->repository->deleteOfId($trackId); + $this->albumRepository->findAlbumOfId($albumId); + } + catch (RecordNotFoundException $e) + { + throw new PageNotFoundException($e->getMessage()); + } + + try + { + $this->trackRepository->deleteOfId($trackId); } catch (RecordNotFoundException $e) { @@ -82,6 +123,6 @@ public function delete(int $trackId) } session()->setFlashdata('status', 'Album track has been deleted'); - return redirect()->route('track-index'); + return redirect()->route('track-index', [$albumId]); } } diff --git a/src/Domain/Album/Album.php b/src/Domain/Album/Album.php index 46ac12e..b5791a7 100644 --- a/src/Domain/Album/Album.php +++ b/src/Domain/Album/Album.php @@ -2,6 +2,11 @@ use CodeIgniter\Entity; +/** + * @property int $id + * @property string $artist + * @property string $title + */ class Album extends Entity { public function __construct(array $data = null) diff --git a/src/Domain/Track/Track.php b/src/Domain/Track/Track.php index 8c88118..6288d69 100644 --- a/src/Domain/Track/Track.php +++ b/src/Domain/Track/Track.php @@ -2,6 +2,11 @@ use CodeIgniter\Entity; +/** + * @property int $id + * @property int $album_id + * @property string $title + */ class Track extends Entity { public function __construct(array $data = null) diff --git a/src/Domain/Track/TrackRepository.php b/src/Domain/Track/TrackRepository.php index 6b48869..c449f94 100644 --- a/src/Domain/Track/TrackRepository.php +++ b/src/Domain/Track/TrackRepository.php @@ -1,9 +1,10 @@ model = $model; } - public function findPaginatedData(int $albumId, string $keyword = ''): ?array + public function findPaginatedData(Album $album, string $keyword = ''): ?array { if ($keyword) { $this->model ->builder() - ->where('album_id', $albumId) + ->where('album_id', $album->id) ->groupStart() ->like('artist', $keyword) ->orLike('title', $keyword) From 89b5b5d7ec40eae4cda59932d2d5f8a23c32ad13 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 23 Mar 2020 02:05:49 +0700 Subject: [PATCH 03/19] reduce try catch --- src/Controllers/Track.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/Controllers/Track.php b/src/Controllers/Track.php index 48e983b..fb986df 100644 --- a/src/Controllers/Track.php +++ b/src/Controllers/Track.php @@ -71,14 +71,6 @@ public function edit(int $albumId, int $trackId) try { $this->albumRepository->findAlbumOfId($albumId); - } - catch (RecordNotFoundException $e) - { - throw new PageNotFoundException($e->getMessage()); - } - - try - { $track = $this->trackRepository->findTrackOfId($trackId); } catch (RecordNotFoundException $e) @@ -107,14 +99,6 @@ public function delete(int $albumId, int $trackId) try { $this->albumRepository->findAlbumOfId($albumId); - } - catch (RecordNotFoundException $e) - { - throw new PageNotFoundException($e->getMessage()); - } - - try - { $this->trackRepository->deleteOfId($trackId); } catch (RecordNotFoundException $e) From 478283301cca9c9d81dc29df2aab8ae1292550a0 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 26 Mar 2020 19:31:26 +0700 Subject: [PATCH 04/19] handle album_id key not exists --- src/Domain/Track/Track.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Domain/Track/Track.php b/src/Domain/Track/Track.php index 6288d69..ad3c443 100644 --- a/src/Domain/Track/Track.php +++ b/src/Domain/Track/Track.php @@ -1,6 +1,7 @@ Date: Fri, 27 Mar 2020 03:44:37 +0700 Subject: [PATCH 05/19] add track migration --- src/Config/Routes.php | 12 +++--- .../Migrations/2020-03-26-203957_Track.php | 37 +++++++++++++++++++ src/Models/TrackModel.php | 2 + src/Views/album/index.php | 3 +- 4 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 src/Database/Migrations/2020-03-26-203957_Track.php diff --git a/src/Config/Routes.php b/src/Config/Routes.php index a17eb11..d7e8114 100644 --- a/src/Config/Routes.php +++ b/src/Config/Routes.php @@ -16,14 +16,14 @@ $routes->group('track', ['namespace' => 'Album\Controllers'], function ($routes) { // URI: /track/1 - $routes->get('(:num)', 'Track::index', ['as' => 'track-index']); + $routes->get('(:num)', 'Track::index/$1', ['as' => 'track-index']); // URI: /track/add/(:num) - $routes->match(['get', 'post'], 'add/(:num)', 'Track::add', ['as' => 'track-add']); + $routes->match(['get', 'post'], 'add/(:num)', 'Track::add/$1', ['as' => 'track-add']); - // example URI: /track/delete/1/2 - $routes->get('delete/(:num)/(:num)', 'Track::delete/$1', ['as' => 'track-delete']); + // example URI: /track/delete/1 + $routes->get('delete/(:num)', 'Track::delete/$1', ['as' => 'track-delete']); - // example URI: /track/1/2 - $routes->match(['get', 'post'], 'edit/(:num)/(:num)', 'Track::edit/$1/$2', ['as' => 'track-edit']); + // example URI: /track/1 + $routes->match(['get', 'post'], 'edit/(:num)', 'Track::edit/$1', ['as' => 'track-edit']); }); diff --git a/src/Database/Migrations/2020-03-26-203957_Track.php b/src/Database/Migrations/2020-03-26-203957_Track.php new file mode 100644 index 0000000..0047f4a --- /dev/null +++ b/src/Database/Migrations/2020-03-26-203957_Track.php @@ -0,0 +1,37 @@ +forge->addField([ + 'id' => [ + 'type' => 'BIGINT', + 'unsigned' => true, + 'auto_increment' => true, + ], + 'album_id' => [ + 'type' => 'BIGINT', + 'unsigned' => true, + ], + 'title' => [ + 'type' => 'VARCHAR', + 'constraint' => '255', + ], + 'author' => [ + 'type' => 'VARCHAR', + 'constraint' => '255', + ], + ]); + $this->forge->addKey('id', true); + $this->forge->addForeignKey('album_id', 'album', 'id', 'CASCADE', 'CASCADE'); + $this->forge->createTable('track'); + } + + public function down() + { + $this->forge->dropTable('track'); + } +} diff --git a/src/Models/TrackModel.php b/src/Models/TrackModel.php index a6a4de5..ed3be7b 100644 --- a/src/Models/TrackModel.php +++ b/src/Models/TrackModel.php @@ -10,9 +10,11 @@ class TrackModel extends Model protected $allowedFields = [ 'album_id', 'title', + 'author', ]; protected $validationRules = [ 'album_id' => 'required|numeric|is_unique[album.id,album_id,{album_id}]', 'title' => 'required|alpha_numeric_space|min_length[3]|max_length[255]|is_unique[track.title,id,{id}]', + 'author' => 'required|alpha_numeric_space|min_length[3]|max_length[255]', ]; } diff --git a/src/Views/album/index.php b/src/Views/album/index.php index 362d2d0..b47ed93 100644 --- a/src/Views/album/index.php +++ b/src/Views/album/index.php @@ -41,13 +41,14 @@ title) ?> artist) ?> - id), 'Edit'); ?> + id), 'Edit'); ?> id), 'Delete', ['onclick' => 'return confirm(\'Are you sure?\')'] ); ?> + id), 'Album Tracks', ['style' => 'color: red;']); ?> Date: Fri, 27 Mar 2020 03:44:48 +0700 Subject: [PATCH 06/19] add track migration --- src/Domain/Track/Track.php | 2 +- src/Views/album/index.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Domain/Track/Track.php b/src/Domain/Track/Track.php index ad3c443..10b6c3c 100644 --- a/src/Domain/Track/Track.php +++ b/src/Domain/Track/Track.php @@ -16,7 +16,7 @@ public function __construct(array $data = null) { throw new InvalidArgumentException('album_id key must be exists'); } - $data['title'] = $data['title'] ?? ''; + $data['title'] = $data['title'] ?? ''; parent::__construct($data); } diff --git a/src/Views/album/index.php b/src/Views/album/index.php index b47ed93..6912060 100644 --- a/src/Views/album/index.php +++ b/src/Views/album/index.php @@ -41,7 +41,7 @@ title) ?> artist) ?> - id), 'Edit'); ?> + id), 'Edit'); ?> id), 'Delete', From f421f3a18ea337dc236b2a3f9f7b87e2b87a76b6 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 27 Mar 2020 04:13:58 +0700 Subject: [PATCH 07/19] disable enable foreign key check for running test --- src/Views/track/index.php | 10 +++++----- test/Controller/AlbumTest.php | 12 ++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Views/track/index.php b/src/Views/track/index.php index 362d2d0..5644a2b 100644 --- a/src/Views/track/index.php +++ b/src/Views/track/index.php @@ -1,6 +1,6 @@ setVar('title', $title); // extends layout @@ -31,15 +31,15 @@ Artist   - + No album found. + foreach ($tracks as $track) : ?> - title) ?> - artist) ?> + title) ?> + author) ?> id), 'Edit'); ?> disableForeignKeyChecks(); + } + public function testIndexAlbumHasNoData() { Database::connect()->table('album')->truncate(); @@ -182,4 +188,10 @@ public function testDeleteExistenceAlbum() $this->assertTrue($result->isRedirect()); } + + public function tearDown(): void + { + parent::tearDown(); + \Config\Database::connect()->enableForeignKeyChecks(); + } } From c2c911ee7c001a03ab2cfecafc26c866ebbb2c01 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 27 Mar 2020 07:19:32 +0700 Subject: [PATCH 08/19] disable - enable on truncate album table --- test/Controller/AlbumTest.php | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/test/Controller/AlbumTest.php b/test/Controller/AlbumTest.php index 1a382ec..6e6ea99 100644 --- a/test/Controller/AlbumTest.php +++ b/test/Controller/AlbumTest.php @@ -19,15 +19,11 @@ class AlbumTest extends CIDatabaseTestCase protected $namespace = 'Album'; protected $seed = AlbumSeeder::class; - protected function setUp(): void - { - parent::setUp(); - \Config\Database::connect()->disableForeignKeyChecks(); - } - public function testIndexAlbumHasNoData() { + Database::connect()->disableForeignKeyChecks(); Database::connect()->table('album')->truncate(); + Database::connect()->enableForeignKeyChecks(); $result = $this->controller(Album::class) ->execute('index'); @@ -188,10 +184,4 @@ public function testDeleteExistenceAlbum() $this->assertTrue($result->isRedirect()); } - - public function tearDown(): void - { - parent::tearDown(); - \Config\Database::connect()->enableForeignKeyChecks(); - } } From 8e252502a205649ae1ef755066c4a5363e46fcea Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 27 Mar 2020 07:22:19 +0700 Subject: [PATCH 09/19] add TrackSeeder --- test/Database/Seeds/TrackSeeder.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 test/Database/Seeds/TrackSeeder.php diff --git a/test/Database/Seeds/TrackSeeder.php b/test/Database/Seeds/TrackSeeder.php new file mode 100644 index 0000000..83ef831 --- /dev/null +++ b/test/Database/Seeds/TrackSeeder.php @@ -0,0 +1,18 @@ + 1, + 'album_id' => 1, + 'title' => 'Melompat Lebih Tinggi', + 'author' => 'Eros Chandra', + ]; + + $this->db->table('album')->insert($row); + } +} From 25ffd5bc789fb8c0709aa0e05f0c1e69d7ccde3a Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 27 Mar 2020 07:26:35 +0700 Subject: [PATCH 10/19] seeder data --- test/Database/Seeds/AlbumSeeder.php | 2 +- test/Database/Seeds/TrackSeeder.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/Database/Seeds/AlbumSeeder.php b/test/Database/Seeds/AlbumSeeder.php index 8e3fb01..8f9742e 100644 --- a/test/Database/Seeds/AlbumSeeder.php +++ b/test/Database/Seeds/AlbumSeeder.php @@ -9,7 +9,7 @@ public function run() $row = [ 'id' => 1, 'artist' => 'Sheila On 7', - 'title' => 'Melompat Lebih Tinggi', + 'title' => 'Kisah Klasik Untuk Masa Depan', ]; $this->db->table('album')->insert($row); diff --git a/test/Database/Seeds/TrackSeeder.php b/test/Database/Seeds/TrackSeeder.php index 83ef831..729d6b2 100644 --- a/test/Database/Seeds/TrackSeeder.php +++ b/test/Database/Seeds/TrackSeeder.php @@ -9,8 +9,8 @@ public function run() $row = [ 'id' => 1, 'album_id' => 1, - 'title' => 'Melompat Lebih Tinggi', - 'author' => 'Eros Chandra', + 'title' => 'Sebuah Kisah Klasik', + 'author' => 'Eross Chandra', ]; $this->db->table('album')->insert($row); From 47225bc428e92ef2994e86cb98666750547bba91 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 27 Mar 2020 07:28:45 +0700 Subject: [PATCH 11/19] data --- test/Controller/AlbumTest.php | 4 ++-- .../Persistence/Album/SQLAlbumRepositoryTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/Controller/AlbumTest.php b/test/Controller/AlbumTest.php index 6e6ea99..5282cbd 100644 --- a/test/Controller/AlbumTest.php +++ b/test/Controller/AlbumTest.php @@ -106,7 +106,7 @@ public function testAddAlbumValidData() $request->setMethod('post'); $request->setGlobal('post', [ 'artist' => 'Siti Nurhaliza', - 'title' => 'Purnama Merindu', + 'title' => 'Anugrah Aidilfitri', ]); $result = $this->withRequest($request) @@ -159,7 +159,7 @@ public function testEditAlbumValidData() $request->setMethod('post'); $request->setGlobal('post', [ 'artist' => 'Siti Nurhaliza', - 'title' => 'Purnama Merindu', + 'title' => 'Anugrah Aidilfitri', ]); $result = $this->withRequest($request) diff --git a/test/Database/Infrastructure/Persistence/Album/SQLAlbumRepositoryTest.php b/test/Database/Infrastructure/Persistence/Album/SQLAlbumRepositoryTest.php index 22dbe56..778af76 100644 --- a/test/Database/Infrastructure/Persistence/Album/SQLAlbumRepositoryTest.php +++ b/test/Database/Infrastructure/Persistence/Album/SQLAlbumRepositoryTest.php @@ -80,7 +80,7 @@ public function validData() 'insert' => [ [ 'artist' => 'Siti Nurhaliza', - 'title' => 'Purnama Merindu', + 'title' => 'Anugrah Aidilfitri', ], ], 'update' => [ From 9047e8fedf76921c6ed8dabaa1bba18caeb0fe37 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 27 Mar 2020 07:35:59 +0700 Subject: [PATCH 12/19] move seed to src --- README.md | 7 +++++++ src/Controllers/Track.php | 2 +- {test => src}/Database/Seeds/AlbumSeeder.php | 2 +- {test => src}/Database/Seeds/TrackSeeder.php | 4 ++-- test/Controller/AlbumTest.php | 2 +- .../Persistence/Album/SQLAlbumRepositoryTest.php | 2 +- test/Database/Seeds/.gitkeep | 0 7 files changed, 13 insertions(+), 6 deletions(-) rename {test => src}/Database/Seeds/AlbumSeeder.php (85%) rename {test => src}/Database/Seeds/TrackSeeder.php (73%) delete mode 100644 test/Database/Seeds/.gitkeep diff --git a/README.md b/README.md index 19ee5ac..0617910 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,13 @@ database.default.DBDriver = MySQLi php spark migrate -n Album ``` +**4.** Run db seed (Optional) + +```bash +php spark db:seed "Album\Database\Seeds\AlbumSeeder" +php spark db:seed "Album\Database\Seeds\TrackSeeder" +``` + **4.** Run development server: ```bash diff --git a/src/Controllers/Track.php b/src/Controllers/Track.php index fb986df..e58c6e8 100644 --- a/src/Controllers/Track.php +++ b/src/Controllers/Track.php @@ -1,7 +1,7 @@ 'Eross Chandra', ]; - $this->db->table('album')->insert($row); + $this->db->table('track')->insert($row); } } diff --git a/test/Controller/AlbumTest.php b/test/Controller/AlbumTest.php index 5282cbd..1d2e0ec 100644 --- a/test/Controller/AlbumTest.php +++ b/test/Controller/AlbumTest.php @@ -1,7 +1,7 @@ Date: Fri, 27 Mar 2020 07:46:38 +0700 Subject: [PATCH 13/19] using attributes values in entity --- src/Domain/Album/Album.php | 12 +++++------- src/Domain/Track/Track.php | 16 ++++++---------- src/Views/track/index.php | 2 +- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/Domain/Album/Album.php b/src/Domain/Album/Album.php index b5791a7..f19e3a8 100644 --- a/src/Domain/Album/Album.php +++ b/src/Domain/Album/Album.php @@ -9,13 +9,11 @@ */ class Album extends Entity { - public function __construct(array $data = null) - { - $data['artist'] = $data['artist'] ?? ''; - $data['title'] = $data['title'] ?? ''; - - parent::__construct($data); - } + protected $attributes = [ + 'id' => null, + 'artist' => null, + 'title' => null, + ]; public function setArtist(string $artist): self { diff --git a/src/Domain/Track/Track.php b/src/Domain/Track/Track.php index 10b6c3c..67a8afb 100644 --- a/src/Domain/Track/Track.php +++ b/src/Domain/Track/Track.php @@ -10,16 +10,12 @@ */ class Track extends Entity { - public function __construct(array $data = null) - { - if (! isset($data['album_id'])) - { - throw new InvalidArgumentException('album_id key must be exists'); - } - $data['title'] = $data['title'] ?? ''; - - parent::__construct($data); - } + protected $attributes = [ + 'id' => null, + 'album_id' => null, + 'title' => null, + 'author' => null, + ]; public function setTitle(string $title): self { diff --git a/src/Views/track/index.php b/src/Views/track/index.php index 5644a2b..ad7f432 100644 --- a/src/Views/track/index.php +++ b/src/Views/track/index.php @@ -1,6 +1,6 @@ title); $this->setVar('title', $title); // extends layout From 680e8dc0b772b888b85cd010d3afb043e5c3202c Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 27 Mar 2020 08:08:54 +0700 Subject: [PATCH 14/19] album references --- src/Config/Routes.php | 4 ++-- src/Controllers/Track.php | 9 +++++++-- src/Views/album/add.php | 1 + src/Views/album/edit.php | 3 ++- src/Views/album/index.php | 6 +++--- src/Views/track/edit.php | 21 ++++++++++++--------- src/Views/track/index.php | 12 +++++++----- 7 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/Config/Routes.php b/src/Config/Routes.php index d7e8114..548fe62 100644 --- a/src/Config/Routes.php +++ b/src/Config/Routes.php @@ -22,8 +22,8 @@ $routes->match(['get', 'post'], 'add/(:num)', 'Track::add/$1', ['as' => 'track-add']); // example URI: /track/delete/1 - $routes->get('delete/(:num)', 'Track::delete/$1', ['as' => 'track-delete']); + $routes->get('delete/(:num)/(:num)', 'Track::delete/$1/$2', ['as' => 'track-delete']); // example URI: /track/1 - $routes->match(['get', 'post'], 'edit/(:num)', 'Track::edit/$1', ['as' => 'track-edit']); + $routes->match(['get', 'post'], 'edit/(:num)/(:num)', 'Track::edit/$1/$2', ['as' => 'track-edit']); }); diff --git a/src/Controllers/Track.php b/src/Controllers/Track.php index e58c6e8..516a8d3 100644 --- a/src/Controllers/Track.php +++ b/src/Controllers/Track.php @@ -33,6 +33,7 @@ public function index(int $albumId) } $data['keyword'] = $this->request->getGet('keyword') ?? ''; + $data['album'] = $album; $data['tracks'] = $this->trackRepository->findPaginatedData($album, $data['keyword']); $data['pager'] = $this->trackRepository->pager(); @@ -70,7 +71,7 @@ public function edit(int $albumId, int $trackId) { try { - $this->albumRepository->findAlbumOfId($albumId); + $album = $this->albumRepository->findAlbumOfId($albumId); $track = $this->trackRepository->findTrackOfId($trackId); } catch (RecordNotFoundException $e) @@ -91,7 +92,11 @@ public function edit(int $albumId, int $trackId) return redirect()->withInput()->back(); } - return view('Album\Views\track\edit', ['track' => $track, 'errors' => session()->getFlashData('errors')]); + return view('Album\Views\track\edit', [ + 'album' => $album, + 'track' => $track, + 'errors' => session()->getFlashData('errors') + ]); } public function delete(int $albumId, int $trackId) diff --git a/src/Views/album/add.php b/src/Views/album/add.php index cc83974..7006632 100644 --- a/src/Views/album/add.php +++ b/src/Views/album/add.php @@ -28,6 +28,7 @@ echo form_close(); ?> +
title)); echo $errors['title'] ?? ''; -echo form_submit('Save', 'Save New Album'); +echo form_submit('Save', 'Update Album'); echo form_close(); ?> +
title) ?> artist) ?> - id), 'Edit'); ?> + id), 'Album Tracks Details'); ?>  |  + id), 'Edit'); ?>  |  id), 'Delete', - ['onclick' => 'return confirm(\'Are you sure?\')'] + ['onclick' => 'return confirm(\'Track records will also deleted, are you sure?\')'] ); ?> - id), 'Album Tracks', ['style' => 'color: red;']); ?> artist, $album->title); $this->setVar('title', $title); // extends layout @@ -15,21 +15,24 @@ id)); -echo form_label('Artist', 'artist'); -echo form_hidden('id', set_value('id', $album->id)); -echo form_input('artist', set_value('artist', $album->artist)); -echo $errors['artist'] ?? ''; +echo form_open(sprintf('track/edit/%d/%d', $album->id, $track->id)); +echo form_hidden('album_id', set_value('album_id', $album->id)); +echo form_hidden('id', set_value('id', $track->id)); echo form_label('Title', 'title'); -echo form_input('title', set_value('title', $album->title)); +echo form_input('title', set_value('title', $track->title)); echo $errors['title'] ?? ''; -echo form_submit('Save', 'Save New Album'); +echo form_label('Author', 'author'); +echo form_input('author', set_value('author', $track->author)); +echo $errors['author'] ?? ''; + +echo form_submit('Save', 'Update Album Track'); echo form_close(); ?> - +
+id), sprintf('Back to Track Index of %s:%s', $album->artist, $album->title)); ?> title); +$title = sprintf('Album Tracks of %s:%s', $album->artist, $album->title); $this->setVar('title', $title); // extends layout @@ -11,7 +11,9 @@ ?>

- + +  |  + id), 'Add new album track'); ?>

Title - Artist + Author   @@ -41,9 +43,9 @@ title) ?> author) ?> - id), 'Edit'); ?> + id, $track->id), 'Edit'); ?> |  id), + sprintf('track/delete/%d/%d', $album->id, $track->id), 'Delete', ['onclick' => 'return confirm(\'Are you sure?\')'] ); From 4d3d056825caf916f166c784ddfc2f41ee84a0ef Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 27 Mar 2020 08:16:21 +0700 Subject: [PATCH 15/19] add track functionality --- src/Controllers/Track.php | 5 ++++- src/Models/TrackModel.php | 2 +- src/Views/track/add.php | 17 ++++++++++------- src/Views/track/index.php | 2 +- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/Controllers/Track.php b/src/Controllers/Track.php index 516a8d3..03ccd7a 100644 --- a/src/Controllers/Track.php +++ b/src/Controllers/Track.php @@ -64,7 +64,10 @@ public function add(int $albumId) return redirect()->withInput()->back(); } - return view('Album\Views\track\add', ['albumId' => $album->id, 'errors' => session()->getFlashData('errors')]); + return view('Album\Views\track\add', [ + 'album' => $album, + 'errors' => session()->getFlashData('errors') + ]); } public function edit(int $albumId, int $trackId) diff --git a/src/Models/TrackModel.php b/src/Models/TrackModel.php index ed3be7b..be90a27 100644 --- a/src/Models/TrackModel.php +++ b/src/Models/TrackModel.php @@ -13,7 +13,7 @@ class TrackModel extends Model 'author', ]; protected $validationRules = [ - 'album_id' => 'required|numeric|is_unique[album.id,album_id,{album_id}]', + 'album_id' => 'required|numeric', 'title' => 'required|alpha_numeric_space|min_length[3]|max_length[255]|is_unique[track.title,id,{id}]', 'author' => 'required|alpha_numeric_space|min_length[3]|max_length[255]', ]; diff --git a/src/Views/track/add.php b/src/Views/track/add.php index 00492c6..67c3edd 100644 --- a/src/Views/track/add.php +++ b/src/Views/track/add.php @@ -1,6 +1,6 @@ artist, $album->title); $this->setVar('title', $title); // extends layout @@ -15,20 +15,23 @@ id)); +echo form_hidden('album_id', set_value('album_id', $album->id)); echo form_label('Title', 'title'); echo form_input('title', set_value('title')); echo $errors['title'] ?? ''; -echo form_submit('Save', 'Save New Album'); +echo form_label('Author', 'author'); +echo form_input('author', set_value('author')); +echo $errors['author'] ?? ''; + +echo form_submit('Save', 'Save New Album Track'); echo form_close(); ?> - +
+id), sprintf('Back to Track Index of %s:%s', $album->artist, $album->title)); ?> - No album found. + No album track found. From 95d289d309e62978b92f4df4d68b98f15ecfb393 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 27 Mar 2020 08:16:30 +0700 Subject: [PATCH 16/19] add track functionality --- src/Controllers/Track.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Controllers/Track.php b/src/Controllers/Track.php index 03ccd7a..023c09d 100644 --- a/src/Controllers/Track.php +++ b/src/Controllers/Track.php @@ -65,8 +65,8 @@ public function add(int $albumId) } return view('Album\Views\track\add', [ - 'album' => $album, - 'errors' => session()->getFlashData('errors') + 'album' => $album, + 'errors' => session()->getFlashData('errors'), ]); } @@ -96,9 +96,9 @@ public function edit(int $albumId, int $trackId) } return view('Album\Views\track\edit', [ - 'album' => $album, - 'track' => $track, - 'errors' => session()->getFlashData('errors') + 'album' => $album, + 'track' => $track, + 'errors' => session()->getFlashData('errors'), ]); } From b29d5b470a898d6d4ba0115248d79a5b7344f1d1 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 27 Mar 2020 10:22:12 +0700 Subject: [PATCH 17/19] functional --- src/Domain/Track/Track.php | 6 +++++ .../Persistence/Album/SQLAlbumRepository.php | 3 ++- .../Persistence/Track/SQLTrackRepository.php | 9 ++++--- src/Views/album/index.php | 4 +-- src/Views/layout.php | 25 +++++++++++++++++++ src/Views/track/index.php | 6 ++--- 6 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/Domain/Track/Track.php b/src/Domain/Track/Track.php index 67a8afb..d5c80fc 100644 --- a/src/Domain/Track/Track.php +++ b/src/Domain/Track/Track.php @@ -22,4 +22,10 @@ public function setTitle(string $title): self $this->attributes['title'] = ucwords($title); return $this; } + + public function setAuthor(string $author): self + { + $this->attributes['author'] = ucwords($author); + return $this; + } } diff --git a/src/Infrastructure/Persistence/Album/SQLAlbumRepository.php b/src/Infrastructure/Persistence/Album/SQLAlbumRepository.php index 76dd7b0..f31f2e5 100644 --- a/src/Infrastructure/Persistence/Album/SQLAlbumRepository.php +++ b/src/Infrastructure/Persistence/Album/SQLAlbumRepository.php @@ -19,7 +19,8 @@ public function findPaginatedData(string $keyword = ''): ?array { if ($keyword) { - $this->model->builder() + $this->model + ->builder() ->groupStart() ->like('artist', $keyword) ->orLike('title', $keyword) diff --git a/src/Infrastructure/Persistence/Track/SQLTrackRepository.php b/src/Infrastructure/Persistence/Track/SQLTrackRepository.php index 6582aa5..f80c767 100644 --- a/src/Infrastructure/Persistence/Track/SQLTrackRepository.php +++ b/src/Infrastructure/Persistence/Track/SQLTrackRepository.php @@ -18,14 +18,17 @@ public function __construct(TrackModel $model) public function findPaginatedData(Album $album, string $keyword = ''): ?array { + $this->model + ->builder() + ->where('album_id', $album->id); + if ($keyword) { $this->model ->builder() - ->where('album_id', $album->id) ->groupStart() - ->like('artist', $keyword) - ->orLike('title', $keyword) + ->like('title', $keyword) + ->orLike('author', $keyword) ->groupEnd(); } diff --git a/src/Views/album/index.php b/src/Views/album/index.php index ed2e90f..da666b4 100644 --- a/src/Views/album/index.php +++ b/src/Views/album/index.php @@ -29,11 +29,11 @@ Title Artist -   + Options - No album found. + No album found. diff --git a/src/Views/layout.php b/src/Views/layout.php index d506b05..529a3b0 100644 --- a/src/Views/layout.php +++ b/src/Views/layout.php @@ -2,6 +2,31 @@ CI4 App - <?php echo $this->getData()['title'] ?? ''; ?> + renderSection('content') ?> diff --git a/src/Views/track/index.php b/src/Views/track/index.php index 69d948f..00f6c49 100644 --- a/src/Views/track/index.php +++ b/src/Views/track/index.php @@ -18,7 +18,7 @@ 'get']); +echo form_open(sprintf('track/%d', $album->id), ['method' => 'get']); echo form_input('keyword', esc($keyword), ['placeholder' => 'Search keyword']); echo form_close(); ?> @@ -31,11 +31,11 @@ Title Author -   + Options - No album track found. + No album track found. From 925684ff0fc03542676771e3c4d4cb1b4ad3e558 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 27 Mar 2020 11:00:16 +0700 Subject: [PATCH 18/19] done --- src/Domain/Track/Track.php | 7 +- test/Controller/AlbumTest.php | 1 + test/Controller/TrackTest.php | 210 ++++++++++++++++++ .../Track/SQLTrackRepositoryTest.php | 160 +++++++++++++ test/unit/Domain/Album/AlbumTest.php | 4 +- test/unit/Domain/Track/TrackTest.php | 34 +++ 6 files changed, 413 insertions(+), 3 deletions(-) create mode 100644 test/Controller/TrackTest.php create mode 100644 test/Database/Infrastructure/Persistence/Track/SQLTrackRepositoryTest.php create mode 100644 test/unit/Domain/Track/TrackTest.php diff --git a/src/Domain/Track/Track.php b/src/Domain/Track/Track.php index d5c80fc..dd41a61 100644 --- a/src/Domain/Track/Track.php +++ b/src/Domain/Track/Track.php @@ -1,7 +1,6 @@ null, ]; + public function setAlbumId(int $album_id): self + { + $this->attributes['album_id'] = $album_id; + return $this; + } + public function setTitle(string $title): self { $this->attributes['title'] = ucwords($title); diff --git a/test/Controller/AlbumTest.php b/test/Controller/AlbumTest.php index 1d2e0ec..47a9cd6 100644 --- a/test/Controller/AlbumTest.php +++ b/test/Controller/AlbumTest.php @@ -158,6 +158,7 @@ public function testEditAlbumValidData() $request = Services::request(); $request->setMethod('post'); $request->setGlobal('post', [ + 'id' => 1, 'artist' => 'Siti Nurhaliza', 'title' => 'Anugrah Aidilfitri', ]); diff --git a/test/Controller/TrackTest.php b/test/Controller/TrackTest.php new file mode 100644 index 0000000..1eabbb5 --- /dev/null +++ b/test/Controller/TrackTest.php @@ -0,0 +1,210 @@ +controller(Track::class) + ->execute('index', 2); + + $this->assertEquals(404, $result->response()->getStatusCode()); + } + + public function testIndexTrackHasNoData() + { + Database::connect()->disableForeignKeyChecks(); + Database::connect()->table('track')->truncate(); + Database::connect()->enableForeignKeyChecks(); + + $result = $this->controller(Track::class) + ->execute('index', 1); + + $this->assertTrue($result->isOK()); + $this->assertTrue($result->see('No album track found.')); + } + + public function testIndexTrackHasData() + { + $result = $this->controller(Track::class) + ->execute('index', 1); + + $this->assertTrue($result->isOK()); + $this->assertTrue($result->see('Eross')); + } + + public function testIndexSearchTrackFound() + { + $request = Services::request(); + $request->setMethod('get'); + $request->setGlobal('get', [ + 'keyword' => 'kisah', + ]); + + $result = $this->withRequest($request) + ->controller(Track::class) + ->execute('index', 1); + + $this->assertTrue($result->see('Sebuah Kisah Klasik')); + } + + public function testIndexSearchTrackNotFound() + { + $request = Services::request(); + $request->setMethod('get'); + $request->setGlobal('get', [ + 'keyword' => 'Purnama', + ]); + + $result = $this->withRequest($request) + ->controller(Track::class) + ->execute('index', 1); + + $this->assertTrue($result->see('No album track found.')); + } + + public function testAddTrackByNotFoundAlbum() + { + $result = $this->controller(Track::class) + ->execute('add', 2); + + $this->assertEquals(404, $result->response()->getStatusCode()); + } + + public function testAddTrack() + { + $result = $this->controller(Track::class) + ->execute('add', 1); + + $this->assertTrue($result->isOK()); + } + + public function testAddTrackInvalidData() + { + $request = Services::request(null, false); + $request->setMethod('post'); + + $result = $this->withRequest($request) + ->controller(Track::class) + ->execute('add', 1); + $this->assertTrue($result->isRedirect()); + + $request = Services::request(null, false); + $request->setMethod('get'); + + $result = $this->withRequest($request) + ->controller(Track::class) + ->execute('add', 1); + $this->assertTrue($result->isOK()); + $this->assertTrue($result->see('The title field is required.')); + $this->assertTrue($result->see('The author field is required.')); + } + + public function testAddTrackValidData() + { + $request = Services::request(); + $request->setMethod('post'); + $request->setGlobal('post', [ + 'album_id' => 1, + 'title' => 'Sahabat Sejati', + 'author' => 'Erros Chandra', + ]); + + $result = $this->withRequest($request) + ->controller(Track::class) + ->execute('add', 1); + + $this->assertTrue($result->isRedirect()); + } + + public function testEditUnexistenceTrack() + { + $result = $this->controller(Track::class) + ->execute('edit', rand(1000, 2000), rand(1000, 2000)); + + $this->assertEquals(404, $result->response()->getStatusCode()); + } + + public function testEditExistenceTrack() + { + $result = $this->controller(Track::class) + ->execute('edit', 1, 1); + + $this->assertTrue($result->isOK()); + } + + public function testEditTrackInvalidData() + { + $request = Services::request(null, false); + $request->setMethod('post'); + + $result = $this->withRequest($request) + ->controller(Track::class) + ->execute('edit', 1, 1); + $this->assertTrue($result->isRedirect()); + + $request = Services::request(null, false); + $request->setMethod('get'); + + $result = $this->withRequest($request) + ->controller(Track::class) + ->execute('edit', 1, 1); + $this->assertTrue($result->isOK()); + $this->assertTrue($result->see('The title field is required.')); + $this->assertTrue($result->see('The author field is required.')); + } + + public function testEditTrackValidData() + { + $request = Services::request(); + $request->setMethod('post'); + $request->setGlobal('post', [ + 'id' => 1, + 'album_id' => 1, + 'title' => 'Temani Aku', + 'author' => 'Erros Chandra', + ]); + + $result = $this->withRequest($request) + ->controller(Track::class) + ->execute('edit', 1, 1); + + $this->assertTrue($result->isRedirect()); + } + + public function testDeleteUnexistenceTrack() + { + $result = $this->controller(Track::class) + ->execute('delete', rand(1000, 2000), rand(1000, 2000)); + + $this->assertEquals(404, $result->response()->getStatusCode()); + } + + public function testDeleteExistenceTrack() + { + $result = $this->controller(Track::class) + ->execute('delete', 1, 1); + + $this->assertTrue($result->isRedirect()); + } +} diff --git a/test/Database/Infrastructure/Persistence/Track/SQLTrackRepositoryTest.php b/test/Database/Infrastructure/Persistence/Track/SQLTrackRepositoryTest.php new file mode 100644 index 0000000..1d3906f --- /dev/null +++ b/test/Database/Infrastructure/Persistence/Track/SQLTrackRepositoryTest.php @@ -0,0 +1,160 @@ +repository = Services::trackRepository(); + } + + public function testPagerIsNullBeforeFindPaginatedDataCalled() + { + $this->assertNull($this->repository->pager()); + } + + public function testfindPaginatedDataWithKeywordNotFoundInDatabase() + { + $album = new Album(); + $album->id = 1; + + $tracks = $this->repository->findPaginatedData($album, 'Pak Ngah'); + $this->assertEmpty($tracks); + } + + public function testfindPaginatedDataWithKeywordFoundInDatabase() + { + $album = new Album(); + $album->id = 1; + + $tracks = $this->repository->findPaginatedData($album, 'Eross Chandra'); + $this->assertNotEmpty($tracks); + } + + public function testPager() + { + $this->assertInstanceOf(PagerInterface::class, $this->repository->pager()); + } + + public function testFindTrackOfIdWithNotFoundIdInDatabase() + { + $this->expectException(TrackNotFoundException::class); + $this->repository->findTrackOfId(rand(1000, 2000)); + } + + public function testFindTrackOfIdWithFoundIdInDatabase() + { + $this->assertInstanceOf(Track::class, $this->repository->findTrackOfId(1)); + } + + public function invalidData() + { + return [ + 'empty array' => [ + [], + ], + 'null' => [ + null + ], + ]; + } + + /** + * @dataProvider invalidData + */ + public function testSaveInvalidData($data) + { + $this->assertFalse($this->repository->save($data)); + } + + public function validData() + { + return [ + 'insert' => [ + [ + 'album_id' => 1, + 'title' => 'Sahabat Sejati', + 'author' => 'Erros Chandra', + ], + ], + 'update' => [ + [ + 'id' => 1, + 'album_id' => 1, + 'title' => 'Temani Aku', + 'author' => 'Erros Chandra', + ], + ], + ]; + } + + /** + * @dataProvider validData + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function testSaveValidData(array $data) + { + $this->assertTrue($this->repository->save($data)); + } + + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function testErrorIsNullOnNoSaveCalled() + { + $this->assertNull($this->repository->errors()); + } + + /** + * @dataProvider validData + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function testErrorIsNullAfterSaveCalledWithValidData($data) + { + $this->repository->save($data); + $this->assertNull($this->repository->errors()); + } + + /** + * @dataProvider invalidData + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function testErrorIsArrayAfterSaveCalledWithInValidData($data) + { + $this->repository->save($data); + $this->assertIsArray($this->repository->errors()); + } + + public function testDeleteTrackOfIdWithNotFoundIdInDatabase() + { + $this->expectException(TrackNotFoundException::class); + $this->repository->deleteOfId(rand(1000, 2000)); + } + + public function testDeleteTrackOfIdWithFoundIdInDatabase() + { + $this->assertTrue($this->repository->deleteOfId(1)); + } +} diff --git a/test/unit/Domain/Album/AlbumTest.php b/test/unit/Domain/Album/AlbumTest.php index 48683ac..38fa06e 100644 --- a/test/unit/Domain/Album/AlbumTest.php +++ b/test/unit/Domain/Album/AlbumTest.php @@ -22,7 +22,7 @@ public function testSetArtist() public function testSetTitle() { - $this->entity->setTitle('melompat lebih tinggi'); - $this->assertEquals('Melompat Lebih Tinggi', $this->entity->title); + $this->entity->setTitle('kisah klasik untuk masa depan'); + $this->assertEquals('Kisah Klasik Untuk Masa Depan', $this->entity->title); } } diff --git a/test/unit/Domain/Track/TrackTest.php b/test/unit/Domain/Track/TrackTest.php new file mode 100644 index 0000000..378a3f2 --- /dev/null +++ b/test/unit/Domain/Track/TrackTest.php @@ -0,0 +1,34 @@ +entity = new Track(); + } + + public function testSetAlbumId() + { + $this->entity->setAlbumId(1); + $this->assertEquals(1, $this->entity->album_id); + } + + public function testSetTitle() + { + $this->entity->setTitle('sebuah kisah klasik'); + $this->assertEquals('Sebuah Kisah Klasik', $this->entity->title); + } + + public function testSetAuthor() + { + $this->entity->setAuthor('eross chandra'); + $this->assertEquals('Eross Chandra', $this->entity->author); + } +} From 92e4824d8fd204d4d4ded09fc28268676a8d6a6b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 27 Mar 2020 11:04:40 +0700 Subject: [PATCH 19/19] cs --- src/Domain/Track/Track.php | 4 ++-- src/Views/track/add.php | 6 +++++- src/Views/track/edit.php | 6 +++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Domain/Track/Track.php b/src/Domain/Track/Track.php index dd41a61..e86e37b 100644 --- a/src/Domain/Track/Track.php +++ b/src/Domain/Track/Track.php @@ -16,9 +16,9 @@ class Track extends Entity 'author' => null, ]; - public function setAlbumId(int $album_id): self + public function setAlbumId(int $albumId): self { - $this->attributes['album_id'] = $album_id; + $this->attributes['album_id'] = $albumId; return $this; } diff --git a/src/Views/track/add.php b/src/Views/track/add.php index 67c3edd..3f13a25 100644 --- a/src/Views/track/add.php +++ b/src/Views/track/add.php @@ -31,7 +31,11 @@ ?>
-id), sprintf('Back to Track Index of %s:%s', $album->artist, $album->title)); ?> +id), + sprintf('Back to Track Index of %s:%s', $album->artist, $album->title) + ); +?>
-id), sprintf('Back to Track Index of %s:%s', $album->artist, $album->title)); ?> +id), + sprintf('Back to Track Index of %s:%s', $album->artist, $album->title) + ); +?>