SonataAdminExtensionTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. use Symfony\Bridge\Twig\Extension\TranslationExtension;
  15. use Symfony\Component\Translation\Translator;
  16. use Symfony\Component\Translation\MessageSelector;
  17. use Symfony\Component\Translation\Loader\XliffFileLoader;
  18. use Symfony\Component\Routing\Generator\UrlGenerator;
  19. use Symfony\Component\Routing\Loader\XmlFileLoader;
  20. use Symfony\Component\Config\FileLocator;
  21. use Symfony\Bridge\Twig\Extension\RoutingExtension;
  22. use Symfony\Component\Routing\RequestContext;
  23. use Sonata\AdminBundle\Exception\NoValueException;
  24. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  25. use Sonata\AdminBundle\Admin\AdminInterface;
  26. /**
  27. * Test for SonataAdminExtension
  28. *
  29. * @author Andrej Hudec <pulzarraider@gmail.com>
  30. */
  31. class SonataAdminExtensionTest extends \PHPUnit_Framework_TestCase
  32. {
  33. /**
  34. * @var SonataAdminExtension
  35. */
  36. private $twigExtension;
  37. /**
  38. * @var Twig_Environment
  39. */
  40. private $environment;
  41. /**
  42. * @var AdminInterface
  43. */
  44. private $admin;
  45. /**
  46. * @var FieldDescriptionInterface
  47. */
  48. private $fieldDescription;
  49. /**
  50. * @var \stdClass
  51. */
  52. private $object;
  53. /**
  54. * @var Pool
  55. */
  56. private $pool;
  57. public function setUp()
  58. {
  59. date_default_timezone_set('Europe/London');
  60. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  61. $this->pool = new Pool($container, '', '');
  62. $this->twigExtension = new SonataAdminExtension($this->pool);
  63. $loader = new StubFilesystemLoader(array(
  64. __DIR__.'/../../../Resources/views/CRUD',
  65. ));
  66. $this->environment = new \Twig_Environment($loader, array('strict_variables' => true, 'cache' => false, 'autoescape' => true, 'optimizations' => 0));
  67. $this->environment->addExtension($this->twigExtension);
  68. // translation extension
  69. $translator = new Translator('en', new MessageSelector());
  70. $translator->addLoader('xlf', new XliffFileLoader());
  71. $translator->addResource('xlf', __DIR__.'/../../../Resources/translations/SonataAdminBundle.en.xliff', 'en', 'SonataAdminBundle');
  72. $this->environment->addExtension(new TranslationExtension($translator));
  73. // routing extension
  74. $xmlFileLoader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../../../Resources/config/routing')));
  75. $routeCollection = $xmlFileLoader->load('sonata_admin.xml');
  76. $requestContext = new RequestContext();
  77. $urlGenerator = new UrlGenerator($routeCollection, $requestContext);
  78. $this->environment->addExtension(new RoutingExtension($urlGenerator));
  79. $this->twigExtension->initRuntime($this->environment);
  80. // initialize object
  81. $this->object = new \stdClass();
  82. // initialize admin
  83. $this->admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  84. $this->admin->expects($this->any())
  85. ->method('isGranted')
  86. ->will($this->returnValue(true));
  87. $this->admin->expects($this->any())
  88. ->method('getCode')
  89. ->will($this->returnValue('xyz'));
  90. $this->admin->expects($this->any())
  91. ->method('id')
  92. ->with($this->equalTo($this->object))
  93. ->will($this->returnValue(12345));
  94. $this->admin->expects($this->any())
  95. ->method('trans')
  96. ->will($this->returnCallback(function($id) {
  97. return $id;
  98. }));
  99. // for php5.3 BC
  100. $admin = $this->admin;
  101. $container->expects($this->any())
  102. ->method('get')
  103. ->will($this->returnCallback(function($id) use ($admin) {
  104. if ($id == 'sonata_admin_foo_service') {
  105. return $admin;
  106. }
  107. return null;
  108. }));
  109. // initialize field description
  110. $this->fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  111. $this->fieldDescription->expects($this->any())
  112. ->method('getName')
  113. ->will($this->returnValue('fd_name'));
  114. $this->fieldDescription->expects($this->any())
  115. ->method('getAdmin')
  116. ->will($this->returnValue($this->admin));
  117. $this->fieldDescription->expects($this->any())
  118. ->method('getLabel')
  119. ->will($this->returnValue('Data'));
  120. }
  121. public function testSlugify()
  122. {
  123. $this->assertEquals($this->twigExtension->slugify('test'), 'test');
  124. $this->assertEquals($this->twigExtension->slugify('S§!@@#$#$alut'), 's-alut');
  125. $this->assertEquals($this->twigExtension->slugify('Symfony2'), 'symfony2');
  126. $this->assertEquals($this->twigExtension->slugify('test'), 'test');
  127. $this->assertEquals($this->twigExtension->slugify('c\'est bientôt l\'été'), 'c-est-bientot-l-ete');
  128. $this->assertEquals($this->twigExtension->slugify(urldecode('%2Fc\'est+bientôt+l\'été')), 'c-est-bientot-l-ete');
  129. }
  130. /**
  131. * @dataProvider getRenderListElementTests
  132. */
  133. public function testRenderListElement($expected, $type, $value, array $options)
  134. {
  135. $this->admin->expects($this->any())
  136. ->method('getTemplate')
  137. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
  138. $this->fieldDescription->expects($this->any())
  139. ->method('getValue')
  140. ->will($this->returnValue($value));
  141. $this->fieldDescription->expects($this->any())
  142. ->method('getType')
  143. ->will($this->returnValue($type));
  144. $this->fieldDescription->expects($this->any())
  145. ->method('getOptions')
  146. ->will($this->returnValue($options));
  147. $this->fieldDescription->expects($this->any())
  148. ->method('getTemplate')
  149. ->will($this->returnCallback(function() use ($type) {
  150. switch ($type) {
  151. case 'string':
  152. return 'SonataAdminBundle:CRUD:list_string.html.twig';
  153. case 'boolean':
  154. return 'SonataAdminBundle:CRUD:list_boolean.html.twig';
  155. case 'datetime':
  156. return 'SonataAdminBundle:CRUD:list_datetime.html.twig';
  157. case 'date':
  158. return 'SonataAdminBundle:CRUD:list_date.html.twig';
  159. case 'time':
  160. return 'SonataAdminBundle:CRUD:list_time.html.twig';
  161. case 'currency':
  162. return 'SonataAdminBundle:CRUD:list_currency.html.twig';
  163. case 'percent':
  164. return 'SonataAdminBundle:CRUD:list_percent.html.twig';
  165. case 'choice':
  166. return 'SonataAdminBundle:CRUD:list_choice.html.twig';
  167. case 'array':
  168. return 'SonataAdminBundle:CRUD:list_array.html.twig';
  169. case 'trans':
  170. return 'SonataAdminBundle:CRUD:list_trans.html.twig';
  171. case 'nonexistent':
  172. // template doesn`t exist
  173. return 'SonataAdminBundle:CRUD:list_nonexistent_template.html.twig';
  174. default:
  175. return false;
  176. }
  177. }));
  178. $this->assertEquals($expected, trim(preg_replace('/\s+/', ' ', $this->twigExtension->renderListElement($this->object, $this->fieldDescription))));
  179. }
  180. public function getRenderListElementTests()
  181. {
  182. return array(
  183. array('<td class="sonata-ba-list-field sonata-ba-list-field-string" objectId="12345"> Example </td>', 'string', 'Example', array()),
  184. array('<td class="sonata-ba-list-field sonata-ba-list-field-string" objectId="12345"> </td>', 'string', null, array()),
  185. array('<td class="sonata-ba-list-field sonata-ba-list-field-nonexistent" objectId="12345"> Example </td>', 'nonexistent', 'Example', array()),
  186. array('<td class="sonata-ba-list-field sonata-ba-list-field-nonexistent" objectId="12345"> </td>', 'nonexistent', null, array()),
  187. array('<td class="sonata-ba-list-field sonata-ba-list-field-text" objectId="12345"> Example </td>', 'text', 'Example', array()),
  188. array('<td class="sonata-ba-list-field sonata-ba-list-field-text" objectId="12345"> </td>', 'text', null, array()),
  189. array('<td class="sonata-ba-list-field sonata-ba-list-field-textarea" objectId="12345"> Example </td>', 'textarea', 'Example', array()),
  190. array('<td class="sonata-ba-list-field sonata-ba-list-field-textarea" objectId="12345"> </td>', 'textarea', null, array()),
  191. 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')), array()),
  192. array('<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> &nbsp; </td>', 'datetime', null, array()),
  193. array('<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> 24.12.2013 10:11:12 </td>', 'datetime', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array('format'=>'d.m.Y H:i:s')),
  194. array('<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> &nbsp; </td>', 'datetime', null, array('format'=>'d.m.Y H:i:s')),
  195. 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')), array()),
  196. array('<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> &nbsp; </td>', 'date', null, array()),
  197. array('<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> 24.12.2013 </td>', 'date', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array('format'=>'d.m.Y')),
  198. array('<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> &nbsp; </td>', 'date', null, array('format'=>'d.m.Y')),
  199. 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')), array()),
  200. array('<td class="sonata-ba-list-field sonata-ba-list-field-time" objectId="12345"> &nbsp; </td>', 'time', null, array()),
  201. array('<td class="sonata-ba-list-field sonata-ba-list-field-number" objectId="12345"> 10.746135 </td>', 'number', 10.746135, array()),
  202. array('<td class="sonata-ba-list-field sonata-ba-list-field-number" objectId="12345"> </td>', 'number', null, array()),
  203. array('<td class="sonata-ba-list-field sonata-ba-list-field-integer" objectId="12345"> 5678 </td>', 'integer', 5678, array()),
  204. array('<td class="sonata-ba-list-field sonata-ba-list-field-integer" objectId="12345"> </td>', 'integer', null, array()),
  205. array('<td class="sonata-ba-list-field sonata-ba-list-field-percent" objectId="12345"> 1074.6135 % </td>', 'percent', 10.746135, array()),
  206. array('<td class="sonata-ba-list-field sonata-ba-list-field-percent" objectId="12345"> 0 % </td>', 'percent', null, array()),
  207. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> EUR 10.746135 </td>', 'currency', 10.746135, array('currency' => 'EUR')),
  208. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> </td>', 'currency', null, array('currency' => 'EUR')),
  209. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> GBP 51.23456 </td>', 'currency', 51.23456, array('currency' => 'GBP')),
  210. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> </td>', 'currency', null, array('currency' => 'GBP')),
  211. 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'), array('safe' => false)),
  212. array('<td class="sonata-ba-list-field sonata-ba-list-field-array" objectId="12345"> </td>', 'array', null, array('safe' => false)),
  213. array('<td class="sonata-ba-list-field sonata-ba-list-field-boolean" objectId="12345"> <i class="icon-ok-circle"></i>&nbsp;yes </td>', 'boolean', true, array('editable'=>false)),
  214. array('<td class="sonata-ba-list-field sonata-ba-list-field-boolean" objectId="12345"> <i class="icon-ban-circle"></i>&nbsp;no </td>', 'boolean', false, array('editable'=>false)),
  215. array('<td class="sonata-ba-list-field sonata-ba-list-field-boolean" objectId="12345"> <i class="icon-ban-circle"></i>&nbsp;no </td>', 'boolean', null, array('editable'=>false)),
  216. array('<td class="sonata-ba-list-field sonata-ba-list-field-boolean" objectId="12345"> <a href="http://localhost/core/set-object-field-value?context=list&amp;field=fd_name&amp;objectId=12345&amp;value=0&amp;code=xyz" class="sonata-ba-action sonata-ba-edit-inline"><i class="icon-ok-circle"></i>&nbsp;yes</a> </td>', 'boolean', true, array('editable'=>true)),
  217. array('<td class="sonata-ba-list-field sonata-ba-list-field-boolean" objectId="12345"> <a href="http://localhost/core/set-object-field-value?context=list&amp;field=fd_name&amp;objectId=12345&amp;value=1&amp;code=xyz" class="sonata-ba-action sonata-ba-edit-inline"><i class="icon-ban-circle"></i>&nbsp;no</a> </td>', 'boolean', false, array('editable'=>true)),
  218. array('<td class="sonata-ba-list-field sonata-ba-list-field-boolean" objectId="12345"> <a href="http://localhost/core/set-object-field-value?context=list&amp;field=fd_name&amp;objectId=12345&amp;value=1&amp;code=xyz" class="sonata-ba-action sonata-ba-edit-inline"><i class="icon-ban-circle"></i>&nbsp;no</a> </td>', 'boolean', null, array('editable'=>true)),
  219. array('<td class="sonata-ba-list-field sonata-ba-list-field-trans" objectId="12345"> Delete </td>', 'trans', 'action_delete', array('safe'=>false, 'catalogue'=>'SonataAdminBundle')),
  220. array('<td class="sonata-ba-list-field sonata-ba-list-field-trans" objectId="12345"> </td>', 'trans', null, array('safe'=>false, 'catalogue'=>'SonataAdminBundle')),
  221. );
  222. }
  223. /**
  224. * @dataProvider getRenderViewElementTests
  225. */
  226. public function testRenderViewElement($expected, $type, $value, array $options)
  227. {
  228. $this->admin->expects($this->any())
  229. ->method('getTemplate')
  230. ->will($this->returnValue('SonataAdminBundle:CRUD:base_show_field.html.twig'));
  231. $this->fieldDescription->expects($this->any())
  232. ->method('getValue')
  233. ->will($this->returnCallback(function() use ($value) {
  234. if ($value instanceof NoValueException) {
  235. throw $value;
  236. }
  237. return $value;
  238. }));
  239. $this->fieldDescription->expects($this->any())
  240. ->method('getType')
  241. ->will($this->returnValue($type));
  242. $this->fieldDescription->expects($this->any())
  243. ->method('getOptions')
  244. ->will($this->returnValue($options));
  245. $this->fieldDescription->expects($this->any())
  246. ->method('getTemplate')
  247. ->will($this->returnCallback(function() use ($type) {
  248. switch ($type) {
  249. case 'boolean':
  250. return 'SonataAdminBundle:CRUD:show_boolean.html.twig';
  251. case 'datetime':
  252. return 'SonataAdminBundle:CRUD:show_datetime.html.twig';
  253. case 'date':
  254. return 'SonataAdminBundle:CRUD:show_date.html.twig';
  255. case 'time':
  256. return 'SonataAdminBundle:CRUD:show_time.html.twig';
  257. case 'currency':
  258. return 'SonataAdminBundle:CRUD:show_currency.html.twig';
  259. case 'percent':
  260. return 'SonataAdminBundle:CRUD:show_percent.html.twig';
  261. case 'choice':
  262. return 'SonataAdminBundle:CRUD:show_choice.html.twig';
  263. case 'array':
  264. return 'SonataAdminBundle:CRUD:show_array.html.twig';
  265. case 'trans':
  266. return 'SonataAdminBundle:CRUD:show_trans.html.twig';
  267. default:
  268. return false;
  269. }
  270. }));
  271. $this->assertEquals($expected, trim(preg_replace('/\s+/', ' ', $this->twigExtension->renderViewElement($this->fieldDescription, $this->object))));
  272. }
  273. public function getRenderViewElementTests()
  274. {
  275. return array(
  276. array('<th>Data</th> <td>Example</td>', 'string', 'Example', array('safe' => false)),
  277. array('<th>Data</th> <td>Example</td>', 'text', 'Example', array('safe' => false)),
  278. array('<th>Data</th> <td>Example</td>', 'textarea', 'Example', array('safe' => false)),
  279. array('<th>Data</th> <td>December 24, 2013 10:11</td>', 'datetime', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array()),
  280. array('<th>Data</th> <td>24.12.2013 10:11:12</td>', 'datetime', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array('format'=>'d.m.Y H:i:s')),
  281. array('<th>Data</th> <td>December 24, 2013</td>', 'date', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array()),
  282. array('<th>Data</th> <td>24.12.2013</td>', 'date', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array('format'=>'d.m.Y')),
  283. array('<th>Data</th> <td>10:11:12</td>', 'time', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array()),
  284. array('<th>Data</th> <td>10.746135</td>', 'number', 10.746135, array('safe' => false)),
  285. array('<th>Data</th> <td>5678</td>', 'integer', 5678, array('safe' => false)),
  286. array('<th>Data</th> <td> 1074.6135 % </td>', 'percent', 10.746135, array()),
  287. array('<th>Data</th> <td> EUR 10.746135 </td>', 'currency', 10.746135, array('currency' => 'EUR')),
  288. array('<th>Data</th> <td> GBP 51.23456 </td>', 'currency', 51.23456, array('currency' => 'GBP')),
  289. array('<th>Data</th> <td> [1 => First] [2 => Second] </td>', 'array', array(1 => 'First', 2 => 'Second'), array('safe' => false)),
  290. array('<th>Data</th> <td><i class="icon-ok-circle"></i>yes</td>', 'boolean', true, array()),
  291. array('<th>Data</th> <td><i class="icon-ban-circle"></i>no</td>', 'boolean', false, array()),
  292. array('<th>Data</th> <td> Delete </td>', 'trans', 'action_delete', array('safe'=>false, 'catalogue'=>'SonataAdminBundle')),
  293. // NoValueException
  294. array('<th>Data</th> <td></td>', 'string', new NoValueException(), array('safe' => false)),
  295. array('<th>Data</th> <td></td>', 'text', new NoValueException(), array('safe' => false)),
  296. array('<th>Data</th> <td></td>', 'textarea', new NoValueException(), array('safe' => false)),
  297. array('<th>Data</th> <td>&nbsp;</td>', 'datetime', new NoValueException(), array()),
  298. array('<th>Data</th> <td>&nbsp;</td>', 'datetime', new NoValueException(), array('format'=>'d.m.Y H:i:s')),
  299. array('<th>Data</th> <td>&nbsp;</td>', 'date', new NoValueException(), array()),
  300. array('<th>Data</th> <td>&nbsp;</td>', 'date', new NoValueException(), array('format'=>'d.m.Y')),
  301. array('<th>Data</th> <td>&nbsp;</td>', 'time', new NoValueException(), array()),
  302. array('<th>Data</th> <td></td>', 'number', new NoValueException(), array('safe' => false)),
  303. array('<th>Data</th> <td></td>', 'integer', new NoValueException(), array('safe' => false)),
  304. array('<th>Data</th> <td> 0 % </td>', 'percent', new NoValueException(), array()),
  305. array('<th>Data</th> <td> </td>', 'currency', new NoValueException(), array('currency' => 'EUR')),
  306. array('<th>Data</th> <td> </td>', 'currency', new NoValueException(), array('currency' => 'GBP')),
  307. array('<th>Data</th> <td> </td>', 'array', new NoValueException(), array('safe' => false)),
  308. array('<th>Data</th> <td><i class="icon-ban-circle"></i>no</td>', 'boolean', new NoValueException(), array()),
  309. array('<th>Data</th> <td> </td>', 'trans', new NoValueException(), array('safe'=>false, 'catalogue'=>'SonataAdminBundle')),
  310. );
  311. }
  312. public function testGetValueFromFieldDescription()
  313. {
  314. $object = new \stdClass();
  315. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  316. $fieldDescription->expects($this->any())
  317. ->method('getValue')
  318. ->will($this->returnValue('test123'));
  319. $this->assertEquals('test123', $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
  320. }
  321. public function testGetValueFromFieldDescriptionWithRemoveLoopException()
  322. {
  323. $object = $this->getMock('\ArrayAccess');
  324. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  325. try {
  326. $this->assertEquals('anything', $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription, array('loop'=>true)));
  327. } catch (\RuntimeException $e) {
  328. $this->assertContains('remove the loop requirement', $e->getMessage());
  329. return;
  330. }
  331. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  332. }
  333. public function testGetValueFromFieldDescriptionWithNoValueException()
  334. {
  335. $object = new \stdClass();
  336. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  337. $fieldDescription->expects($this->any())
  338. ->method('getValue')
  339. ->will($this->returnCallback(function() {
  340. throw new NoValueException();
  341. }));
  342. $fieldDescription->expects($this->any())
  343. ->method('getAssociationAdmin')
  344. ->will($this->returnValue(null));
  345. $this->assertEquals(null, $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
  346. }
  347. public function testGetValueFromFieldDescriptionWithNoValueExceptionNewAdminInstance()
  348. {
  349. $object = new \stdClass();
  350. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  351. $fieldDescription->expects($this->any())
  352. ->method('getValue')
  353. ->will($this->returnCallback(function() {
  354. throw new NoValueException();
  355. }));
  356. $fieldDescription->expects($this->any())
  357. ->method('getAssociationAdmin')
  358. ->will($this->returnValue($this->admin));
  359. $this->admin->expects($this->once())
  360. ->method('getNewInstance')
  361. ->will($this->returnValue('foo'));
  362. $this->assertEquals('foo', $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
  363. }
  364. public function testOutput()
  365. {
  366. $this->fieldDescription->expects($this->any())
  367. ->method('getTemplate')
  368. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
  369. $this->fieldDescription->expects($this->any())
  370. ->method('getFieldName')
  371. ->will($this->returnValue('fd_name'));
  372. $this->environment->disableDebug();
  373. $parameters = array(
  374. 'admin' => $this->admin,
  375. 'value' => 'foo',
  376. 'field_description' => $this->fieldDescription,
  377. 'object' => $this->object,
  378. );
  379. $template = $this->environment->loadTemplate('SonataAdminBundle:CRUD:base_list_field.html.twig');
  380. $this->assertEquals('<td class="sonata-ba-list-field sonata-ba-list-field-" objectId="12345"> foo </td>',
  381. trim(preg_replace('/\s+/', ' ', $this->twigExtension->output($this->fieldDescription, $template, $parameters))));
  382. $this->environment->enableDebug();
  383. $this->assertEquals('<!-- START fieldName: fd_name template: SonataAdminBundle:CRUD:base_list_field.html.twig compiled template: SonataAdminBundle:CRUD:base_list_field.html.twig --> <td class="sonata-ba-list-field sonata-ba-list-field-" objectId="12345"> foo </td> <!-- END - fieldName: fd_name -->',
  384. trim(preg_replace('/\s+/', ' ', $this->twigExtension->output($this->fieldDescription, $template, $parameters))));
  385. }
  386. public function testRenderRelationElementNoObject()
  387. {
  388. $this->assertEquals('foo', $this->twigExtension->renderRelationElement('foo', $this->fieldDescription));
  389. }
  390. public function testRenderRelationElementToString()
  391. {
  392. $this->fieldDescription->expects($this->once())
  393. ->method('getOption')
  394. ->with($this->identicalTo('associated_tostring'))
  395. ->will($this->returnValue('__toString'));
  396. $element = $this->getMock('stdClass', array('__toString'));
  397. $element->expects($this->any())
  398. ->method('__toString')
  399. ->will($this->returnValue('bar'));
  400. $this->assertEquals('bar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
  401. }
  402. public function testRenderRelationElementCustomToString()
  403. {
  404. $this->fieldDescription->expects($this->any())
  405. ->method('getOption')
  406. ->with($this->identicalTo('associated_tostring'))
  407. ->will($this->returnValue('customToString'));
  408. $element = $this->getMock('stdClass', array('customToString'));
  409. $element->expects($this->any())
  410. ->method('customToString')
  411. ->will($this->returnValue('fooBar'));
  412. $this->assertEquals('fooBar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
  413. }
  414. public function testRenderRelationElementMethodNotExist()
  415. {
  416. $this->fieldDescription->expects($this->any())
  417. ->method('getOption')
  418. ->with($this->identicalTo('associated_tostring'))
  419. ->will($this->returnValue('nonExistedMethod'));
  420. $element = new \stdClass();
  421. try {
  422. $this->twigExtension->renderRelationElement($element, $this->fieldDescription);
  423. } catch (\RuntimeException $e) {
  424. $this->assertContains('You must define an `associated_tostring` option or create a `stdClass::__toString` method to the field option "fd_name" from service "xyz".', $e->getMessage());
  425. return;
  426. }
  427. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  428. }
  429. public function testGetUrlsafeIdentifier()
  430. {
  431. $enitity = new \stdClass();
  432. // set admin to pool
  433. $this->pool->setAdminClasses(array('stdClass'=>'sonata_admin_foo_service'));
  434. $this->admin->expects($this->once())
  435. ->method('getUrlsafeIdentifier')
  436. ->with($this->equalTo($enitity))
  437. ->will($this->returnValue(1234567));
  438. $this->assertEquals(1234567, $this->twigExtension->getUrlsafeIdentifier($enitity));
  439. }
  440. }