BaseAdminModelManagerTest.php 3.0 KB

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