SonataAdminExtensionTest.php 11 KB

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