BaseAdminModelManagerTest.php 2.9 KB

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