SonataAdminExtensionTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Tests\Twig\Extension;
  11. use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension;
  12. use Sonata\AdminBundle\Admin\Pool;
  13. use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
  14. class SonataAdminExtensionTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @var SonataAdminExtension
  18. */
  19. private $twigExtension;
  20. public function setUp()
  21. {
  22. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  23. $pool = new Pool($container, '', '');
  24. $this->twigExtension = new SonataAdminExtension($pool);
  25. $loader = new StubFilesystemLoader(array(
  26. __DIR__.'/../../../Resources/views/CRUD',
  27. ));
  28. $environment = new \Twig_Environment($loader, array('strict_variables' => true));
  29. $environment->addExtension($this->twigExtension);
  30. $this->twigExtension->initRuntime($environment);
  31. }
  32. public function testSlugify()
  33. {
  34. $this->assertEquals($this->twigExtension->slugify('test'), 'test');
  35. $this->assertEquals($this->twigExtension->slugify('S§!@@#$#$alut'), 's-alut');
  36. $this->assertEquals($this->twigExtension->slugify('Symfony2'), 'symfony2');
  37. $this->assertEquals($this->twigExtension->slugify('test'), 'test');
  38. $this->assertEquals($this->twigExtension->slugify('c\'est bientôt l\'été'), 'c-est-bientot-l-ete');
  39. $this->assertEquals($this->twigExtension->slugify(urldecode('%2Fc\'est+bientôt+l\'été')), 'c-est-bientot-l-ete');
  40. }
  41. /**
  42. * @dataProvider getRenderListElementTests
  43. */
  44. public function testRenderListElement($expectedOutput, $type, $value)
  45. {
  46. $object = new \stdClass();
  47. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  48. $admin->expects($this->any())
  49. ->method('getTemplate')
  50. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
  51. $admin->expects($this->any())
  52. ->method('id')
  53. ->with($this->equalTo($object))
  54. ->will($this->returnValue(12345));
  55. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  56. $fieldDescription->expects($this->any())
  57. ->method('getAdmin')
  58. ->will($this->returnValue($admin));
  59. $fieldDescription->expects($this->any())
  60. ->method('getValue')
  61. ->will($this->returnValue($value));
  62. $fieldDescription->expects($this->any())
  63. ->method('getType')
  64. ->will($this->returnValue($type));
  65. $fieldDescription->expects($this->any())
  66. ->method('getOptions')
  67. ->will($this->returnValue(array('currency' => 'EUR')));
  68. $fieldDescription->expects($this->any())
  69. ->method('getTemplate')
  70. ->will($this->returnCallback(
  71. function() use ($type) {
  72. switch ($type) {
  73. case 'string':
  74. return 'SonataAdminBundle:CRUD:list_string.html.twig';
  75. case 'boolean':
  76. return 'SonataAdminBundle:CRUD:list_boolean.html.twig';
  77. case 'datetime':
  78. return 'SonataAdminBundle:CRUD:list_datetime.html.twig';
  79. case 'date':
  80. return 'SonataAdminBundle:CRUD:list_date.html.twig';
  81. case 'time':
  82. return 'SonataAdminBundle:CRUD:list_time.html.twig';
  83. case 'currency':
  84. return 'SonataAdminBundle:CRUD:list_currency.html.twig';
  85. case 'percent':
  86. return 'SonataAdminBundle:CRUD:list_percent.html.twig';
  87. case 'array':
  88. return 'SonataAdminBundle:CRUD:list_array.html.twig';
  89. default:
  90. return false;
  91. }
  92. }
  93. ));
  94. $this->assertEquals($expectedOutput, trim(preg_replace('/\s+/', ' ', $this->twigExtension->renderListElement($object, $fieldDescription))));
  95. }
  96. public function getRenderListElementTests()
  97. {
  98. //@todo Add tests for "boolean" and "trans" type
  99. return array(
  100. array('<td class="sonata-ba-list-field sonata-ba-list-field-string" objectId="12345"> Example </td>', 'string', 'Example'),
  101. array('<td class="sonata-ba-list-field sonata-ba-list-field-text" objectId="12345"> Example </td>', 'text', 'Example'),
  102. array('<td class="sonata-ba-list-field sonata-ba-list-field-textarea" objectId="12345"> Example </td>', 'textarea', 'Example'),
  103. array('<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> December 24, 2013 11:11 </td>', 'datetime', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London'))),
  104. array('<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> December 24, 2013 </td>', 'date', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London'))),
  105. array('<td class="sonata-ba-list-field sonata-ba-list-field-time" objectId="12345"> 11:11:12 </td>', 'time', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London'))),
  106. array('<td class="sonata-ba-list-field sonata-ba-list-field-number" objectId="12345"> 10.746135 </td>', 'number', 10.746135),
  107. array('<td class="sonata-ba-list-field sonata-ba-list-field-integer" objectId="12345"> 5678 </td>', 'integer', 5678),
  108. array('<td class="sonata-ba-list-field sonata-ba-list-field-percent" objectId="12345"> 1074.6135 % </td>', 'percent', 10.746135),
  109. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> EUR 10.746135 </td>', 'currency', 10.746135),
  110. array('<td class="sonata-ba-list-field sonata-ba-list-field-array" objectId="12345"> [1 => First] [2 => Second] </td>', 'array', array(1 => 'First', 2 => 'Second')),
  111. );
  112. }
  113. /**
  114. * @dataProvider getRenderViewElementTests
  115. */
  116. public function testRenderViewElement($expectedOutput, $type, $value)
  117. {
  118. $object = new \stdClass();
  119. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  120. $admin->expects($this->any())
  121. ->method('getTemplate')
  122. ->will($this->returnValue('SonataAdminBundle:CRUD:base_show_field.html.twig'));
  123. $admin->expects($this->any())
  124. ->method('id')
  125. ->with($this->equalTo($object))
  126. ->will($this->returnValue(12345));
  127. $admin->expects($this->any())
  128. ->method('trans')
  129. ->will($this->returnCallback(function($id) {
  130. return $id;
  131. }));
  132. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  133. $fieldDescription->expects($this->any())
  134. ->method('getAdmin')
  135. ->will($this->returnValue($admin));
  136. $fieldDescription->expects($this->any())
  137. ->method('getValue')
  138. ->will($this->returnValue($value));
  139. $fieldDescription->expects($this->any())
  140. ->method('getLabel')
  141. ->will($this->returnValue('Data'));
  142. $fieldDescription->expects($this->any())
  143. ->method('getType')
  144. ->will($this->returnValue($type));
  145. $fieldDescription->expects($this->any())
  146. ->method('getOptions')
  147. ->will($this->returnValue(array('currency' => 'EUR', 'safe' => false)));
  148. $fieldDescription->expects($this->any())
  149. ->method('getTemplate')
  150. ->will($this->returnCallback(
  151. function() use ($type) {
  152. switch ($type) {
  153. case 'boolean':
  154. return 'SonataAdminBundle:CRUD:show_boolean.html.twig';
  155. case 'datetime':
  156. return 'SonataAdminBundle:CRUD:show_datetime.html.twig';
  157. case 'date':
  158. return 'SonataAdminBundle:CRUD:show_date.html.twig';
  159. case 'time':
  160. return 'SonataAdminBundle:CRUD:show_time.html.twig';
  161. case 'currency':
  162. return 'SonataAdminBundle:CRUD:show_currency.html.twig';
  163. case 'percent':
  164. return 'SonataAdminBundle:CRUD:show_percent.html.twig';
  165. case 'array':
  166. return 'SonataAdminBundle:CRUD:show_array.html.twig';
  167. default:
  168. return false;
  169. }
  170. }
  171. ));
  172. $this->assertEquals($expectedOutput, trim(preg_replace('/\s+/', ' ', $this->twigExtension->renderListElement($object, $fieldDescription))));
  173. }
  174. public function getRenderViewElementTests()
  175. {
  176. //@todo Add tests for "boolean" and "trans" type
  177. return array(
  178. array('<th>Data</th> <td>Example</td>', 'string', 'Example'),
  179. array('<th>Data</th> <td>Example</td>', 'text', 'Example'),
  180. array('<th>Data</th> <td>Example</td>', 'textarea', 'Example'),
  181. array('<th>Data</th> <td>December 24, 2013 11:11</td>', 'datetime', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London'))),
  182. array('<th>Data</th> <td>December 24, 2013</td>', 'date', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London'))),
  183. array('<th>Data</th> <td>11:11:12</td>', 'time', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London'))),
  184. array('<th>Data</th> <td>10.746135</td>', 'number', 10.746135),
  185. array('<th>Data</th> <td>5678</td>', 'integer', 5678),
  186. array('<th>Data</th> <td> 1074.6135 % </td>', 'percent', 10.746135),
  187. array('<th>Data</th> <td> EUR 10.746135 </td>', 'currency', 10.746135),
  188. array('<th>Data</th> <td> [1 => First] [2 => Second] </td>', 'array', array(1 => 'First', 2 => 'Second')),
  189. );
  190. }
  191. }