Skip to content

Commit

Permalink
fix the missing helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
moeen-basra committed Dec 20, 2019
1 parent 47bad53 commit 4601fa6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 42 deletions.
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":1256:{a:2:{s:7:"defects";a:7:{s:74:"Tests\Feature\ArticleTest::that_only_loading_articles_for_provided_user_id";i:4;s:49:"Tests\Feature\ArticleTest::that_load_all_articles";i:4;s:62:"Tests\Feature\ArticleTest::that_loaded_only_published_articles";i:4;s:59:"Tests\Feature\ArticleTest::that_load_only_published_article";i:4;s:95:"Tests\Feature\ArticleTest::that_article_get_published_and_total_number_of_published_get_changed";i:4;s:99:"Tests\Feature\ArticleTest::that_article_get_unpublished_and_total_number_of_unpublished_get_changed";i:4;s:44:"Tests\Feature\LoginTest::test_user_can_login";i:3;}s:5:"times";a:8:{s:74:"Tests\Feature\ArticleTest::that_only_loading_articles_for_provided_user_id";d:1.668;s:49:"Tests\Feature\ArticleTest::that_load_all_articles";d:0.541;s:62:"Tests\Feature\ArticleTest::that_loaded_only_published_articles";d:0.376;s:59:"Tests\Feature\ArticleTest::that_load_only_published_article";d:0.556;s:95:"Tests\Feature\ArticleTest::that_article_get_published_and_total_number_of_published_get_changed";d:0.438;s:99:"Tests\Feature\ArticleTest::that_article_get_unpublished_and_total_number_of_unpublished_get_changed";d:0.419;s:44:"Tests\Feature\LoginTest::test_user_can_login";d:1.037;s:37:"Tests\Unit\ExampleTest::testBasicTest";d:0.21;}}}
20 changes: 11 additions & 9 deletions app/Http/Controllers/Api/ArticleController.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

namespace App\Http\Controllers\Api;

use App\Article;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Http\Controllers\Controller;
use App\Http\Requests\ArticleRequest;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\Request;

class ArticleController extends Controller
{
Expand Down Expand Up @@ -57,15 +59,15 @@ public function create()
/**
* Store a newly created resource in storage.
*
* @param ArticleRequest $request
* @param ArticleRequest $request
* @return \Illuminate\Http\Response
*/
public function store(ArticleRequest $request)
{
$user = $request->user();

$article = new Article($request->validated());
$article->slug = str_slug($request->get('title'));
$article->slug = Str::slug($request->get('title'));

$user->articles()->save($article);

Expand All @@ -76,7 +78,7 @@ public function store(ArticleRequest $request)
* Display the specified resource.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show(Request $request, $id)
Expand All @@ -91,7 +93,7 @@ public function show(Request $request, $id)
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
Expand All @@ -102,16 +104,16 @@ public function edit($id)
/**
* Update the specified resource in storage.
*
* @param ArticleRequest $request
* @param int $id
* @param ArticleRequest $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(ArticleRequest $request, $id)
{
$article = Article::findOrFail($id);

$data = $request->validated();
$data['slug'] = str_slug($data['title']);
$data['slug'] = Str::slug($data['title']);
$article->update($data);

return response()->json($article, 200);
Expand All @@ -120,7 +122,7 @@ public function update(ArticleRequest $request, $id)
/**
* Remove the specified resource from storage.
*
* @param int $id
* @param int $id
* @return \Illuminate\Http\Response
*/
public function delete($id)
Expand Down
6 changes: 4 additions & 2 deletions database/factories/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
|
*/

use Illuminate\Support\Str;

/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(App\User::class, function (Faker\Generator $faker) {
static $password;
Expand All @@ -21,7 +23,7 @@
'phone' => $faker->phoneNumber,
'about' => $faker->sentence(10),
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
'remember_token' => Str::random(10),
];
});

Expand All @@ -32,7 +34,7 @@

return [
'title' => $title,
'slug' => str_slug($title),
'slug' => Str::slug($title),
'description' => $faker->sentence(15),
'content' => implode(' ', $faker->paragraphs(2)),
'published' => true,
Expand Down
60 changes: 29 additions & 31 deletions tests/Feature/ArticleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

namespace Tests\Feature;

use App\Article;
use App\User;
use App\Article;
use Carbon\Carbon;
use Symfony\Component\HttpKernel\Tests\Exception\NotFoundHttpExceptionTest;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Str;

class ArticleTest extends TestCase
{
Expand All @@ -24,6 +22,17 @@ public function setUp(): void
$this->user = $this->createAdminUser();
}

private function createAdminUser()
{
return User::create([
'name' => 'Moeen Basra',
'email' => '[email protected]',
'password' => bcrypt('secret'),
'is_admin' => true,
'remember_token' => Str::random(10),
]);
}

/** @test */
public function that_only_loading_articles_for_provided_user_id()
{
Expand All @@ -35,6 +44,14 @@ public function that_only_loading_articles_for_provided_user_id()

}

private function seedUnpublishedArticles($num = 15)
{
factory(Article::class, $num)->create([
'user_id' => $this->user->id,
'published' => false,
]);
}

/** @test */
public function that_load_all_articles()
{
Expand All @@ -55,6 +72,14 @@ public function that_loaded_only_published_articles()
$this->assertCount(5, $articles);
}

private function seedPublishedArticles($num = 5)
{
factory(Article::class, $num)->create([
'user_id' => $this->user->id,
'published' => true,
]);
}

/** @test */
public function that_load_only_published_article()
{
Expand Down Expand Up @@ -109,31 +134,4 @@ public function that_article_get_unpublished_and_total_number_of_unpublished_get

$this->assertEquals($articles->count(), 6);
}

private function seedUnpublishedArticles($num = 15)
{
factory(Article::class, $num)->create([
'user_id' => $this->user->id,
'published' => false,
]);
}

private function seedPublishedArticles($num = 5)
{
factory(Article::class, $num)->create([
'user_id' => $this->user->id,
'published' => true,
]);
}

private function createAdminUser()
{
return User::create([
'name' => 'Moeen Basra',
'email' => '[email protected]',
'password' => bcrypt('secret'),
'is_admin' => true,
'remember_token' => str_random(10),
]);
}
}

0 comments on commit 4601fa6

Please sign in to comment.