Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4cb66e4

Browse files
committed
add api tests with example, acceptance and etc.
1 parent 307e2da commit 4cb66e4

File tree

11 files changed

+195
-13
lines changed

11 files changed

+195
-13
lines changed

‎app/Exceptions/Handler.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Illuminate\Auth\Access\AuthorizationException;
55
use Illuminate\Database\Eloquent\ModelNotFoundException;
66
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
7+
use Illuminate\Http\JsonResponse;
78
use Illuminate\Validation\ValidationException;
89
use Symfony\Component\HttpKernel\Exception\HttpException;
910

@@ -47,6 +48,8 @@ public function render($request, Exception $e)
4748
{
4849
if ($e instanceof HttpException) {
4950
return $this->renderHttpException($e);
51+
} elseif($e instanceof ModelNotFoundException && $request->segment(1) == "api") {
52+
return new JsonResponse(['error'=>'model not found'], 404);
5053
} else {
5154
return parent::render($request, $e);
5255
}

‎app/Http/Controllers/Api/PostsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function store(PostStoreRequest $request)
5454
{
5555
$post = $this->post->create($request->all());
5656

57-
return $this->responseFactory->json($post);
57+
return $this->responseFactory->json($post, 201);
5858
}
5959

6060

‎database/factories/ModelFactory.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@
1111
|
1212
*/
1313

14+
use Illuminate\Database\Eloquent\Model;
15+
1416
$factory->define(App\User::class, function (Faker\Generator $faker) {
1517
return [
16-
'email' => $faker->email,
17-
'password' => bcrypt(str_random(10)),
18+
'email' => $faker->email,
19+
'password' => bcrypt(str_random(10)),
1820
'remember_token' => str_random(10),
1921
];
2022
});
2123

2224
$factory->defineAs(App\User::class, 'admin', function (Faker\Generator $faker) {
2325
return [
24-
'email' => $faker->email,
25-
'password' => bcrypt(str_random(10)),
26+
'email' => $faker->email,
27+
'password' => bcrypt(str_random(10)),
2628
'remember_token' => str_random(10),
2729
];
28-
});
30+
});

‎tests/acceptance.suite.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
actor: AcceptanceTester
22
modules:
33
enabled:
4-
- \Helper\Acceptance
4+
- PhpBrowser:
5+
url: http://127.0.0.1:8000/
6+
- \Helper\Acceptance
7+
- Sequence

‎tests/acceptance/PostsGeneratedCept.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
//You can use https://chrome.google.com/webstore/detail/codeception-testtools/jhaegbojocomemkcnmnpmoobbmnkijik
3+
/** @var \Codeception\Scenario $scenario */
4+
$I = new AcceptanceTester($scenario);
5+
$I->amOnPage('/posts');
6+
$I->click('Add new post');
7+
$I->seeCurrentURLEquals('/posts/create');
8+
$I->fillField('title', 'test');
9+
$I->fillField('body', 'test new post');
10+
$I->click('Submit');
11+
$I->seeCurrentURLEquals('/posts');
12+
$I->see('test new post');
13+
$I->click('Edit');
14+
$I->see('test new post');
15+
$I->click('Update');
16+
$I->click('Edit');
17+
$I->fillField('body', 'test new post 2');
18+
$I->click('Update');
19+
$I->see('test new post 2');
20+
$I->click('Delete');
21+
$I->seeCurrentURLEquals('/posts');

‎tests/acceptance/RegisterCept.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/** @var \Codeception\Scenario $scenario */
3+
$I = new AcceptanceTester($scenario);
4+
$I->amOnPage('/register');
5+
$I->see('Name');
6+
$I->see('Confirm Password');
7+
$I->see('Register');
8+
$I->fillField('name', 'Ivelin');
9+
$I->fillField('email', 'ibpavlov'.sq().'@mailinator.com');
10+
$I->fillField('password', 'qwerty');
11+
$I->fillField('password_confirmation', 'qwerty');
12+
$I->click('Register', 'button');
13+
$I->see('Hello world!');
14+
$I->see('Logged in');
15+
$I->see('Logout');

‎tests/acceptance/testLoginCept.php

Lines changed: 0 additions & 3 deletions
This file was deleted.

‎tests/api.suite.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ modules:
66
- REST:
77
depends: Laravel5
88
- Laravel5:
9-
environment_file: .env.testing
9+
environment_file: .env.testing
10+
- Sequence

‎tests/api/PostsResourceCept.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
'title' => 'Game of Rings',
66
'body' => 'By George Tolkien'
77
]);
8-
$I->seeResponseCodeIs(200);
8+
$I->seeResponseCodeIs(201);
99
$I->seeResponseIsJson();
1010
$I->seeResponseContainsJson([
1111
'title' => 'Game of Rings'

‎tests/api/PostsResourceCest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function getSinglePost(ApiTester $I)
4242
public function createPost(ApiTester $I)
4343
{
4444
$I->sendPOST($this->endpoint, ['title' => 'Game of Rings', 'body' => 'By George Tolkien']);
45-
$I->seeResponseCodeIs(200);
45+
$I->seeResponseCodeIs(201);
4646
$I->seeResponseIsJson();
4747
$I->seeResponseContainsJson(['title' => 'Game of Rings']);
4848
$id = $I->grabDataFromResponseByJsonPath('$.id')[0];

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /