Skip to content
This repository was archived by the owner on Aug 30, 2020. It is now read-only.

Commit 1f5efc3

Browse files
committed
testing
1 parent 72ddb63 commit 1f5efc3

File tree

8 files changed

+463
-0
lines changed

8 files changed

+463
-0
lines changed

composer.json

+9
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,20 @@
1919
"laravelcollective/html": "^5.6|^6.0|^7.0",
2020
"elnooronline/laravel-locales": "^2.0"
2121
},
22+
"require-dev": {
23+
"orchestra/testbench": "^5.3",
24+
"mockery/mockery": "^1.4"
25+
},
2226
"autoload": {
2327
"psr-4": {
2428
"Elnooronline\\LaravelBootstrapForms\\": "src/"
2529
}
2630
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"Elnooronline\\LaravelBootstrapForms\\Tests\\": "tests/"
34+
}
35+
},
2736
"extra": {
2837
"laravel": {
2938
"providers": [

phpunit.xml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="Laravel Bootstrap Forms Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
</phpunit>

src/Providers/BootstrapFormsServiceProvider.php

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public function boot()
2626
], 'laravel-bootstrap-forms.views');
2727

2828
FormDirectives::register();
29+
30+
if ($this->app->runningInConsole() || $this->app->runningUnitTests()) {
31+
$this->loadTranslationsFrom(__DIR__.'/../../tests/resources/lang', 'test');
32+
$this->loadViewsFrom(__DIR__.'/../../tests/resources/views', 'test');
33+
}
2934
}
3035

