BaseAdminModelManagerTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\Admin;
  11. use Sonata\AdminBundle\Admin\Admin;
  12. use Sonata\AdminBundle\Route\RouteCollection;
  13. use Sonata\AdminBundle\Model\ModelManagerInterface;
  14. class Admin_BaseAdminModelManagerTest extends Admin
  15. {
  16. }
  17. class BaseAdminModelManagerTest extends \PHPUnit_Framework_TestCase
  18. {
  19. public function testHook()
  20. {
  21. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  22. $modelManager->expects($this->once())->method('create');
  23. $modelManager->expects($this->once())->method('update');
  24. $modelManager->expects($this->once())->method('delete');
  25. $admin = new Admin_BaseAdminModelManagerTest('code', 'class', 'controller');
  26. $admin->setModelManager($modelManager);
  27. $t = new \stdClass();
  28. $admin->update($t);
  29. $admin->create($t);
  30. $admin->delete($t);
  31. }
  32. public function testObject()
  33. {
  34. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  35. $modelManager->expects($this->once())->method('find')->will($this->returnCallback(function($class, $id) {
  36. if ($class != 'class') {
  37. throw new \RuntimeException('Invalid class argument');
  38. }
  39. if ($id != 10) {
  40. throw new \RuntimeException('Invalid id argument');
  41. }
  42. }));
  43. $admin = new Admin_BaseAdminModelManagerTest('code', 'class', 'controller');
  44. $admin->setModelManager($modelManager);
  45. $admin->getObject(10);
  46. }
  47. public function testCreateQuery()
  48. {
  49. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  50. $modelManager->expects($this->once())->method('createQuery')->will($this->returnCallback(function($class) {
  51. if ($class != 'class') {
  52. throw new \RuntimeException('Invalid class argument');
  53. }
  54. }));
  55. $admin = new Admin_BaseAdminModelManagerTest('code', 'class', 'controller');
  56. $admin->setModelManager($modelManager);
  57. $admin->createQuery();
  58. }
  59. public function testId()
  60. {
  61. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  62. $modelManager->expects($this->exactly(2))->method('getNormalizedIdentifier');
  63. $admin = new Admin_BaseAdminModelManagerTest('code', 'class', 'controller');
  64. $admin->setModelManager($modelManager);
  65. $admin->id('Entity');
  66. $admin->getNormalizedIdentifier('Entity');
  67. }
  68. }