Skip to content

Commit 82799e6

Browse files
author
Tortue Torche
committed
Refactoring Controller::render() method to always return a \Illuminate\Http\Response object.
Add tests for this method.
1 parent dc537e4 commit 82799e6

File tree

11 files changed

+314
-8
lines changed

11 files changed

+314
-8
lines changed

src/Efficiently/JqueryLaravel/ControllerAdditions.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ trait ControllerAdditions
1212
* @param array $data Data that should be made available to the view. Only used when $view is a string path
1313
* @param string|boolean $layout Master layout path
1414
*
15-
* @return \Illuminate\View\View|\Illuminate\Http\Response
15+
* @return \Illuminate\Http\Response
1616
*/
1717
public function render($view, $data = [], $layout = null)
1818
{
@@ -51,10 +51,10 @@ public function render($view, $data = [], $layout = null)
5151
if ($this->layout) {
5252
$this->layout->content = $view;
5353

54-
return $this->layout;
55-
} else {
56-
return $view;
54+
return response($this->layout);
5755
}
56+
57+
return response($view);
5858
}
5959

6060
/**
@@ -64,7 +64,7 @@ public function render($view, $data = [], $layout = null)
6464
*/
6565
protected function setupLayout()
6666
{
67-
if (! is_null($this->layout)) {
67+
if ($this->layout) {
6868
$this->layout = view($this->layout);
6969
}
7070
}

src/Efficiently/JqueryLaravel/EloquentHtmlHelper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class EloquentHtmlHelper
55

66
/**
77
* The DOM id convention is to use the singular form of an object or class with the id following an underscore.
8-
* If no id is found, prefix with create_ instead.
8+
* If no id is found, prefix with "create_" instead.
99
*
1010
* @param object|\Illuminate\Database\Eloquent\Model $record
1111
* @param string $prefix
@@ -24,8 +24,8 @@ public function domId($record, $prefix = null, $fallbackPrefix = 'create')
2424

2525
/**
2626
* The Form id convention is to use the singular form of an object or class with the id following an underscore.
27-
* If id is found, prefix with edit_.
28-
* If no id is found, prefix with create_ instead.
27+
* If id is found, prefix with "edit_".
28+
* If no id is found, prefix with "create_" instead.
2929
*
3030
* @param object|\Illuminate\Database\Eloquent\Model $record
3131
* @param string $fallbackPrefix By default it's 'create'
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
use Mockery as m;
4+
use Jql\Project;
5+
use Jql\Http\Controllers\ProjectsController;
6+
7+
class JqlControllerAdditionsTest extends JqlTestCase
8+
{
9+
public function setUp()
10+
{
11+
parent::setUp();
12+
Route::resource('projects', 'ProjectsController');
13+
View::addLocation(__DIR__.'/fixtures/resources/views');
14+
15+
$this->controller = new ProjectsController;
16+
}
17+
18+
public function testRenderMethodWithDefaultLayout()
19+
{
20+
// New Project
21+
$project = new Project;
22+
$project->id = 2;
23+
$project->exists = true;
24+
$project->name = 'Project 2';
25+
$project->priority = 'low';
26+
27+
$mock = $this->mockProject($project);
28+
$response = $this->controller->callAction('index', [$mock]);
29+
30+
$expectDefaultLayout = '<title>jQuery-Laravel with default layout</title>';
31+
$expectProjectName = 'Project 2';
32+
33+
$this->assertContains($expectDefaultLayout, $response->getContent());
34+
$this->assertNotContains($expectDefaultLayout.' foo', $response->getContent());
35+
$this->assertContains($expectProjectName, $response->getContent());
36+
$this->assertNotContains($expectProjectName.' foo', $response->getContent());
37+
}
38+
39+
public function testRenderMethodWithCustomLayout()
40+
{
41+
// New Project
42+
$project = new Project;
43+
$project->id = 3;
44+
$project->exists = true;
45+
$project->name = 'Project 3';
46+
$project->priority = 'high';
47+
48+
$mock = $this->mockProject($project);
49+
$response = $this->controller->callAction('index', [$mock, 'app2']);
50+
51+
$expectDefaultLayout = '<title>jQuery-Laravel with custom layout</title>';
52+
$expectProjectName = 'Project 3';
53+
54+
$this->assertContains($expectDefaultLayout, $response->getContent());
55+
$this->assertNotContains($expectDefaultLayout.' foo', $response->getContent());
56+
$this->assertContains($expectProjectName, $response->getContent());
57+
$this->assertNotContains($expectProjectName.' foo', $response->getContent());
58+
}
59+
60+
public function testRenderMethodWithoutLayout()
61+
{
62+
// New Project
63+
$project = new Project;
64+
$project->id = 4;
65+
$project->exists = true;
66+
$project->name = 'Project 4';
67+
$project->priority = 'low';
68+
69+
$mock = $this->mockProject($project);
70+
$response = $this->controller->callAction('index', [$mock, false]);
71+
72+
$expectDefaultLayout = '<title>jQuery-Laravel without layout</title>';
73+
$expectProjectName = 'Project 4';
74+
75+
$this->assertContains($expectDefaultLayout, $response->getContent());
76+
$this->assertContains($expectProjectName, $response->getContent());
77+
$this->assertNotContains($expectProjectName.' foo', $response->getContent());
78+
}
79+
80+
public function testRenderMethodWithJSRequest()
81+
{
82+
// New Project
83+
$project = new Project;
84+
$project->id = 5;
85+
$project->exists = true;
86+
$project->name = 'Project 5';
87+
$project->priority = 'low';
88+
89+
$mock = $this->mockProject($project);
90+
$response = $this->controller->callAction('index', [$mock, null, 'js']);
91+
92+
$expectDefaultLayout = '<title>jQuery-Laravel tests</title>';
93+
$expectProjectJson = json_encode($project);
94+
95+
$this->assertNotContains($expectDefaultLayout, $response->getContent());
96+
$this->assertContains($expectProjectJson, $response->getContent());
97+
$this->assertNotContains($expectProjectJson.' foo', $response->getContent());
98+
}
99+
100+
protected function mockProject($project)
101+
{
102+
$mock = m::mock(Project::class);
103+
$mock->shouldReceive('all')->andReturn([$project]);
104+
105+
return $mock;
106+
}
107+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php namespace Jql\Http\Controllers;
2+
3+
use Illuminate\Foundation\Bus\DispatchesJobs;
4+
use Illuminate\Routing\Controller as BaseController;
5+
use Illuminate\Foundation\Validation\ValidatesRequests;
6+
7+
abstract class Controller extends BaseController
8+
{
9+
use DispatchesJobs, ValidatesRequests;
10+
use \Efficiently\JqueryLaravel\ControllerAdditions;
11+
12+
/**
13+
* The layout that should be used for responses.
14+
*/
15+
protected $layout = 'app';
16+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php namespace Jql\Http\Controllers;
2+
3+
use Jql\Project;
4+
use Response;
5+
6+
class ProjectsController extends Controller
7+
{
8+
9+
/**
10+
* Display a listing of the resource.
11+
* @param \Jql\Project $projectModel
12+
* @param string|null|boolean $layout Default to null.
13+
* @param string $format Default to 'html'.
14+
* @return Response
15+
*/
16+
public function index(Project $projectModel, $layout = null, $format = 'html')
17+
{
18+
$projects = $projectModel->all();
19+
20+
switch ($format) {
21+
case 'js':
22+
$render = $this->render(['js' => 'projects.index'], compact('projects'));
23+
break;
24+
case 'html':
25+
default:
26+
// No js fallback
27+
if (is_null($layout)) {
28+
// default layout
29+
$render = $this->render('projects.index', compact('projects'));
30+
} elseif ($layout === false) {
31+
// no layout
32+
$render = $this->render('projects.index_standalone', compact('projects'), $layout);
33+
} else {
34+
// custom layout
35+
$render = $this->render('projects.index', compact('projects'), $layout);
36+
}
37+
break;
38+
}
39+
40+
return $render;
41+
}
42+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
5+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
{!! csrf_meta_tags() !!}
8+
9+
<title>jQuery-Laravel with default layout</title>
10+
11+
{{--!! stylesheet_link_tag('app') !!--}}
12+
13+
{{-- Size should be 32 x 32 pixels --}}
14+
{{-- favicon_link_tag('favicon.ico', ['rel' => 'shortcut icon']) --}}
15+
16+
{{--!! javascript_include_tag('app') !!--}}
17+
</head>
18+
<body>
19+
<nav id='navbar_top' class="navbar navbar-default" data-turbolinks-permanent>
20+
<div class="container-fluid">
21+
<div class="navbar-header">
22+
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
23+
<span class="sr-only">Toggle Navigation</span>
24+
<span class="icon-bar"></span>
25+
<span class="icon-bar"></span>
26+
<span class="icon-bar"></span>
27+
</button>
28+
<a class="navbar-brand" href="{{ url('/') }}">jQuery-Laravel demo</a>
29+
</div>
30+
31+
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
32+
<ul class="nav navbar-nav">
33+
<li><a href="{{ route('projects.index') }}" data-remote>Projects</a></li>
34+
</ul>
35+
</div>
36+
</div>
37+
</nav>
38+
39+
<div class="container">
40+
<section id="content">
41+
@yield('content')
42+
</section>{{-- /content --}}
43+
</div>{{-- /container --}}
44+
</body>
45+
</html>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
5+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
{!! csrf_meta_tags() !!}
8+
9+
<title>jQuery-Laravel with custom layout</title>
10+
11+
{{--!! stylesheet_link_tag('app') !!--}}
12+
13+
{{-- Size should be 32 x 32 pixels --}}
14+
{{-- favicon_link_tag('favicon.ico', ['rel' => 'shortcut icon']) --}}
15+
16+
{{--!! javascript_include_tag('app') !!--}}
17+
</head>
18+
<body>
19+
<nav id='navbar_top' class="navbar navbar-default" data-turbolinks-permanent>
20+
<div class="container-fluid">
21+
<div class="navbar-header">
22+
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
23+
<span class="sr-only">Toggle Navigation</span>
24+
<span class="icon-bar"></span>
25+
<span class="icon-bar"></span>
26+
<span class="icon-bar"></span>
27+
</button>
28+
<a class="navbar-brand" href="{{ url('/') }}">jQuery-Laravel demo</a>
29+
</div>
30+
31+
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
32+
<ul class="nav navbar-nav">
33+
<li><a href="{{ route('projects.index') }}" data-remote>Projects</a></li>
34+
</ul>
35+
</div>
36+
</div>
37+
</nav>
38+
39+
<div class="container">
40+
<section id="content">
41+
@yield('content')
42+
</section>{{-- /content --}}
43+
</div>{{-- /container --}}
44+
</body>
45+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@div_for($project, null, ['class' => 'row'])
2+
<div class="col-lg-10 col-md-8 col-sm-6">
3+
<dl class="dl-horizontal">
4+
<dt>Name</dt>
5+
<dd>
6+
{{ $project->name }}
7+
</dd>
8+
9+
<dt>Priority</dt>
10+
<dd>
11+
{{ $project->priority }}
12+
</dd>
13+
</dl>
14+
</div>
15+
@end_div_for
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@section('content')
2+
<h1 class="page-header">All projects:</h1>
3+
<div id="projects">
4+
{{-- renders views/projects/_project.blade.php for each project --}}
5+
@foreach ($projects as $project)
6+
@include('projects._project')
7+
@endforeach
8+
</div>
9+
@stop
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@foreach ($projects as $project)
2+
var project{{ $project->id }} = {!! json_encode($project) !!};
3+
@endforeach

0 commit comments

Comments
 (0)