3136
/**

tests/TestCase.php

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
namespace Elnooronline\LaravelBootstrapForms\Tests;
4+
5+
use Collective\Html\FormFacade;
6+
use Elnooronline\LaravelBootstrapForms\Facades\BsForm;
7+
use Illuminate\Support\Facades\View;
8+
use Illuminate\Support\ViewErrorBag;
9+
use Collective\Html\HtmlServiceProvider;
10+
use Orchestra\Testbench\TestCase as OrchestraTestCase;
11+
use Elnooronline\LaravelLocales\Providers\LocalesServiceProvider;
12+
use Elnooronline\LaravelBootstrapForms\Providers\BootstrapFormsServiceProvider;
13+
14+
class TestCase extends OrchestraTestCase
15+
{
16+
/**
17+
* @var \Illuminate\Routing\UrlGenerator
18+
*/
19+
protected $urlGenerator;
20+
21+
/**
22+
* @var \Illuminate\Contracts\View\Factory|\Mockery\LegacyMockInterface|\Mockery\MockInterface
23+
*/
24+
protected $viewFactory;
25+
26+
/**
27+
* @var \Illuminate\Contracts\Session\Session|\Mockery\LegacyMockInterface|\Mockery\MockInterface
28+
*/
29+
protected $session;
30+
31+
/**
32+
* @var \Illuminate\Contracts\Foundation\Application|mixed
33+
*/
34+
protected $blade;
35+
36+
/**
37+
* Setup the test environment.
38+
*
39+
* @return void
40+
*/
41+
public function setUp(): void
42+
{
43+
parent::setUp();
44+
45+
View::share('errors', new ViewErrorBag);
46+
47+
$this->app->setLocale('en');
48+
49+
$this->blade = resolve('blade.compiler');
50+
}
51+
52+
/**
53+
* Load package service provider.
54+
*
55+
* @param \Illuminate\Foundation\Application $app
56+
* @return array
57+
*/
58+
protected function getPackageProviders($app)
59+
{
60+
return [
61+
HtmlServiceProvider::class,
62+
BootstrapFormsServiceProvider::class,
63+
LocalesServiceProvider::class,
64+
];
65+
}
66+
67+
/**
68+
* Get package aliases.
69+
*
70+
* @param \Illuminate\Foundation\Application $app
71+
*
72+
* @return array
73+
*/
74+
protected function getPackageAliases($app)
75+
{
76+
return [
77+
'Form' => FormFacade::class,
78+
'BsForm' => BsForm::class,
79+
];
80+
}
81+
82+
/**
83+
* Define environment setup.
84+
*
85+
* @param \Illuminate\Foundation\Application $app
86+
*
87+
* @return void
88+
*/
89+
protected function getEnvironmentSetUp($app)
90+
{
91+
$app['config']->set('session.driver', 'array');
92+
}
93+
94+
/**
95+
* Minify html content.
96+
*
97+
* @param $input
98+
* @return string|string[]|null
99+
*/
100+
protected function minifyHtml($input) {
101+
if(trim($input) === "") return $input;
102+
// Remove extra white-space(s) between HTML attribute(s)
103+
$input = preg_replace_callback('#<([^\/\s<>!]+)(?:\s+([^<>]*?)\s*|\s*)(\/?)>#s', function($matches) {
104+
return '<' . $matches[1] . preg_replace('#([^\s=]+)(\=([\'"]?)(.*?)\3)?(\s+|$)#s', ' $1$2', $matches[2]) . $matches[3] . '>';
105+
}, str_replace("\r", "", $input));
106+
107+
return preg_replace(
108+
array(
109+
// t = text
110+
// o = tag open
111+
// c = tag close
112+
// Keep important white-space(s) after self-closing HTML tag(s)
113+
'#<(img|input)(>| .*?>)#s',
114+
// Remove a line break and two or more white-space(s) between tag(s)
115+
'#(<!--.*?-->)|(>)(?:\n*|\s{2,})(<)|^\s*|\s*$#s',
116+
'#(<!--.*?-->)|(?<!\>)\s+(<\/.*?>)|(<[^\/]*?>)\s+(?!\<)#s', // t+c || o+t
117+
'#(<!--.*?-->)|(<[^\/]*?>)\s+(<[^\/]*?>)|(<\/.*?>)\s+(<\/.*?>)#s', // o+o || c+c
118+
'#(<!--.*?-->)|(<\/.*?>)\s+(\s)(?!\<)|(?<!\>)\s+(\s)(<[^\/]*?\/?>)|(<[^\/]*?\/?>)\s+(\s)(?!\<)#s', // c+t || t+o || o+t -- separated by long white-space(s)
119+
'#(<!--.*?-->)|(<[^\/]*?>)\s+(<\/.*?>)#s', // empty tag
120+
'#<(img|input)(>| .*?>)<\/\1>#s', // reset previous fix
121+
'#(&nbsp;)&nbsp;(?![<\s])#', // clean up ...
122+
'#(?<=\>)(&nbsp;)(?=\<)#', // --ibid
123+
// Remove HTML comment(s) except IE comment(s)
124+
'#\s*<!--(?!\[if\s).*?-->\s*|(?<!\>)\n+(?=\<[^!])#s'
125+
),
126+
array(
127+
'<$1$2</$1>',
128+
'$1$2$3',
129+
'$1$2$3',
130+
'$1$2$3$4$5',
131+
'$1$2$3$4$5$6$7',
132+
'$1$2$3',
133+
'<$1$2',
134+
'$1 ',
135+
'$1',
136+
""
137+
),
138+
$input);
139+
}
140+
}

