Skip to content

Commit 7000b86

Browse files
authored
Merge pull request #201 from indranshastri/develop
Created Page Test of CRUD
2 parents a8dfa71 + 4e0ca3d commit 7000b86

File tree

7 files changed

+197
-8
lines changed

7 files changed

+197
-8
lines changed

app/Http/Controllers/Api/V1/PagesController.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,15 @@ public function show(Page $page)
5757
*/
5858
public function store(Request $request)
5959
{
60+
6061
$validation = $this->validatePages($request);
6162
if ($validation->fails()) {
6263
return $this->throwValidation($validation->messages()->first());
6364
}
6465

65-
$this->repository->create($request->all());
66-
67-
return new PagesResource(Page::orderBy('created_at', 'desc')->first());
66+
$page = $this->repository->create($request->all());
67+
68+
return new PagesResource($page);
6869
}
6970

7071
/**

app/Http/Controllers/Api/V1/UsersController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(UserRepository $repository)
3030
*
3131
* @return \Illuminate\Http\JsonResponse
3232
*/
33-
public function index(ManageUserRequest $request)
33+
public function index(Request $request)
3434
{
3535
$limit = $request->get('paginate') ? $request->get('paginate') : 25;
3636

app/Http/Middleware/VerifyCsrfToken.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ class VerifyCsrfToken extends BaseVerifier
1212
* @var array
1313
*/
1414
protected $except = [
15-
//
15+
'api/*'
1616
];
1717
}

app/Repositories/Backend/Pages/PagesRepository.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,14 @@ public function create(array $input)
5757

5858
if ($page = Page::create($input)) {
5959
event(new PageCreated($page));
60-
61-
return true;
60+
61+
return $page;
6262
}
6363

6464
throw new GeneralException(trans('exceptions.backend.pages.create_error'));
65+
66+
67+
6568
}
6669

