diff --git a/app/Article.php b/app/Article.php index 1129acde..66c135d9 100644 --- a/app/Article.php +++ b/app/Article.php @@ -18,6 +18,8 @@ class Article extends Model */ protected $table = 'articles'; + protected $fillable = ['title', 'slug', 'description', 'content']; + /** * The attributes that should be mutated to dates. * diff --git a/app/Http/Controllers/Api/ArticleController.php b/app/Http/Controllers/Api/ArticleController.php index 5256dcba..5892ce24 100644 --- a/app/Http/Controllers/Api/ArticleController.php +++ b/app/Http/Controllers/Api/ArticleController.php @@ -61,12 +61,8 @@ public function store(ArticleRequest $request) { $user = $request->user(); - $article = new Article(); - - $article->title = $request->get('title'); + $article = new Article($request->validated()); $article->slug = str_slug($request->get('title')); - $article->description = $request->get('description'); - $article->content = $request->get('content'); $user->articles()->save($article); @@ -111,14 +107,10 @@ public function update(ArticleRequest $request, $id) { $article = Article::findOrFail($id); - $article->slug = $request->get('slug'); - $article->title = $request->get('title'); - $article->description = $request->get('description'); - $article->content = $request->get('content'); - $article->published = $request->get('published'); - $article->published_at = $request->get('published_at'); + $data = $request->validated(); + $data['slug'] = str_slug($data['title']); - $article->save(); + $article->update($data); return response()->json($article, 200); } diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php index 68a56338..a167eb65 100644 --- a/app/Http/Controllers/Api/UserController.php +++ b/app/Http/Controllers/Api/UserController.php @@ -15,12 +15,7 @@ public function update(UserRequest $request, $id) { $user = User::findOrFail($id); - $user->name = $request->get('name'); - $user->email = $request->get('email'); - $user->phone = $request->get('phone'); - $user->about = $request->get('about'); - - $user->save(); + $user->update($request->validated()); return response()->json([ 'user' => $user diff --git a/app/User.php b/app/User.php index 2451e0c5..c5327bb1 100644 --- a/app/User.php +++ b/app/User.php @@ -16,7 +16,7 @@ class User extends Authenticatable * @var array */ protected $fillable = [ - 'name', 'email', 'password', + 'name', 'email', 'phone', 'about', 'password', ]; /** diff --git a/package-lock.json b/package-lock.json index 4c12423e..6b48c879 100644 --- a/package-lock.json +++ b/package-lock.json @@ -393,7 +393,7 @@ "dev": true, "requires": { "browserslist": "2.5.1", - "caniuse-lite": "1.0.30000752", + "caniuse-lite": "1.0.30000755", "normalize-range": "0.1.2", "num2fraction": "1.2.2", "postcss": "6.0.13", @@ -4443,7 +4443,7 @@ "integrity": "sha512-jAvM2ku7YDJ+leAq3bFH1DE0Ylw+F+EQDq4GkqZfgPEqpWYw9ofQH85uKSB9r3Tv7XDbfqVtE+sdvKJW7IlPJA==", "dev": true, "requires": { - "caniuse-lite": "1.0.30000752", + "caniuse-lite": "1.0.30000755", "electron-to-chromium": "1.3.27" } }, @@ -4588,7 +4588,7 @@ "dev": true, "requires": { "browserslist": "1.7.7", - "caniuse-db": "1.0.30000752", + "caniuse-db": "1.0.30000755", "lodash.memoize": "4.1.2", "lodash.uniq": "4.5.0" }, @@ -4599,22 +4599,22 @@ "integrity": "sha1-C9dnBCWL6CmyOYu1Dkti0aFmsLk=", "dev": true, "requires": { - "caniuse-db": "1.0.30000752", + "caniuse-db": "1.0.30000755", "electron-to-chromium": "1.3.27" } } } }, "caniuse-db": { - "version": "1.0.30000752", - "resolved": "https://registry.npmjs.org/caniuse-db/-/caniuse-db-1.0.30000752.tgz", - "integrity": "sha1-2qEp1UznAOlK0UOIve9RTsHi3VQ=", + "version": "1.0.30000755", + "resolved": "https://registry.npmjs.org/caniuse-db/-/caniuse-db-1.0.30000755.tgz", + "integrity": "sha1-oIxUfDnb5K0H3MqXY/y7/wyJHeA=", "dev": true }, "caniuse-lite": { - "version": "1.0.30000752", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30000752.tgz", - "integrity": "sha1-CnnfUgZp2S3cf1dAbu2TWUgmMTA=", + "version": "1.0.30000755", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30000755.tgz", + "integrity": "sha1-nOX24GvXXsggmr6IU8O+7wIkjWU=", "dev": true }, "capture-stack-trace": { @@ -5501,7 +5501,7 @@ "dev": true, "requires": { "browserslist": "1.7.7", - "caniuse-db": "1.0.30000752", + "caniuse-db": "1.0.30000755", "normalize-range": "0.1.2", "num2fraction": "1.2.2", "postcss": "5.2.18", @@ -5514,7 +5514,7 @@ "integrity": "sha1-C9dnBCWL6CmyOYu1Dkti0aFmsLk=", "dev": true, "requires": { - "caniuse-db": "1.0.30000752", + "caniuse-db": "1.0.30000755", "electron-to-chromium": "1.3.27" } }, @@ -12013,7 +12013,7 @@ "integrity": "sha1-C9dnBCWL6CmyOYu1Dkti0aFmsLk=", "dev": true, "requires": { - "caniuse-db": "1.0.30000752", + "caniuse-db": "1.0.30000755", "electron-to-chromium": "1.3.27" } }, @@ -13778,7 +13778,9 @@ "dev": true }, "ree-validate": { - "version": "git+https://github.com/moeen-basra/ree-validate.git#e8e2f754016421427e36fb21ba8ad8a4838bae58", + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/ree-validate/-/ree-validate-2.0.1.tgz", + "integrity": "sha512-WyW/EEPBAf/phOr91DTuA3qth4W3bruhKQhsWpZ5jZIk9Sy3tlDozUcmaWj0BAW4ASuf95UJ64XKUls2Q8WKyA==", "dev": true }, "regenerate": { diff --git a/package.json b/package.json index 1d8561bc..7f18b09c 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "redux-devtools-log-monitor": "^1.3.0", "redux-logger": "^3.0.6", "redux-thunk": "^2.2.0", - "ree-validate": "git+https://github.com/moeen-basra/ree-validate.git" + "ree-validate": "^2.0.1" }, "dependencies": {} } diff --git a/readme.md b/readme.md index 67bbde33..93cbf360 100644 --- a/readme.md +++ b/readme.md @@ -73,6 +73,6 @@ TODO: - [x] Articles Crud - [x] Form validation Client and Server - [ ] Reset Password -- [ ] Tests +- [x] Tests - [x] Upgrade to Laravel 5.5 - [x] Upgrade to React 16 \ No newline at end of file diff --git a/tests/Feature/ArticleTest.php b/tests/Feature/ArticleTest.php index 72eb4f12..9bcc7935 100644 --- a/tests/Feature/ArticleTest.php +++ b/tests/Feature/ArticleTest.php @@ -4,6 +4,7 @@ use App\Article; use App\User; +use Carbon\Carbon; use Symfony\Component\HttpKernel\Tests\Exception\NotFoundHttpExceptionTest; use Tests\TestCase; use Illuminate\Foundation\Testing\WithoutMiddleware; @@ -35,7 +36,7 @@ public function that_only_loading_articles_for_provided_user_id() } /** @test */ - public function that_load_all_loads_all_articles() + public function that_load_all_articles() { $this->seedUnpublishedArticles(); @@ -69,17 +70,59 @@ public function that_load_only_published_article() $this->assertInstanceOf(Article::class, $article); } - private function seedUnpublishedArticles() + /** @test */ + public function that_article_get_published_and_total_number_of_published_get_changed() + { + $this->seedPublishedArticles(2); + $this->seedUnpublishedArticles(5); + + $date = Carbon::now()->format('Y-m-d'); + + $article = Article::where('published', false)->first(); + $article->published = true; + $article->published_at = $date; + + $article->save(); + + $this->assertEquals($article->published, true); + $this->assertEquals($article->published_at->format('Y-m-d'), $date); + + $articles = Article::where('published', true)->get(); + + $this->assertEquals($articles->count(), 3); + } + + /** @test */ + public function that_article_get_unpublished_and_total_number_of_unpublished_get_changed() + { + $this->seedPublishedArticles(2); + $this->seedUnpublishedArticles(5); + + $article = Article::where('published', true)->first(); + $article->published = false; + $article->published_at = null; + + $article->save(); + + $this->assertEquals($article->published, false); + $this->assertEquals($article->published_at, null); + + $articles = Article::where('published', false)->get(); + + $this->assertEquals($articles->count(), 6); + } + + private function seedUnpublishedArticles($num = 15) { - factory(Article::class, 15)->create([ + factory(Article::class, $num)->create([ 'user_id' => $this->user->id, 'published' => false, ]); } - private function seedPublishedArticles() + private function seedPublishedArticles($num = 5) { - factory(Article::class, 5)->create([ + factory(Article::class, $num)->create([ 'user_id' => $this->user->id, 'published' => true, ]);