tests/Unit/TextComponentTest.php

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
namespace Elnooronline\LaravelBootstrapForms\Tests\Unit;
4+
5+
use Illuminate\Support\Str;
6+
use Elnooronline\LaravelBootstrapForms\Facades\BsForm;
7+
use Elnooronline\LaravelBootstrapForms\Tests\TestCase;
8+
9+
class TextComponentTest extends TestCase
10+
{
11+
/** @test */
12+
public function it_can_generate_an_input_field()
13+
{
14+
$textInput = BsForm::resource(null)->text('body')->toHtml();
15+
16+
$this->assertEquals(
17+
$this->minifyHtml($textInput),
18+
'<div class="form-group"><input class="form-control" name="body" type="text"><small class="form-text text-muted"></small></div>'
19+
);
20+
}
21+
22+
/** @test */
23+
public function it_can_generate_required_input_field()
24+
{
25+
$textInput = BsForm::resource(null)->text('body')->required()->toHtml();
26+
27+
$this->assertEquals(
28+
$this->minifyHtml($textInput),
29+
'<div class="form-group"><input class="form-control" required="required" name="body" type="text"><small class="form-text text-muted"></small></div>'
30+
);
31+
}
32+
33+
/** @test */
34+
public function it_can_generate_an_input_field_with_max_length_attribute()
35+
{
36+
$textInput = BsForm::resource(null)->text('body')->maxLength(2)->toHtml();
37+
38+
$this->assertEquals(
39+
$this->minifyHtml($textInput),
40+
'<div class="form-group"><input class="form-control" maxlength="2" name="body" type="text"><small class="form-text text-muted"></small></div>'
41+
);
42+
}
43+
44+
/** @test */
45+
public function it_can_generate_an_input_field_with_autofocus_attribute()
46+
{
47+
$textInput = BsForm::resource(null)->text('body')->autofocus()->toHtml();
48+
49+
$this->assertEquals(
50+
$this->minifyHtml($textInput),
51+
'<div class="form-group"><input class="form-control" autofocus="autofocus" name="body" type="text"><small class="form-text text-muted"></small></div>'
52+
);
53+
}
54+
55+
/** @test */
56+
public function it_can_generate_an_input_field_with_value()
57+
{
58+
$textInput = BsForm::resource(null)->text('body')->value('foo')->toHtml();
59+
60+
$this->assertEquals(
61+
$this->minifyHtml($textInput),
62+
'<div class="form-group"><input class="form-control" name="body" type="text" value="foo"><small class="form-text text-muted"></small></div>'
63+
);
64+
}
65+
66+
/** @test */
67+
public function it_can_generate_an_input_field_with_custom_attribute()
68+
{
69+
$textInput = BsForm::resource(null)->text('body')->attribute('foo', 'bar')->toHtml();
70+
71+
$this->assertEquals(
72+
$this->minifyHtml($textInput),
73+
'<div class="form-group"><input class="form-control" foo="bar" name="body" type="text"><small class="form-text text-muted"></small></div>'
74+
);
75+
}
76+
77+
/** @test */
78+
public function it_can_generate_an_input_field_with_resource()
79+
{
80+
$textInput = BsForm::resource('test::blogs')->text('body')->toHtml();
81+
82+
$this->assertEquals(
83+
$this->minifyHtml($textInput),
84+
'<div class="form-group"><label for="body">Body</label><input class="form-control" placeholder="Write something" name="body" type="text" id="body"><small class="form-text text-muted"></small></div>'
85+
);
86+
}
87+
88+
/** @test */
89+
public function it_can_generate_an_input_field_with_custom_label()
90+
{
91+
$textInput = BsForm::resource('test::blogs')->text('body')->label('foo')->toHtml();
92+
93+
$this->assertEquals(
94+
$this->minifyHtml($textInput),
95+
'<div class="form-group"><label for="body">foo</label><input class="form-control" placeholder="Write something" name="body" type="text" id="body"><small class="form-text text-muted"></small></div>'
96+
);
97+
}
98+
99+
/** @test */
100+
public function it_can_generate_an_input_field_with_custom_placeholder()
101+
{
102+
$textInput = BsForm::resource('test::blogs')->text('body')->placeholder('foo')->toHtml();
103+
104+
$this->assertEquals(
105+
$this->minifyHtml($textInput),
106+
'<div class="form-group"><label for="body">Body</label><input class="form-control" placeholder="foo" name="body" type="text" id="body"><small class="form-text text-muted"></small></div>'
107+
);
108+
}
109+
110+
/** @test */
111+
public function it_can_generate_an_input_field_with_custom_note()
112+
{
113+
$textInput = BsForm::resource('test::blogs')->text('body')->note('foo')->toHtml();
114+
115+
$this->assertEquals(
116+
$this->minifyHtml($textInput),
117+
'<div class="form-group"><label for="body">Body</label><input class="form-control" placeholder="Write something" name="body" type="text" id="body"><small class="form-text text-muted">foo</small></div>'
118+
);
119+
}
120+
121+
/** @test */
122+
public function it_can_generate_multilingual_input_field()
123+
{
124+
$this->assertTrue(
125+
Str::contains(
126+
$this->minifyHtml(view('test::blogs')->render()),
127+
'class="nav nav-tabs"'
128+
)
129+
);
130+
$this->assertTrue(
131+
Str::contains(
132+
$this->minifyHtml(view('test::blogs')->render()),
133+
'name="body:en"'
134+
)
135+
);
136+
}
137+
}

0 commit comments

Comments
 (0)