| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- use Watson\BootstrapForm\BootstrapForm;
- class BootstrapFormTest extends PHPUnit_Framework_TestCase
- {
- protected $bootstrapForm;
- protected $htmlBuilderMock;
- protected $formBuidlerMock;
- protected $configMock;
- protected $sessionMock;
- public function setUp()
- {
- $this->htmlBuilderMock = Mockery::mock('Collective\Html\HtmlBuilder');
- $this->formBuidlerMock = Mockery::mock('Collective\Html\FormBuilder');
- $this->configMock = Mockery::mock('Illuminate\Contracts\Config\Repository')->shouldDeferMissing();
- $this->sessionMock = Mockery::mock('Illuminate\Session\SessionManager')->shouldDeferMissing();
- $this->bootstrapForm = new BootstrapForm(
- $this->htmlBuilderMock,
- $this->formBuidlerMock,
- $this->configMock,
- $this->sessionMock
- );
- }
- /** @test */
- public function it_opens_default_form()
- {
- $this->formBuidlerMock->shouldReceive('open')->once()->with([
- 'role' => 'form',
- 'class' => 'form-horizontal'
- ])->andReturn('foo');
- $this->configMock->shouldReceive('get')
- ->with('bootstrap_form.type')
- ->once()
- ->andReturn('form-horizontal');
- $result = $this->bootstrapForm->open();
- $this->assertEquals('foo', $result);
- }
- /** @test */
- public function it_opens_store_model_form()
- {
- $model = Mockery::mock('Illuminate\Database\Eloquent\Model');
- $model->exists = false;
- $this->formBuidlerMock->shouldReceive('model')
- ->once()
- ->with($model, [
- 'role' => 'form',
- 'route' => 'bar',
- 'method' => 'POST',
- 'class' => 'form-horizontal',
- ])
- ->andReturn('foo');
- $this->configMock->shouldReceive('get')
- ->with('bootstrap_form.type')
- ->once()
- ->andReturn('form-horizontal');
- $result = $this->bootstrapForm->open([
- 'model' => $model,
- 'store' => 'bar',
- 'update' => 'baz'
- ]);
- $this->assertEquals('foo', $result);
- }
- /** @test */
- public function it_opens_update_model_form()
- {
- $model = Mockery::mock('Illuminate\Database\Eloquent\Model');
- $model->exists = true;
- $model->shouldReceive('getRouteKey')
- ->once()
- ->andReturn(1);
- $this->formBuidlerMock->shouldReceive('model')
- ->once()
- ->with($model, [
- 'role' => 'form',
- 'route' => ['baz', 1],
- 'method' => 'PUT',
- 'class' => 'form-horizontal',
- ])
- ->andReturn('foo');
- $this->configMock->shouldReceive('get')
- ->with('bootstrap_form.type')
- ->once()
- ->andReturn('form-horizontal');
- $result = $this->bootstrapForm->open([
- 'model' => $model,
- 'store' => 'bar',
- 'update' => 'baz'
- ]);
- $this->assertEquals('foo', $result);
- }
- /** @test */
- public function it_opens_a_vertical_form()
- {
- $this->formBuidlerMock->shouldReceive('open')
- ->with([
- 'role' => 'form',
- 'class' => '',
- ])
- ->once()
- ->andReturn('foo');
- $result = $this->bootstrapForm->vertical();
- $this->assertEquals('foo', $result);
- }
- /** @test */
- public function it_opens_an_inline_form()
- {
- $this->formBuidlerMock->shouldReceive('open')
- ->with([
- 'class' => 'form-inline',
- 'role' => 'form'
- ])
- ->once()
- ->andReturn('foo');
- $result = $this->bootstrapForm->inline();
- $this->assertEquals('foo', $result);
- }
- /** @test */
- public function it_opens_a_horizontal_form()
- {
- $this->formBuidlerMock->shouldReceive('open')
- ->with([
- 'class' => 'form-horizontal',
- 'role' => 'form'
- ])
- ->once()
- ->andReturn('foo');
- $result = $this->bootstrapForm->horizontal();
- $this->assertEquals('foo', $result);
- }
- /** @test */
- public function it_closes_a_form()
- {
- $this->formBuidlerMock->shouldReceive('close')->once()->andReturn('foo');
- $result = $this->bootstrapForm->close();
- $this->assertEquals('foo', $result);
- }
- /** @test */
- public function it_returns_normal_field_names()
- {
- $result = $this->bootstrapForm->flattenFieldName('foo');
- $this->assertEquals('foo', $result);
- }
- /** @test */
- public function it_removes_empty_array_from_field_name()
- {
- $result = $this->bootstrapForm->flattenFieldName('foo[]');
- $this->assertEquals('foo', $result);
- }
- /** @test */
- public function it_flattens_array_from_field_name()
- {
- $result = $this->bootstrapForm->flattenFieldName('foo[bar]');
- $this->assertEquals('foo.bar', $result);
- }
-
- /** @test */
- public function in_allows_zero_in_field_name()
- {
- $result = $this->bootstrapForm->flattenFieldName('foo[0]');
- $this->assertEquals('foo.0', $result);
- }
- /** @test */
- public function it_flattens_nested_array_from_field_name()
- {
- $result = $this->bootstrapForm->flattenFieldName('foo[bar][baz]');
- $this->assertEquals('foo.bar.baz', $result);
- }
- }
|