6770
/**

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"laravel/socialite": "^3.0",
2424
"laravel/tinker": "~1.0",
2525
"laravelcollective/html": "^5.4.0",
26-
"spatie/laravel-cors": "^1.1",
26+
"spatie/laravel-cors": "^1.2",
2727
"tymon/jwt-auth": "dev-develop",
2828
"unisharp/laravel-filemanager": "~1.8",
2929
"yajra/laravel-datatables-oracle": "~8.0"

database/factories/PageFactory.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77
$factory->define(Page::class, function (Faker $faker) {
88
$title = $faker->sentence;
99

10+
$newestPage = Page::orderBy('id', 'desc')->first();
11+
1012
return [
1113
'title' => $title,
1214
'page_slug' => str_slug($title),
1315
'description' => $faker->paragraph,
16+
'cannonical_link' => "http://localhost:8000/".str_slug($title),
1417
'created_by' => function () {
1518
return factory(User::class)->create()->id;
1619
},
20+
'status' => 1,
21+
'created_at' => Carbon\Carbon::now(),
22+
'updated_at' => Carbon\Carbon::now(),
1723
];
1824
});

tests/Feature/Api/V1/PageTest.php

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?php
2+
3+
namespace Tests\Feature\Api\V1;
4+
5+
use Tests\TestCase;
6+
use Illuminate\Foundation\Testing\WithFaker;
7+
use Illuminate\Foundation\Testing\RefreshDatabase;
8+
use Tests\BrowserKitTestCase;
9+
use App\Models\Access\User\User;
10+
11+
use App\Models\Page\Page;
12+
13+
use JWTAuth;
14+
15+
class PageTest extends TestCase
16+
{
17+
public $token='';
18+
public $headers='';
19+
public $user='';
20+
public function setUp()
21+
{
22+
parent::setUp();
23+
$this->user = User::find(1);
24+
25+
$this->token = JWTAuth::fromUser($this->user);
26+
$this->headers = ['Authorization' => "Bearer ".$this->token];
27+
}
28+
/**
29+
* A basic test example.
30+
*
31+
* @return void
32+
*/
33+
public function testExample()
34+
{
35+
$this->assertTrue(true);
36+
}
37+
38+
/**
39+
* A basic test to get response form pages api
40+
*
41+
* @return void
42+
*/
43+
/** @test */
44+
public function Get_records_from_pages()
45+
{
46+
47+
$payload = [];
48+
$response = $this->json('GET', '/api/v1/pages',$payload, $this->headers);
49+
$response
50+
->assertStatus(200)
51+
->assertJsonStructure([
52+
'data'=>[
53+
[
54+
"id",
55+
"title",
56+
"status_label",
57+
"status",
58+
"created_at",
59+
"created_by"
60+
]
61+
],
62+
'links',
63+
'meta'
64+
]);
65+
66+
}
67+
68+
/**
69+
* A basic test to get response form pages api
70+
*
71+
* @return void
72+
*/
73+
/** @test */
74+
public function get_one_created_page_from_db()
75+
{
76+
$page = create(Page::class);
77+
$payload = [];
78+
$response = $this->json('GET', '/api/v1/pages/'.$page->id, $payload, $this->headers);
79+
$response
80+
->assertStatus(200)
81+
->assertJson([
82+
"data"=>[
83+
"id" => $page->id,
84+
"title" => $page->title,
85+
"status_label" => $page->status_label,
86+
"status" => ($page->isActive()) ? 'Active' :'InActive',
87+
"created_by" => $page->created_by,
88+
],
89+
]);
90+
91+
}
92+
/**
93+
* Author: Indra Shastri
94+
* Date:03-03-2018
95+
* A basic test to update a page from api
96+
*
97+
*
98+
* @return void
99+
*/
100+
/** @test */
101+
public function update_a_page_in_db_and_get_response()
102+
{
103+
$page = make(Page::class);
104+
$payload = [
105+
"title" => $page->title,
106+
"description" => $page->description,
107+
"cannonical_link" => $page->cannonical_link,
108+
"seo_title" => "some tittle",
109+
"seo_keyword" => "some keywords",
110+
"seo_description" => "<p>&nbsp;</p>↵<h1>SEO Description</h1>↵<p>some seco desctription</p>↵<p>askdsaj;ldsjfd</p>",
111+
"status" => "1",
112+
];
113+
$response = "";
114+
$response = $this->json('PUT', '/api/v1/pages/1', $payload, $this->headers);
115+
116+
$response->assertStatus(200);
117+
$response->assertJson([
118+
"data"=>[
119+
"title" => $page->title,
120+
"status_label" => $page->status_label,
121+
"status" => ($page->isActive()) ? 'Active' :'InActive',
122+
"created_by" => "".$this->user->id,
123+
],
124+
]);
125+
126+
}
127+
/**
128+
* Author: Indra Shastri
129+
* Date:03-03-2018
130+
* A basic test to create a page from api
131+
*
132+
* @return void
133+
*/
134+
/** @test */
135+
public function create_a_new_page_in_db_and_get_response()
136+
{
137+
138+
$page = make(Page::class);
139+
$payload = [
140+
"title" => $page->title,
141+
"description" => $page->description,
142+
"cannonical_link" => $page->cannonical_link,
143+
"seo_title" => "some tittle",
144+
"seo_keyword" => "some keywords",
145+
"seo_description" => "<p>&nbsp;</p>↵<h1>SEO Description</h1>↵<p>some seco desctription</p>↵<p>askdsaj;ldsjfd</p>",
146+
"status" => "1",
147+
];
148+
$response = "";
149+
$response = $this->json('POST', '/api/v1/pages', $payload, $this->headers);
150+
$response->assertStatus(201);
151+
$response->assertJson([
152+
"data" => [
153+
"title" => $page->title,
154+
"status_label" => $page->status_label,
155+
"status" => ($page->isActive()) ? 'Active' : 'InActive',
156+
"created_by" => $this->user->first_name,
157+
"created_at" => (\Carbon\Carbon::now())->toDateString()
158+
],
159+
]);
160+
161+
}
162+
/**
163+
* Author: Indra Shastri
164+
* Date:03-03-2018
165+
* A basic test to create a page from api
166+
*
167+
* @return void
168+
*/
169+
/** @test */
170+
public function delete_page_in_db_and_get_response(){
171+
$page = create(Page::class);
172+
$payload=[];
173+
$response = $this->json('DELETE', '/api/v1/pages/'.$page->id, $payload, $this->headers);
174+
$response->assertStatus(200)
175+
->assertJson([
176+
"message"=> "The Page was successfully deleted."
177+
]);
178+
}
179+
}

0 commit comments

Comments
 (0)