PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/anahkiasen/former/tests/Traits/FieldTest.php

https://gitlab.com/hatemdigify/digifyblog
PHP | 209 lines | 156 code | 44 blank | 9 comment | 2 complexity | 69389fc0f089c70b4df6ce3dc66e870e MD5 | raw file
  1. <?php
  2. namespace Former\Traits;
  3. use Former\TestCases\FormerTests;
  4. use Illuminate\Support\Str;
  5. class FieldTest extends FormerTests
  6. {
  7. ////////////////////////////////////////////////////////////////////
  8. /////////////////////////// DATA PROVIDERS /////////////////////////
  9. ////////////////////////////////////////////////////////////////////
  10. public function provideSizes()
  11. {
  12. $_sizes = array('mini', 'small', 'medium', 'large', 'xlarge', 'xxlarge', 'span1', 'span6', 'span12', 'foo');
  13. foreach ($_sizes as $s) {
  14. $sizes[] = array($s);
  15. }
  16. return $sizes;
  17. }
  18. ////////////////////////////////////////////////////////////////////
  19. //////////////////////////////// TESTS /////////////////////////////
  20. ////////////////////////////////////////////////////////////////////
  21. public function testCanCreateFieldsOutsideForms()
  22. {
  23. $this->former->close();
  24. $field = $this->former->text('foo')->raw();
  25. $this->assertEquals('<input id="foo" type="text" name="foo">', $field->__toString());
  26. }
  27. public function testCanChangeTheFieldIdAndKeepLabelInSync()
  28. {
  29. $field = $this->former->text('foo');
  30. $field->id('bar');
  31. $matcher = '<div class="control-group"><label for="bar" class="control-label">Foo</label><div class="controls"><input id="bar" type="text" name="foo"></div></div>';
  32. $this->assertEquals($matcher, $field->__toString());
  33. }
  34. public function testCanChangeTypeMidCourse()
  35. {
  36. $field = $this->former->text('foo')->setType('email');
  37. $this->assertEquals('email', $field->getType());
  38. }
  39. public function testCanRenameField()
  40. {
  41. $input = $this->former->text('foo')->name('bar')->__toString();
  42. $matcher = $this->controlGroup(
  43. '<input id="bar" type="text" name="bar">',
  44. '<label for="bar" class="control-label">Bar</label>');
  45. $this->assertEquals($matcher, $input);
  46. }
  47. public function testCanSetValueOnFields()
  48. {
  49. $matcher = $this->controlGroup('<input id="foo" type="text" name="foo" value="bar">');
  50. $static = $this->former->text('foo')->value('bar')->__toString();
  51. $this->assertHTML($this->matchField(), $static);
  52. $this->assertHTML($this->matchControlGroup(), $static);
  53. $this->resetLabels();
  54. $input = $this->former->text('foo', null, 'bar')->__toString();
  55. $this->assertHTML($this->matchField(), $input);
  56. $this->assertHTML($this->matchControlGroup(), $input);
  57. }
  58. public function testCanForceValueOnFields()
  59. {
  60. $this->former->populate(array('foo' => 'unbar'));
  61. $static = $this->former->text('foo')->forceValue('bar')->__toString();
  62. $matcher = $this->controlGroup('<input id="foo" type="text" name="foo" value="bar">');
  63. $this->assertEquals($matcher, $static);
  64. }
  65. public function testCanCreateViaMagicAttribute()
  66. {
  67. $static = $this->former->text('foo')->class('foo')->data_bar('bar')->__toString();
  68. $this->assertHTML($this->matchField(), $static);
  69. $this->assertHTML($this->matchControlGroup(), $static);
  70. $this->resetLabels();
  71. $input = $this->former->text('foo', null, null, array('class' => 'foo', 'data-bar' => 'bar'))->__toString();
  72. $this->assertHTML($this->matchField(), $input);
  73. $this->assertHTML($this->matchControlGroup(), $input);
  74. }
  75. public function testCanCreateViaMagicAttributeUnvalue()
  76. {
  77. $static = $this->former->text('foo')->require()->__toString();
  78. $matcher = $this->controlGroup('<input require="true" type="text" name="foo" id="foo">');
  79. $this->assertHTML($this->matchField(), $static);
  80. $this->assertHTML($this->matchControlGroup(), $static);
  81. }
  82. public function testCanSetAttributes()
  83. {
  84. $attributes = array('class' => 'foo', 'data-foo' => 'bar');
  85. $static = $this->former->text('foo')->require()->setAttributes($attributes)->__toString();
  86. $field = $this->matchField();
  87. $field['attributes']['require'] = 'true';
  88. $field['attributes']['class'] = 'foo';
  89. $field['attributes']['data-foo'] = 'bar';
  90. $this->assertHTML($field, $static);
  91. $this->assertHTML($this->matchControlGroup(), $static);
  92. }
  93. public function testCanReplaceAttributes()
  94. {
  95. $attributes = array('class' => 'foo', 'data-foo' => 'bar');
  96. $static = $this->former->text('foo')->require()->replaceAttributes($attributes)->__toString();
  97. $matcher = $this->controlGroup('<input class="foo" data-foo="bar" type="text" name="foo" id="foo">');
  98. $field = $this->matchField();
  99. $field['attributes']['class'] = 'foo';
  100. $field['attributes']['data-foo'] = 'bar';
  101. $this->assertHTML($field, $static);
  102. $this->assertHTML($this->matchControlGroup(), $static);
  103. }
  104. public function testCanGetAttribute()
  105. {
  106. $former = $this->former->span1_text('name')->foo('bar');
  107. $this->assertEquals('span1', $former->class);
  108. $this->assertEquals('bar', $former->foo);
  109. }
  110. public function testCanAddClass()
  111. {
  112. $matcher = $this->controlGroup('<input class="foo bar" type="text" name="foo" id="foo">');
  113. $static = $this->former->text('foo')->class('foo')->addClass('bar')->__toString();
  114. $this->assertHTML($this->matchControlGroup(), $static);
  115. $this->assertHTML($this->matchField(), $static);
  116. $this->resetLabels();
  117. $input = $this->former->text('foo', null, null, array('class' => 'foo'))->addClass('bar')->__toString();
  118. $this->assertHTML($this->matchControlGroup(), $input);
  119. $this->assertHTML($this->matchField(), $input);
  120. }
  121. /**
  122. * @dataProvider provideSizes
  123. */
  124. public function testCanUseMagicMethods($size)
  125. {
  126. $method = $size.'_text';
  127. $class = Str::startsWith($size, 'span') ? $size.' ' : 'input-'.$size.' ';
  128. $static = $this->former->$method('foo')->addClass('bar')->__toString();
  129. if ($class == 'input-foo ') {
  130. $class = null;
  131. }
  132. $field = $this->matchField();
  133. $field['attributes']['class'] = $class.'bar';
  134. $this->assertHTML($this->matchControlGroup(), $static);
  135. $this->assertHTML($field, $static);
  136. }
  137. public function testAutomaticLabelsForSingleSelectField()
  138. {
  139. $field = $this->former->select('foo');
  140. $matcher = '<div class="control-group"><label for="foo" class="control-label">Foo</label><div class="controls"><select id="foo" name="foo"></select></div></div>';
  141. $this->assertEquals($matcher, $field->__toString());
  142. }
  143. public function testAutomaticLabelsForMultiSelectField()
  144. {
  145. $field = $this->former->select('foo[]');
  146. $matcher = '<div class="control-group"><label for="foo[]" class="control-label">Foo</label><div class="controls"><select id="foo[]" name="foo[]"></select></div></div>';
  147. $this->assertEquals($matcher, $field->__toString());
  148. }
  149. public function testCantHaveDuplicateIdsForFields()
  150. {
  151. $field = $this->former->text('name')->render();
  152. $fieldTwo = $this->former->text('name')->render();
  153. $fieldThree = $this->former->text('name')->render();
  154. $this->assertEquals('<input id="name" type="text" name="name">', $field);
  155. $this->assertEquals('<input id="name-2" type="text" name="name">', $fieldTwo);
  156. $this->assertEquals('<input id="name-3" type="text" name="name">', $fieldThree);
  157. }
  158. public function testCanChangeBindingOfField()
  159. {
  160. $this->former->populate(array('bar' => 'unbar'));
  161. $static = $this->former->text('foo')->bind('bar')->__toString();
  162. $matcher = $this->controlGroup('<input id="foo" type="text" name="foo" value="unbar">');
  163. $this->assertEquals($matcher, $static);
  164. }
  165. }