SonataAdminExtensionTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 'array':
  166. return 'SonataAdminBundle:CRUD:list_array.html.twig';
  167. case 'trans':
  168. return 'SonataAdminBundle:CRUD:list_trans.html.twig';
  169. case 'nonexistent':
  170. // template doesn`t exist
  171. return 'SonataAdminBundle:CRUD:list_nonexistent_template.html.twig';
  172. default:
  173. return false;
  174. }
  175. }));
  176. $this->assertEquals($expected, trim(preg_replace('/\s+/', ' ', $this->twigExtension->renderListElement($this->object, $this->fieldDescription))));
  177. }
  178. public function getRenderListElementTests()
  179. {
  180. return array(
  181. array('<td class="sonata-ba-list-field sonata-ba-list-field-string" objectId="12345"> Example </td>', 'string', 'Example', array()),
  182. array('<td class="sonata-ba-list-field sonata-ba-list-field-string" objectId="12345"> </td>', 'string', null, array()),
  183. array('<td class="sonata-ba-list-field sonata-ba-list-field-nonexistent" objectId="12345"> Example </td>', 'nonexistent', 'Example', array()),
  184. array('<td class="sonata-ba-list-field sonata-ba-list-field-nonexistent" objectId="12345"> </td>', 'nonexistent', null, array()),
  185. array('<td class="sonata-ba-list-field sonata-ba-list-field-text" objectId="12345"> Example </td>', 'text', 'Example', array()),
  186. array('<td class="sonata-ba-list-field sonata-ba-list-field-text" objectId="12345"> </td>', 'text', null, array()),
  187. array('<td class="sonata-ba-list-field sonata-ba-list-field-textarea" objectId="12345"> Example </td>', 'textarea', 'Example', array()),
  188. array('<td class="sonata-ba-list-field sonata-ba-list-field-textarea" objectId="12345"> </td>', 'textarea', null, array()),
  189. 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()),
  190. array('<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> &nbsp; </td>', 'datetime', null, array()),
  191. 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')),
  192. 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')),
  193. 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()),
  194. array('<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> &nbsp; </td>', 'date', null, array()),
  195. 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')),
  196. array('<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> &nbsp; </td>', 'date', null, array('format'=>'d.m.Y')),
  197. 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()),
  198. array('<td class="sonata-ba-list-field sonata-ba-list-field-time" objectId="12345"> &nbsp; </td>', 'time', null, array()),
  199. array('<td class="sonata-ba-list-field sonata-ba-list-field-number" objectId="12345"> 10.746135 </td>', 'number', 10.746135, array()),
  200. array('<td class="sonata-ba-list-field sonata-ba-list-field-number" objectId="12345"> </td>', 'number', null, array()),
  201. array('<td class="sonata-ba-list-field sonata-ba-list-field-integer" objectId="12345"> 5678 </td>', 'integer', 5678, array()),
  202. array('<td class="sonata-ba-list-field sonata-ba-list-field-integer" objectId="12345"> </td>', 'integer', null, array()),
  203. array('<td class="sonata-ba-list-field sonata-ba-list-field-percent" objectId="12345"> 1074.6135 % </td>', 'percent', 10.746135, array()),
  204. array('<td class="sonata-ba-list-field sonata-ba-list-field-percent" objectId="12345"> 0 % </td>', 'percent', null, array()),
  205. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> EUR 10.746135 </td>', 'currency', 10.746135, array('currency' => 'EUR')),
  206. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> </td>', 'currency', null, array('currency' => 'EUR')),
  207. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> GBP 51.23456 </td>', 'currency', 51.23456, array('currency' => 'GBP')),
  208. array('<td class="sonata-ba-list-field sonata-ba-list-field-currency" objectId="12345"> </td>', 'currency', null, array('currency' => 'GBP')),
  209. 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)),
  210. array('<td class="sonata-ba-list-field sonata-ba-list-field-array" objectId="12345"> </td>', 'array', null, array('safe' => false)),
  211. 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)),
  212. 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)),
  213. 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)),
  214. 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)),
  215. 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)),
  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=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)),
  217. array('<td class="sonata-ba-list-field sonata-ba-list-field-trans" objectId="12345"> Delete </td>', 'trans', 'action_delete', array('safe'=>false, 'catalogue'=>'SonataAdminBundle')),
  218. array('<td class="sonata-ba-list-field sonata-ba-list-field-trans" objectId="12345"> </td>', 'trans', null, array('safe'=>false, 'catalogue'=>'SonataAdminBundle')),
  219. );
  220. }
  221. /**
  222. * @dataProvider getRenderViewElementTests
  223. */
  224. public function testRenderViewElement($expected, $type, $value, array $options)
  225. {
  226. $this->admin->expects($this->any())
  227. ->method('getTemplate')
  228. ->will($this->returnValue('SonataAdminBundle:CRUD:base_show_field.html.twig'));
  229. $this->fieldDescription->expects($this->any())
  230. ->method('getValue')
  231. ->will($this->returnCallback(function() use ($value) {
  232. if ($value instanceof NoValueException) {
  233. throw $value;
  234. }
  235. return $value;
  236. }));
  237. $this->fieldDescription->expects($this->any())
  238. ->method('getType')
  239. ->will($this->returnValue($type));
  240. $this->fieldDescription->expects($this->any())
  241. ->method('getOptions')
  242. ->will($this->returnValue($options));
  243. $this->fieldDescription->expects($this->any())
  244. ->method('getTemplate')
  245. ->will($this->returnCallback(function() use ($type) {
  246. switch ($type) {
  247. case 'boolean':
  248. return 'SonataAdminBundle:CRUD:show_boolean.html.twig';
  249. case 'datetime':
  250. return 'SonataAdminBundle:CRUD:show_datetime.html.twig';
  251. case 'date':
  252. return 'SonataAdminBundle:CRUD:show_date.html.twig';
  253. case 'time':
  254. return 'SonataAdminBundle:CRUD:show_time.html.twig';
  255. case 'currency':
  256. return 'SonataAdminBundle:CRUD:show_currency.html.twig';
  257. case 'percent':
  258. return 'SonataAdminBundle:CRUD:show_percent.html.twig';
  259. case 'array':
  260. return 'SonataAdminBundle:CRUD:show_array.html.twig';
  261. case 'trans':
  262. return 'SonataAdminBundle:CRUD:show_trans.html.twig';
  263. default:
  264. return false;
  265. }
  266. }));
  267. $this->assertEquals($expected, trim(preg_replace('/\s+/', ' ', $this->twigExtension->renderViewElement($this->fieldDescription, $this->object))));
  268. }
  269. public function getRenderViewElementTests()
  270. {
  271. return array(
  272. array('<th>Data</th> <td>Example</td>', 'string', 'Example', array('safe' => false)),
  273. array('<th>Data</th> <td>Example</td>', 'text', 'Example', array('safe' => false)),
  274. array('<th>Data</th> <td>Example</td>', 'textarea', 'Example', array('safe' => false)),
  275. 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()),
  276. 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')),
  277. array('<th>Data</th> <td>December 24, 2013</td>', 'date', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array()),
  278. 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')),
  279. array('<th>Data</th> <td>10:11:12</td>', 'time', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array()),
  280. array('<th>Data</th> <td>10.746135</td>', 'number', 10.746135, array('safe' => false)),
  281. array('<th>Data</th> <td>5678</td>', 'integer', 5678, array('safe' => false)),
  282. array('<th>Data</th> <td> 1074.6135 % </td>', 'percent', 10.746135, array()),
  283. array('<th>Data</th> <td> EUR 10.746135 </td>', 'currency', 10.746135, array('currency' => 'EUR')),
  284. array('<th>Data</th> <td> GBP 51.23456 </td>', 'currency', 51.23456, array('currency' => 'GBP')),
  285. array('<th>Data</th> <td> [1 => First] [2 => Second] </td>', 'array', array(1 => 'First', 2 => 'Second'), array('safe' => false)),
  286. array('<th>Data</th> <td><i class="icon-ok-circle"></i>yes</td>', 'boolean', true, array()),
  287. array('<th>Data</th> <td><i class="icon-ban-circle"></i>no</td>', 'boolean', false, array()),
  288. array('<th>Data</th> <td> Delete </td>', 'trans', 'action_delete', array('safe'=>false, 'catalogue'=>'SonataAdminBundle')),
  289. // NoValueException
  290. array('<th>Data</th> <td></td>', 'string', new NoValueException(), array('safe' => false)),
  291. array('<th>Data</th> <td></td>', 'text', new NoValueException(), array('safe' => false)),
  292. array('<th>Data</th> <td></td>', 'textarea', new NoValueException(), array('safe' => false)),
  293. array('<th>Data</th> <td>&nbsp;</td>', 'datetime', new NoValueException(), array()),
  294. array('<th>Data</th> <td>&nbsp;</td>', 'datetime', new NoValueException(), array('format'=>'d.m.Y H:i:s')),
  295. array('<th>Data</th> <td>&nbsp;</td>', 'date', new NoValueException(), array()),
  296. array('<th>Data</th> <td>&nbsp;</td>', 'date', new NoValueException(), array('format'=>'d.m.Y')),
  297. array('<th>Data</th> <td>&nbsp;</td>', 'time', new NoValueException(), array()),
  298. array('<th>Data</th> <td></td>', 'number', new NoValueException(), array('safe' => false)),
  299. array('<th>Data</th> <td></td>', 'integer', new NoValueException(), array('safe' => false)),
  300. array('<th>Data</th> <td> 0 % </td>', 'percent', new NoValueException(), array()),
  301. array('<th>Data</th> <td> </td>', 'currency', new NoValueException(), array('currency' => 'EUR')),
  302. array('<th>Data</th> <td> </td>', 'currency', new NoValueException(), array('currency' => 'GBP')),
  303. array('<th>Data</th> <td> </td>', 'array', new NoValueException(), array('safe' => false)),
  304. array('<th>Data</th> <td><i class="icon-ban-circle"></i>no</td>', 'boolean', new NoValueException(), array()),
  305. array('<th>Data</th> <td> </td>', 'trans', new NoValueException(), array('safe'=>false, 'catalogue'=>'SonataAdminBundle')),
  306. );
  307. }
  308. public function testGetValueFromFieldDescription()
  309. {
  310. $object = new \stdClass();
  311. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  312. $fieldDescription->expects($this->any())
  313. ->method('getValue')
  314. ->will($this->returnValue('test123'));
  315. $this->assertEquals('test123', $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
  316. }
  317. public function testGetValueFromFieldDescriptionWithRemoveLoopException()
  318. {
  319. $object = $this->getMock('\ArrayAccess');
  320. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  321. try {
  322. $this->assertEquals('anything', $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription, array('loop'=>true)));
  323. } catch (\RuntimeException $e) {
  324. $this->assertContains('remove the loop requirement', $e->getMessage());
  325. return;
  326. }
  327. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  328. }
  329. public function testGetValueFromFieldDescriptionWithNoValueException()
  330. {
  331. $object = new \stdClass();
  332. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  333. $fieldDescription->expects($this->any())
  334. ->method('getValue')
  335. ->will($this->returnCallback(function() {
  336. throw new NoValueException();
  337. }));
  338. $fieldDescription->expects($this->any())
  339. ->method('getAssociationAdmin')
  340. ->will($this->returnValue(null));
  341. $this->assertEquals(null, $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
  342. }
  343. public function testGetValueFromFieldDescriptionWithNoValueExceptionNewAdminInstance()
  344. {
  345. $object = new \stdClass();
  346. $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  347. $fieldDescription->expects($this->any())
  348. ->method('getValue')
  349. ->will($this->returnCallback(function() {
  350. throw new NoValueException();
  351. }));
  352. $fieldDescription->expects($this->any())
  353. ->method('getAssociationAdmin')
  354. ->will($this->returnValue($this->admin));
  355. $this->admin->expects($this->once())
  356. ->method('getNewInstance')
  357. ->will($this->returnValue('foo'));
  358. $this->assertEquals('foo', $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
  359. }
  360. public function testOutput()
  361. {
  362. $this->fieldDescription->expects($this->any())
  363. ->method('getTemplate')
  364. ->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig'));
  365. $this->fieldDescription->expects($this->any())
  366. ->method('getFieldName')
  367. ->will($this->returnValue('fd_name'));
  368. $this->environment->disableDebug();
  369. $parameters = array(
  370. 'admin' => $this->admin,
  371. 'value' => 'foo',
  372. 'field_description' => $this->fieldDescription,
  373. 'object' => $this->object,
  374. );
  375. $template = $this->environment->loadTemplate('SonataAdminBundle:CRUD:base_list_field.html.twig');
  376. $this->assertEquals('<td class="sonata-ba-list-field sonata-ba-list-field-" objectId="12345"> foo </td>',
  377. trim(preg_replace('/\s+/', ' ', $this->twigExtension->output($this->fieldDescription, $template, $parameters))));
  378. $this->environment->enableDebug();
  379. $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 -->',
  380. trim(preg_replace('/\s+/', ' ', $this->twigExtension->output($this->fieldDescription, $template, $parameters))));
  381. }
  382. public function testRenderRelationElementNoObject()
  383. {
  384. $this->assertEquals('foo', $this->twigExtension->renderRelationElement('foo', $this->fieldDescription));
  385. }
  386. public function testRenderRelationElementToString()
  387. {
  388. $this->fieldDescription->expects($this->once())
  389. ->method('getOption')
  390. ->with($this->identicalTo('associated_tostring'))
  391. ->will($this->returnValue('__toString'));
  392. $element = $this->getMock('stdClass', array('__toString'));
  393. $element->expects($this->any())
  394. ->method('__toString')
  395. ->will($this->returnValue('bar'));
  396. $this->assertEquals('bar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
  397. }
  398. public function testRenderRelationElementCustomToString()
  399. {
  400. $this->fieldDescription->expects($this->any())
  401. ->method('getOption')
  402. ->with($this->identicalTo('associated_tostring'))
  403. ->will($this->returnValue('customToString'));
  404. $element = $this->getMock('stdClass', array('customToString'));
  405. $element->expects($this->any())
  406. ->method('customToString')
  407. ->will($this->returnValue('fooBar'));
  408. $this->assertEquals('fooBar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
  409. }
  410. public function testRenderRelationElementMethodNotExist()
  411. {
  412. $this->fieldDescription->expects($this->any())
  413. ->method('getOption')
  414. ->with($this->identicalTo('associated_tostring'))
  415. ->will($this->returnValue('nonExistedMethod'));
  416. $element = new \stdClass();
  417. try {
  418. $this->twigExtension->renderRelationElement($element, $this->fieldDescription);
  419. } catch (\RuntimeException $e) {
  420. $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());
  421. return;
  422. }
  423. $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
  424. }
  425. public function testGetUrlsafeIdentifier()
  426. {
  427. $enitity = new \stdClass();
  428. // set admin to pool
  429. $this->pool->setAdminClasses(array('stdClass'=>'sonata_admin_foo_service'));
  430. $this->admin->expects($this->once())
  431. ->method('getUrlsafeIdentifier')
  432. ->with($this->equalTo($enitity))
  433. ->will($this->returnValue(1234567));
  434. $this->assertEquals(1234567, $this->twigExtension->getUrlsafeIdentifier($enitity));
  435. }
  436. }