Skip to content

Commit a53c638

Browse files
author
Tortue Torche
committed
Add some tests for the function helpers.
Official Laravel 5.1 support. Replace the deprecated package 'illuminate/html' package by the 'laravelcollective/html' one.
1 parent ad2515e commit a53c638

File tree

7 files changed

+257
-13
lines changed

7 files changed

+257
-13
lines changed

.travis.yml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,24 @@ php:
44
- 5.4
55
- 5.5
66
- 5.6
7+
- 7.0
78
- hhvm
89

9-
before_script:
10-
- composer self-update
11-
- composer install --prefer-source --no-interaction --dev
10+
matrix:
11+
allow_failures:
12+
- php: 5.4 # Allow PHP 5.4 failures because Laravel 5.1 requires PHP >= 5.5
13+
- php: hhvm
14+
- php: 7.0
1215

13-
script: phpunit
16+
sudo: false
17+
18+
env:
19+
- LARAVEL_VERSION="~5.0.0" TESTBENCH_VERSION="~3.0.0"
20+
- LARAVEL_VERSION="~5.1.0" TESTBENCH_VERSION="~3.1.0"
21+
22+
before_install:
23+
- sed -i s/~5.0.0\|\|~5.1.0/${LARAVEL_VERSION}/ composer.json
24+
- sed -i s/~3.0.0\|\|~3.1.0/${TESTBENCH_VERSION}/ composer.json
25+
- composer update # Use update since we'll be changing the composer.json
26+
27+
script: vendor/bin/phpunit tests

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# jquery-laravel
1+
# jquery-laravel [![Build Status](https://travis-ci.org/efficiently/jquery-laravel.png?branch=2.0)](http://travis-ci.org/efficiently/jquery-laravel)
22

33
jQuery! For Laravel 5! So great.
44

composer.json

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,14 @@
2323
],
2424
"require": {
2525
"php": ">=5.4.0",
26-
"illuminate/support": "~5.0",
27-
"illuminate/html": "~5.0"
26+
"illuminate/support": "~5.0.0||~5.1.0 ",
27+
"laravelcollective/html": "~5.0.0||~5.1.0"
28+
},
29+
"require-dev": {
30+
"phpunit/phpunit": "~4.5",
31+
"mockery/mockery": "0.9.*",
32+
"orchestra/testbench": "~3.0.0||~3.1.0",
33+
"anahkiasen/former": "~4.0.0"
2834
},
2935
"autoload": {
3036
"files": [
@@ -34,6 +40,14 @@
3440
"Efficiently\\JqueryLaravel\\": "src/"
3541
}
3642
},
43+
"autoload-dev": {
44+
"classmap": [
45+
"tests/JqlTestCase.php"
46+
],
47+
"psr-4": {
48+
"Jql\\": "tests/fixtures/jql/"
49+
}
50+
},
3751
"config": {
3852
"preferred-install": "dist"
3953
},

