BaseAdminModelManagerTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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\AbstractAdmin;
  12. use Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
  13. class BaseAdminModelManager_Admin extends AbstractAdmin
  14. {
  15. }
  16. class BaseAdminModelManagerTest extends PHPUnit_Framework_TestCase
  17. {
  18. public function testHook()
  19. {
  20. $securityHandler = $this->getMockForAbstractClass('Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface');
  21. $modelManager = $this->getMockForAbstractClass('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 BaseAdminModelManager_Admin('code', 'class', 'controller');
  26. $admin->setModelManager($modelManager);
  27. $admin->setSecurityHandler($securityHandler);
  28. $t = new \stdClass();
  29. $admin->update($t);
  30. $admin->create($t);
  31. $admin->delete($t);
  32. }
  33. public function testObject()
  34. {
  35. $modelManager = $this->getMockForAbstractClass('Sonata\AdminBundle\Model\ModelManagerInterface');
  36. $modelManager->expects($this->once())->method('find')->will($this->returnCallback(function ($class, $id) {
  37. if ($class != 'class') {
  38. throw new \RuntimeException('Invalid class argument');
  39. }
  40. if ($id != 10) {
  41. throw new \RuntimeException('Invalid id argument');
  42. }
  43. }));
  44. $admin = new BaseAdminModelManager_Admin('code', 'class', 'controller');
  45. $admin->setModelManager($modelManager);
  46. $admin->getObject(10);
  47. }
  48. public function testCreateQuery()
  49. {
  50. $modelManager = $this->getMockForAbstractClass('Sonata\AdminBundle\Model\ModelManagerInterface');
  51. $modelManager->expects($this->once())->method('createQuery')->will($this->returnCallback(function ($class) {
  52. if ($class != 'class') {
  53. throw new \RuntimeException('Invalid class argument');
  54. }
  55. }));
  56. $admin = new BaseAdminModelManager_Admin('code', 'class', 'controller');
  57. $admin->setModelManager($modelManager);
  58. $admin->createQuery();
  59. }
  60. public function testId()
  61. {
  62. $modelManager = $this->getMockForAbstractClass('Sonata\AdminBundle\Model\ModelManagerInterface');
  63. $modelManager->expects($this->exactly(2))->method('getNormalizedIdentifier');
  64. $admin = new BaseAdminModelManager_Admin('code', 'class', 'controller');
  65. $admin->setModelManager($modelManager);
  66. $admin->id('Entity');
  67. $admin->getNormalizedIdentifier('Entity');
  68. }
  69. }