Skip to content

Commit

Permalink
More tests. Corrected controller methods. Travis
Browse files Browse the repository at this point in the history
  • Loading branch information
elcobvg committed Jan 9, 2018
1 parent 1796adf commit 39d0d3a
Show file tree
Hide file tree
Showing 17 changed files with 299 additions and 129 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: php

php:
- 7.0

services:
- mongodb

before_script:
- cp .env.travis .env
- composer self-update
- composer install --no-interaction

script:
- vendor/bin/phpunit
8 changes: 5 additions & 3 deletions app/Http/Controllers/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function store(Request $request)
$article->tags()->attach(new Tag(['name' => $name]));
}
}
return $this->respondCreated(new ArticleResource($article));
return (new ArticleResource($article))->response()->header('Status', 201);
}

/**
Expand Down Expand Up @@ -121,9 +121,11 @@ public function destroy(Request $request, string $slug)
abort(404);
}

if ($request->user()->can('delete-article', $article)) {
$article->delete();
if ($request->user()->cannot('delete-article', $article)) {
abort(403);
}

$article->delete();
return $this->respondSuccess();
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function register(Request $request)
'password' => Hash::make($request->input('user.password')),
]);

return $this->respondCreated(new UserResource($user));
return (new UserResource($user))->response()->header('Status', 201);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function store(Request $request, string $slug)
]);
$comment = $article->comments->pop();

return $this->respondCreated(new CommentResource($comment));
return (new CommentResource($comment))->response()->header('Status', 201);
}

/**
Expand Down
11 changes: 0 additions & 11 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@ protected function respondSuccess()
return $this->respond(null, 204);
}

/**
* Respond with created.
*
* @param $data
* @return \Illuminate\Http\JsonResponse
*/
protected function respondCreated($data)
{
return $this->respond($data, 201);
}

/**
* Paginate and filter a collection of items
*
Expand Down
10 changes: 8 additions & 2 deletions app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ public function show($username)
*/
public function follow(string $username)
{
$user = $this->getUserByName($username);
if (! $user = $this->getUserByName($username)) {
abort(404, "User ${username} not found");
}

Auth::user()->follow($user);
return new ProfileResource($user);
}
Expand All @@ -55,7 +58,10 @@ public function follow(string $username)
*/
public function unFollow(string $username)
{
$user = $this->getUserByName($username);
if (! $user = $this->getUserByName($username)) {
abort(404, "User ${username} not found");
}

Auth::user()->unFollow($user);
return new ProfileResource($user);
}
Expand Down
90 changes: 45 additions & 45 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions database/factories/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@

$number = $faker->numberBetween(1, 99);
$gender = $faker->randomElement(['men', 'women']);
gc_collect_cycles();

return [
'username' => $faker->unique()->userName,
'username' => str_replace('.', '', $faker->unique()->userName),
'email' => $faker->unique()->email,
'password' => \Illuminate\Support\Facades\Hash::make('password'),
'bio' => $faker->realText(140),
'bio' => $faker->sentence(10),
'image' => "https://randomuser.me/api/portraits/{$gender}/{$number}.jpg"
];
});

$factory->define(App\Models\Article::class, function (Faker\Generator $faker) {
gc_collect_cycles();

return [
'title' => $faker->sentence,
'description' => $faker->paragraph,
Expand Down
1 change: 1 addition & 0 deletions database/seeds/ArticlesTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function run()
factory(App\Models\Article::class, 25)->create()->each(function ($article) {

$faker = Faker\Factory::create();
gc_collect_cycles();

$authors = App\Models\User::all();
$author = $authors[$faker->numberBetween(0, sizeof($authors) - 1)];
Expand Down
1 change: 1 addition & 0 deletions database/seeds/UsersTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function run()
{
factory(App\Models\User::class, 10)->create();
$this->command->info('Users created');
gc_collect_cycles();

$users = App\Models\User::all();
foreach ($users as $user) {
Expand Down
Loading

0 comments on commit 39d0d3a

Please sign in to comment.