src/Efficiently/JqueryLaravel/helpers.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,13 @@ function dom_class($recordOrClass, $prefix = null)
233233
* 'method' - HTTP verb. Supported verbs are 'post', 'get', 'delete', 'patch', and 'put'. By default it will be 'post'.
234234
* 'data-remote' - If set to true, will allow the Unobtrusive JavaScript drivers to control the submit behavior. By default this behavior is an ajax submit.
235235
* 'data-confirm' - This will use the unobtrusive JavaScript driver to prompt with the question specified. If the user accepts, the link is processed normally, otherwise no action is taken.
236+
* 'data-disable-with' - Value of this parameter will be used as the value for a disabled version of the submit button when the form is submitted. This feature is provided by the unobtrusive JavaScript driver.
236237
* 'form' - This array will be form attributes
237238
* 'formClass' - This controls the class of the form within which the submit button will be placed. By default it will be 'button_to'.
238239
* @return string
239240
*/
240241
function button_to($name, array $options = [])
241242
{
242-
243243
$formOptions = [
244244
'method' => array_pull($options, 'method', 'post'),
245245
'class' => array_pull($options, 'formClass', 'button_to')
@@ -256,14 +256,11 @@ function button_to($name, array $options = [])
256256
if (array_get($options, 'data-remote')) {
257257
$formOptions['data-remote'] = array_pull($options, 'data-remote');
258258
}
259-
if (array_get($options, 'data-confirm')) {
260-
$formOptions['data-confirm'] = array_pull($options, 'data-confirm');
261-
}
259+
262260
$formOptions = array_merge($formOptions, array_pull($options, 'form', []));
263261

264262
$submitButton = app('form')->submit($name, $options);
265-
if (
266-
class_exists('Button') &&
263+
if (class_exists('Button') &&
267264
is_a(new Button, '\Illuminate\Support\Facades\Facade') &&
268265
method_exists(Button::getFacadeRoot(), 'withValue')
269266
) {

tests/JqlHelpersTest.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
use Mockery as m;
4+
5+
class JqlHelpersTest extends JqlTestCase
6+
{
7+
public function testdomId()
8+
{
9+
// New Project
10+
$project = new Jql\Project;
11+
12+
$domId = dom_id($project);
13+
$expect = 'create_project';
14+
$this->assertEquals($expect, $domId);
15+
16+
$expect2 = 'project_2';
17+
$this->assertNotEquals($expect2, $domId);
18+
19+
// Existing Project
20+
$project2 = clone $project;
21+
$project2->id = 2;
22+
$project2->exists = true;
23+
24+
$domId2 = dom_id($project2);
25+
$this->assertEquals($expect2, $domId2);
26+
}
27+
28+
public function testFormId()
29+
{
30+
// New Project
31+
$project = new Jql\Project;
32+
33+
$formId = form_id($project);
34+
$expect = 'create_project';
35+
$this->assertEquals($expect, $formId);
36+
37+
$expect2 = 'edit_project_3';
38+
$this->assertNotEquals($expect2, $formId);
39+
40+
// Existing Project
41+
$project2 = clone $project;
42+
$project2->id = 3;
43+
$project2->exists = true;
44+
45+
$formId2 = form_id($project2);
46+
$this->assertEquals($expect2, $formId2);
47+
}
48+
49+
public function testFormFor()
50+
{
51+
Route::resource('projects', 'ProjectsController');
52+
53+
// New Project
54+
$project = new Jql\Project;
55+
56+
$form = form_for($project);
57+
$expect = '<form method="POST" action="http://localhost/projects" accept-charset="UTF-8" id="create_project" class="create_project">'.
58+
'<input name="_token" type="hidden">';
59+
$this->assertEquals($expect, $form);
60+
61+
$expect2 = '<form method="POST" action="http://localhost/projects/3" accept-charset="UTF-8" id="edit_project_3" class="edit_project">'.
62+
'<input name="_method" type="hidden" value="PATCH"><input name="_token" type="hidden">';
63+
$this->assertNotEquals($expect2, $form);
64+
65+
// Existing Project
66+
$project2 = clone $project;
67+
$project2->id = 3;
68+
$project2->exists = true;
69+
70+
$form2 = form_for($project2);
71+
$this->assertEquals($expect2, $form2);
72+
}
73+
74+
public function testFormerFor()
75+
{
76+
Route::resource('projects', 'ProjectsController');
77+
$this->createSession();
78+
79+
// New Project
80+
$project = new Jql\Project;
81+
82+
$former = former_for($project);
83+
$expect = '<form accept-charset="utf-8" class="form-horizontal create_project" id="create_project" method="POST" action="http://localhost/projects">';
84+
$this->assertEquals($expect, $former);
85+
86+
$expect2 = '<form accept-charset="utf-8" class="form-horizontal edit_project" id="edit_project_3" method="POST" action="http://localhost/projects/3">'.
87+
'<input class="form-control" type="hidden" name="_method" value="PATCH">';
88+
$this->assertNotEquals($expect2, $former);
89+
90+
// Existing Project
91+
$project2 = clone $project;
92+
$project2->id = 3;
93+
$project2->exists = true;
94+
95+
$former2 = (string) former_for($project2);
96+
$this->assertEquals($expect2, $former2);
97+
}
98+
99+
public function testButtonTo()
100+
{
101+
Route::resource('projects', 'ProjectsController');
102+
103+
// Create button
104+
$form = button_to('Create', ['action' => 'ProjectsController@create']);
105+
$expect = '<form method="POST" action="http://localhost/projects/create" accept-charset="UTF-8" class="button_to">'.
106+
'<input name="_token" type="hidden">'.
107+
'<div><input type="submit" value="Create"></div></form>';
108+
$this->assertEquals($expect, $form);
109+
110+
$expect2 = '<form method="POST" action="http://www.example.com" accept-charset="UTF-8" class="button_to" data-remote="true"><input name="_method" type="hidden" value="DELETE">'.
111+
'<input name="_token" type="hidden">'.
112+
'<div><input data-confirm="Are you sure?" data-disable-with="loading..." type="submit" value="Destroy"></div></form>';
113+
$this->assertNotEquals($expect2, $form);
114+
115+
// Destroy Button
116+
$form2 = button_to(
117+
'Destroy',
118+
[
119+
'url' => 'http://www.example.com',
120+
'method' => 'delete',
121+
'data-remote' => 'true',
122+
'data-confirm' => 'Are you sure?',
123+
'data-disable-with' => 'loading...'
124+
]
125+
);
126+
$this->assertEquals($expect2, $form2);
127+
}
128+
}

tests/JqlTestCase.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
use Mockery as m;
4+
5+
abstract class JqlTestCase extends Orchestra\Testbench\TestCase
6+
{
7+
protected $app;
8+
protected $router;
9+
10+
public function tearDown()
11+
{
12+
parent::tearDown();
13+
m::close();
14+
}
15+
16+
protected function mock($className)
17+
{
18+
$mock = m::mock($className);
19+
App::bind($className, function ($app, $parameters = []) use ($mock) {
20+
if (is_array($parameters) && is_array($attributes = array_get($parameters, 0, [])) && respond_to($mock, "fill")) {
21+
$mock = $this->fillMock($mock, $attributes);
22+
}
23+
24+
return $mock;
25+
});
26+
27+
return $mock;
28+
}
29+
30+
protected function fillMock($mock, $attributes = [])
31+
{
32+
$instance = $mock->makePartial();
33+
foreach ($attributes as $key => $value) {
34+
$instance->$key = $value;
35+
}
36+
37+
return $instance;
38+
}
39+
40+
protected function createSession()
41+
{
42+
// Fake session
43+
app('request')->setSession(
44+
new \Illuminate\Session\Store(
45+
'name',
46+
m::mock('SessionHandlerInterface'),
47+
'aaaa'
48+
)
49+
);
50+
}
51+
52+
protected function getPackageProviders($app)
53+
{
54+
return [
55+
'Collective\Html\HtmlServiceProvider',
56+
'Former\FormerServiceProvider',
57+
'Efficiently\JqueryLaravel\JqueryLaravelServiceProvider',
58+
];
59+
}
60+
61+
protected function getPackageAliases($app)
62+
{
63+
return [
64+
'Form' => 'Collective\Html\FormFacade',
65+
'HTML' => 'Collective\Html\HtmlFacade',
66+
'Former' => 'Former\Facades\Former',
67+
// 'HTMLEloquentHelper' => 'Efficiently\JqueryLaravel\Facades\HTMLEloquentHelper',
68+
];
69+
}
70+
}

tests/fixtures/jql/Project.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php namespace Jql;
2+
3+
use Illuminate\Database\Eloquent\Model;
4+
5+
class Project extends Model
6+
{
7+
public static $rules = [
8+
'name' => 'required',
9+
'priority' => 'required',//|unique:projects',
10+
];
11+
protected $fillable = ['name', 'priority'];
12+
13+
// public function user()
14+
// {
15+
// return $this->belongsTo('User');
16+
// }
17+
// public function tasks()
18+
// {
19+
// return $this->hasMany('Task');
20+
// }
21+
}

0 commit comments

Comments
 (0)