BootstrapFormTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. use Watson\BootstrapForm\BootstrapForm;
  3. class BootstrapFormTest extends PHPUnit_Framework_TestCase
  4. {
  5. protected $bootstrapForm;
  6. protected $htmlBuilderMock;
  7. protected $formBuidlerMock;
  8. protected $configMock;
  9. protected $sessionMock;
  10. public function setUp()
  11. {
  12. $this->htmlBuilderMock = Mockery::mock('Collective\Html\HtmlBuilder');
  13. $this->formBuidlerMock = Mockery::mock('Collective\Html\FormBuilder');
  14. $this->configMock = Mockery::mock('Illuminate\Contracts\Config\Repository')->shouldDeferMissing();
  15. $this->sessionMock = Mockery::mock('Illuminate\Session\SessionManager')->shouldDeferMissing();
  16. $this->bootstrapForm = new BootstrapForm(
  17. $this->htmlBuilderMock,
  18. $this->formBuidlerMock,
  19. $this->configMock,
  20. $this->sessionMock
  21. );
  22. }
  23. /** @test */
  24. public function it_opens_default_form()
  25. {
  26. $this->formBuidlerMock->shouldReceive('open')->once()->with([
  27. 'role' => 'form',
  28. 'class' => 'form-horizontal'
  29. ])->andReturn('foo');
  30. $this->configMock->shouldReceive('get')
  31. ->with('bootstrap_form.type')
  32. ->once()
  33. ->andReturn('form-horizontal');
  34. $result = $this->bootstrapForm->open();
  35. $this->assertEquals('foo', $result);
  36. }
  37. /** @test */
  38. public function it_opens_store_model_form()
  39. {
  40. $model = Mockery::mock('Illuminate\Database\Eloquent\Model');
  41. $model->exists = false;
  42. $this->formBuidlerMock->shouldReceive('model')
  43. ->once()
  44. ->with($model, [
  45. 'role' => 'form',
  46. 'route' => 'bar',
  47. 'method' => 'POST',
  48. 'class' => 'form-horizontal',
  49. ])
  50. ->andReturn('foo');
  51. $this->configMock->shouldReceive('get')
  52. ->with('bootstrap_form.type')
  53. ->once()
  54. ->andReturn('form-horizontal');
  55. $result = $this->bootstrapForm->open([
  56. 'model' => $model,
  57. 'store' => 'bar',
  58. 'update' => 'baz'
  59. ]);
  60. $this->assertEquals('foo', $result);
  61. }
  62. /** @test */
  63. public function it_opens_update_model_form()
  64. {
  65. $model = Mockery::mock('Illuminate\Database\Eloquent\Model');
  66. $model->exists = true;
  67. $model->shouldReceive('getRouteKey')
  68. ->once()
  69. ->andReturn(1);
  70. $this->formBuidlerMock->shouldReceive('model')
  71. ->once()
  72. ->with($model, [
  73. 'role' => 'form',
  74. 'route' => ['baz', 1],
  75. 'method' => 'PUT',
  76. 'class' => 'form-horizontal',
  77. ])
  78. ->andReturn('foo');
  79. $this->configMock->shouldReceive('get')
  80. ->with('bootstrap_form.type')
  81. ->once()
  82. ->andReturn('form-horizontal');
  83. $result = $this->bootstrapForm->open([
  84. 'model' => $model,
  85. 'store' => 'bar',
  86. 'update' => 'baz'
  87. ]);
  88. $this->assertEquals('foo', $result);
  89. }
  90. /** @test */
  91. public function it_opens_a_vertical_form()
  92. {
  93. $this->formBuidlerMock->shouldReceive('open')
  94. ->with([
  95. 'role' => 'form',
  96. 'class' => '',
  97. ])
  98. ->once()
  99. ->andReturn('foo');
  100. $result = $this->bootstrapForm->vertical();
  101. $this->assertEquals('foo', $result);
  102. }
  103. /** @test */
  104. public function it_opens_an_inline_form()
  105. {
  106. $this->formBuidlerMock->shouldReceive('open')
  107. ->with([
  108. 'class' => 'form-inline',
  109. 'role' => 'form'
  110. ])
  111. ->once()
  112. ->andReturn('foo');
  113. $result = $this->bootstrapForm->inline();
  114. $this->assertEquals('foo', $result);
  115. }
  116. /** @test */
  117. public function it_opens_a_horizontal_form()
  118. {
  119. $this->formBuidlerMock->shouldReceive('open')
  120. ->with([
  121. 'class' => 'form-horizontal',
  122. 'role' => 'form'
  123. ])
  124. ->once()
  125. ->andReturn('foo');
  126. $result = $this->bootstrapForm->horizontal();
  127. $this->assertEquals('foo', $result);
  128. }
  129. /** @test */
  130. public function it_closes_a_form()
  131. {
  132. $this->formBuidlerMock->shouldReceive('close')->once()->andReturn('foo');
  133. $result = $this->bootstrapForm->close();
  134. $this->assertEquals('foo', $result);
  135. }
  136. /** @test */
  137. public function it_returns_normal_field_names()
  138. {
  139. $result = $this->bootstrapForm->flattenFieldName('foo');
  140. $this->assertEquals('foo', $result);
  141. }
  142. /** @test */
  143. public function it_removes_empty_array_from_field_name()
  144. {
  145. $result = $this->bootstrapForm->flattenFieldName('foo[]');
  146. $this->assertEquals('foo', $result);
  147. }
  148. /** @test */
  149. public function it_flattens_array_from_field_name()
  150. {
  151. $result = $this->bootstrapForm->flattenFieldName('foo[bar]');
  152. $this->assertEquals('foo.bar', $result);
  153. }
  154. /** @test */
  155. public function in_allows_zero_in_field_name()
  156. {
  157. $result = $this->bootstrapForm->flattenFieldName('foo[0]');
  158. $this->assertEquals('foo.0', $result);
  159. }
  160. /** @test */
  161. public function it_flattens_nested_array_from_field_name()
  162. {
  163. $result = $this->bootstrapForm->flattenFieldName('foo[bar][baz]');
  164. $this->assertEquals('foo.bar.baz', $result);
  165. }
  166. }