Skip to content

Commit 33fe9cc

Browse files
committed
Add some tests
1 parent 6cd6325 commit 33fe9cc

File tree

8 files changed

+178
-0
lines changed

8 files changed

+178
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor
2+
composer.lock
3+

composer.json

+10
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,19 @@
1313
"php": ">=5.4.0",
1414
"illuminate/support": "~5.0"
1515
},
16+
"require-dev": {
17+
"phpunit/phpunit": "3.7.*",
18+
"mockery/mockery": "0.9.*",
19+
"satooshi/php-coveralls": "dev-master"
20+
},
1621
"autoload": {
1722
"psr-4": {
1823
"Laraplus\\Form\\": "src/"
1924
}
25+
},
26+
"autoload-dev": {
27+
"classmap": [
28+
"tests/TestCase.php"
29+
]
2030
}
2131
}

phpunit.xml

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

src/Elements.php

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Laraplus\Form\Fields\Open;
55
use Laraplus\Form\Fields\Text;
66
use Laraplus\Form\Fields\Close;
7+
use Laraplus\Form\Fields\Group;
78
use Laraplus\Form\Fields\Select;
89
use Laraplus\Form\Fields\Submit;
910
use Laraplus\Form\Fields\Password;

src/Form.php

+18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace Laraplus\Form;
22

3+
use Closure;
34
use Exception;
45
use ArrayAccess;
56
use Laraplus\Form\Fields\Open;
@@ -118,6 +119,23 @@ public function style($style = null)
118119
return $this;
119120
}
120121

122+
123+
/**
124+
* @param $name
125+
* @param Closure $builder
126+
* @return $this
127+
*/
128+
public function group($name, Closure $builder)
129+
{
130+
$instance = new self($this->presenter, $this->dataStore, $this->config);
131+
132+
$builder($instance);
133+
134+
$this->elements[$name ?: 'element-' . count($this->elements)] = $instance;
135+
136+
return $this;
137+
}
138+
121139
/**
122140
* @param string $style
123141
* @return string

src/Presenters/RawPresenter.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php namespace Laraplus\Form\Presenters;
2+
3+
use Laraplus\Form\Fields\Base\Button;
4+
use Laraplus\Form\Fields\Open;
5+
6+
class RawPresenter extends BasePresenter
7+
{
8+
/**
9+
* @param Open $open
10+
* @return string
11+
*/
12+
public function renderOpeningTag(Open $open)
13+
{
14+
return '<form' . $this->renderAttributes($open->attributes) . '>';
15+
}
16+
17+
/**
18+
* @param Button $button
19+
* @return string
20+
*/
21+
public function renderButton(Button $button)
22+
{
23+
return '<button' . $this->renderAttributes($button->attributes) . '>' . $button->text . '</button>';
24+
}
25+
26+
/**
27+
* @return string
28+
*/
29+
public function renderLabel()
30+
{
31+
return '<label for="' . $this->attributes['id'] . '">' . $this->label . '</label>';
32+
}
33+
34+
/**
35+
* @return string
36+
*/
37+
public function renderField()
38+
{
39+
return $this->element->render();
40+
}
41+
42+
/**
43+
* @return string
44+
*/
45+
public function renderError()
46+
{
47+
return '<strong>' . $this->error . '</strong>';
48+
}
49+
50+
/**
51+
* @return string
52+
*/
53+
public function renderAll()
54+
{
55+
return '<div>' . $this->renderLabel() . $this->renderField() . $this->renderError() . '</div>';
56+
}
57+
}

tests/TestBasicForm.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
class TestBasicForm extends TestCase
4+
{
5+
public function testEmptyFormWithNoToken()
6+
{
7+
$expect = '<form method="GET" action="/"></form>';
8+
9+
$this->form->open('test');
10+
$this->form->close()->noToken();
11+
12+
$this->assertEquals($expect, $this->clean($this->form));
13+
}
14+
15+
public function testEmptyFormWithToken()
16+
{
17+
$expect = '<form method="GET" action="/"><input type="hidden" name="_token" value="secret_token" /></form>';
18+
19+
$this->form->open('test');
20+
$this->form->close();
21+
22+
$this->assertEquals($expect, $this->clean($this->form));
23+
}
24+
}

tests/TestCase.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
use Laraplus\Form\Contracts\ConfigProvider;
4+
use Laraplus\Form\Form;
5+
use Laraplus\Form\Contracts\DataStore;
6+
use Laraplus\Form\Presenters\RawPresenter;
7+
8+
class TestCase extends PHPUnit_Framework_TestCase
9+
{
10+
protected $form;
11+
12+
public function setUp()
13+
{
14+
$presenter = new RawPresenter();
15+
$dataStore = $this->emptyDataStore();
16+
$configProvider = $this->emptyConfigProvider();
17+
18+
$this->form = new Form($presenter, $dataStore, $configProvider);
19+
}
20+
21+
protected function emptyDataStore()
22+
{
23+
$dataStore = Mockery::mock(DataStore::class);
24+
$dataStore->shouldReceive('getUrl')->andReturn('/');
25+
$dataStore->shouldReceive('getToken')->andReturn('secret_token');
26+
27+
return $dataStore;
28+
}
29+
30+
protected function emptyConfigProvider()
31+
{
32+
$config = Mockery::mock(ConfigProvider::class);
33+
$config->shouldReceive('get')->andReturn(null);
34+
35+
return $config;
36+
}
37+
38+
protected function clean($string)
39+
{
40+
return str_replace("\n", '', $string);
41+
}
42+
43+
public function tearDown()
44+
{
45+
Mockery::close();
46+
}
47+
}

0 commit comments

Comments
 (0)