From 8f86c07f5af4d6d9953f4e4eb986378ba855df8a Mon Sep 17 00:00:00 2001 From: Mark Hester Date: Sun, 2 Sep 2018 12:07:21 +0100 Subject: [PATCH 1/7] refresh database on test. --- tests/Article/ArticleBackendTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Article/ArticleBackendTest.php b/tests/Article/ArticleBackendTest.php index 49b50c9b..8f1fddde 100644 --- a/tests/Article/ArticleBackendTest.php +++ b/tests/Article/ArticleBackendTest.php @@ -12,7 +12,7 @@ class ArticleBackendTest extends TestCase { - //use RefreshDatabase; + use RefreshDatabase; /** * @test From e1da358eb1084d666b168bbbdb323fc2a9e0fc1b Mon Sep 17 00:00:00 2001 From: Mark Hester Date: Mon, 3 Sep 2018 15:37:24 +0100 Subject: [PATCH 2/7] refresh database on test. --- app/Model/Categories.php | 12 ++++- app/Modules/Articles/BackendController.php | 38 +--------------- app/Modules/Articles/Routes/backend.php | 8 ++-- database/factories/UserFactory.php | 1 - tests/Article/ArticleBackendTest.php | 51 ++++++++++++++++++++++ 5 files changed, 68 insertions(+), 42 deletions(-) diff --git a/app/Model/Categories.php b/app/Model/Categories.php index 448d1175..d0ed5b48 100644 --- a/app/Model/Categories.php +++ b/app/Model/Categories.php @@ -67,7 +67,7 @@ class Categories extends Model /** * Undocumented function. * - * @return void + * @return string */ public function getRouteKeyName() { @@ -101,4 +101,14 @@ public function editor() { return $this->belongsTo(Account::class, 'editor_id', 'id'); } + + /** + * @param string $slug + * @return + * @throws \Exception + */ + public static function deleteCategory(string $slug) + { + return Categories::query()->where('slug', $slug)->first()->delete(); + } } diff --git a/app/Modules/Articles/BackendController.php b/app/Modules/Articles/BackendController.php index 30bb8b7d..ca902774 100644 --- a/app/Modules/Articles/BackendController.php +++ b/app/Modules/Articles/BackendController.php @@ -64,7 +64,7 @@ public function create() * Generate a form for editing or creating a model. * * @param Article $article model to be used. - * @return void + * @return \Illuminate\Contracts\View\View */ public function form(Article $article) { @@ -89,17 +89,6 @@ public function store(Request $request) return redirect()->route('admin.articles.index'); } - /** - * Display the specified resource. - * - * @param int $id - * @return \Illuminate\Http\Response - */ - public function show($id) - { - // - } - /** * Show the form for editing the specified resource. * @@ -148,31 +137,6 @@ public function destroy($slug) return response()->json(['status' => 'true', 'redirect' => route('admin.articles.index')]); } - /** - * Category Index. - * @param ArticleCategoryRepository $categoryRepository - * @return \Illuminate\Contracts\View\View - */ - public function categories(ArticleCategoryRepository $categoryRepository) - { - return $this->make('categories')->with('categories', $categoryRepository->all()); - } - - /** - * @param Request $request - * @return \Illuminate\Http\RedirectResponse - */ - public function categories_store(Request $request) - { - $this->validate($request, ['unique:title']); - - $category = new Categories; - $category->title = $request['name']; - $category->save(); - - return redirect()->route('admin.articles.categories.index'); - } - /** * @param int $id * @param ArticleCategoryRepository $categoryRepository diff --git a/app/Modules/Articles/Routes/backend.php b/app/Modules/Articles/Routes/backend.php index a4b7625a..a814a629 100644 --- a/app/Modules/Articles/Routes/backend.php +++ b/app/Modules/Articles/Routes/backend.php @@ -12,6 +12,8 @@ | allowing these to be loading one time per update. | */ - Route::get('admin/articles/categories')->uses('BackendController@categories')->name('admin.articles.categories.index'); - Route::post('admin/articles/categories')->uses('BackendController@categories_store')->name('admin.articles.categories.store'); - Route::resource('admin/articles', 'BackendController', ['as' => 'admin']); + //Route::get('admin/articles/categories')->uses('BackendController@categories')->name('admin.articles.categories.index'); + //Route::post('admin/articles/categories')->uses('BackendController@categories_store')->name('admin.articles.categories.store'); + + Route::resource('admin/articles', 'BackendController', ['as' => 'admin'])->except(['show']); + Route::resource('admin/articles/categories', 'CategoryController', ['as' => 'admin.articles'])->except(['show']); diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index a224241c..8bec436b 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -88,7 +88,6 @@ $factory->define(App\Model\Categories::class, function (Faker $faker) { $title = $faker->word; $creator = factory('App\Model\Account')->create()->id; - return [ 'title' => $title, 'status' => $faker->boolean, diff --git a/tests/Article/ArticleBackendTest.php b/tests/Article/ArticleBackendTest.php index 8f1fddde..98e03b5e 100644 --- a/tests/Article/ArticleBackendTest.php +++ b/tests/Article/ArticleBackendTest.php @@ -14,6 +14,57 @@ class ArticleBackendTest extends TestCase { use RefreshDatabase; + /** + * @test + */ + public function index_should_return_an_article_listing() + { + // the database should have an article. + $article = factory('App\Model\Article')->create(); + + // so that when a user signs in. + $this->signIn(); + + // and visits the articles listing. + $response = $this->visit('admin/articles'); + + // they should see the article in the database on the page. + $response->assertSee(ucwords($article->title)); + } + + /** + * @test + */ + public function a_form_can_be_used_to_create_articles() + { + // when a user signs in. + $this->signIn(); + + // they can visit a url. + $response = $this->visit('admin/articles/create'); + + // and see a form. + $response->assertSee(''); + } + + /** + * @test + */ + public function a_form_can_be_used_to_edit_articles() + { + // when a user signs in. + $this->signIn(); + + // looks up an already existing article. + $article = factory('App\Model\Article')->create(); + + // and attempts to edit. + $response = $this->get("admin/articles/{$article->slug}/edit"); + + // they should see a form. + $response->assertSee(''); + } + /** * @test * @throws \Exception From 81c1a2e3e7728706289e296e93eca287c5ce1229 Mon Sep 17 00:00:00 2001 From: Mark Hester Date: Mon, 3 Sep 2018 16:34:38 +0100 Subject: [PATCH 3/7] Category index now works like it should. --- tests/Article/ArticleCategoryBackendTest.php | 60 ++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tests/Article/ArticleCategoryBackendTest.php diff --git a/tests/Article/ArticleCategoryBackendTest.php b/tests/Article/ArticleCategoryBackendTest.php new file mode 100644 index 00000000..f1e63ed4 --- /dev/null +++ b/tests/Article/ArticleCategoryBackendTest.php @@ -0,0 +1,60 @@ +signIn(); + + $category = factory('App\Model\Categories')->create(); + + $response = $this->visit('admin/articles/categories'); + + $response->assertSee($category->title); + } + + /** + * @test + * @var Categories $category + */ + public function a_category_can_be_created_and_stored() + { + $this->signIn(); + + $this->post('admin/articles/categories', ['title' => 'foo']); + + $this->assertDatabaseHas('article_categories', ['title' => 'foo', 'slug' => 'foo']); + } + + /** + * @test + */ + public function an_exiting_category_can_be_deleted() + { + $this->signIn(); + + $category = factory('App\Model\Categories')->create(); + + $this->delete("admin/articles/categories/{$category->slug}"); + + $this->assertDatabaseHas('article_categories', ['title' => $category->title, 'deleted_at' => now()]); + } +} From 763c6311d3de5e0fac02aeabd3d9a95634b8e09c Mon Sep 17 00:00:00 2001 From: Mark Hester Date: Mon, 3 Sep 2018 16:34:53 +0100 Subject: [PATCH 4/7] Category index now works like it should. --- app/Modules/Articles/BackendController.php | 16 ----- .../Articles/Blade/categories.blade.php | 10 +-- app/Modules/Articles/CategoryController.php | 61 +++++++++++++++++++ 3 files changed, 63 insertions(+), 24 deletions(-) create mode 100644 app/Modules/Articles/CategoryController.php diff --git a/app/Modules/Articles/BackendController.php b/app/Modules/Articles/BackendController.php index ca902774..39df1ce8 100644 --- a/app/Modules/Articles/BackendController.php +++ b/app/Modules/Articles/BackendController.php @@ -137,22 +137,6 @@ public function destroy($slug) return response()->json(['status' => 'true', 'redirect' => route('admin.articles.index')]); } - /** - * @param int $id - * @param ArticleCategoryRepository $categoryRepository - * @return \Illuminate\Http\RedirectResponse - * - * @throws \Exception - */ - public function categories_destroy(int $id, ArticleCategoryRepository $categoryRepository) - { - $category = $categoryRepository->whereID($id); - - $category->delete(); - - return redirect()->route('admin.articles.categories.index'); - } - /** * Save the data for the menu to the database. * diff --git a/app/Modules/Articles/Blade/categories.blade.php b/app/Modules/Articles/Blade/categories.blade.php index a3664059..fd036140 100644 --- a/app/Modules/Articles/Blade/categories.blade.php +++ b/app/Modules/Articles/Blade/categories.blade.php @@ -19,7 +19,7 @@