ServicesManipulatorTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\Generator;
  11. use Sonata\AdminBundle\Manipulator\ServicesManipulator;
  12. /**
  13. * @author Marek Stipek <mario.dweller@seznam.cz>
  14. */
  15. class ServicesManipulatorTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /** @var ServicesManipulator */
  18. private $servicesManipulator;
  19. /** @var string */
  20. private $file;
  21. /**
  22. * {@inheritdoc}
  23. */
  24. protected function setUp()
  25. {
  26. $this->file = sprintf('%s/%s.yml', sys_get_temp_dir(), lcg_value());
  27. $this->servicesManipulator = new ServicesManipulator($this->file);
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. protected function tearDown()
  33. {
  34. @unlink($this->file);
  35. }
  36. public function testAddResource()
  37. {
  38. $this->servicesManipulator->addResource(
  39. 'service_id',
  40. 'class',
  41. 'admin_class',
  42. 'controller_name',
  43. 'manager_type'
  44. );
  45. $this->assertSame(
  46. "services:
  47. service_id:
  48. class: admin_class
  49. arguments: [~, class, controller_name]
  50. tags:
  51. - {name: sonata.admin, manager_type: manager_type, group: admin, label: class}\n",
  52. file_get_contents($this->file)
  53. );
  54. $this->servicesManipulator->addResource(
  55. 'another_service_id',
  56. 'another_class',
  57. 'another_admin_class',
  58. 'another_controller_name',
  59. 'another_manager_type'
  60. );
  61. $this->assertSame(
  62. "services:
  63. service_id:
  64. class: admin_class
  65. arguments: [~, class, controller_name]
  66. tags:
  67. - {name: sonata.admin, manager_type: manager_type, group: admin, label: class}
  68. another_service_id:
  69. class: another_admin_class
  70. arguments: [~, another_class, another_controller_name]
  71. tags:
  72. - {name: sonata.admin, manager_type: another_manager_type, group: admin, label: another_class}\n",
  73. file_get_contents($this->file)
  74. );
  75. }
  76. /**
  77. * @expectedException \RuntimeException
  78. * @expectedExceptionMessage The service "service_id" is already defined
  79. */
  80. public function testAddResourceShouldThrowException()
  81. {
  82. $this->servicesManipulator->addResource(
  83. 'service_id',
  84. 'class',
  85. 'admin_class',
  86. 'controller_name',
  87. 'manager_type'
  88. );
  89. $this->servicesManipulator->addResource(
  90. 'service_id',
  91. 'class',
  92. 'admin_class',
  93. 'controller_name',
  94. 'manager_type'
  95. );
  96. }
  97. public function testAddResourceWithEmptyServices()
  98. {
  99. file_put_contents($this->file, 'services:');
  100. $this->servicesManipulator->addResource(
  101. 'service_id',
  102. 'class',
  103. 'admin_class',
  104. 'controller_name',
  105. 'manager_type'
  106. );
  107. $this->assertSame(
  108. "services:
  109. service_id:
  110. class: admin_class
  111. arguments: [~, class, controller_name]
  112. tags:
  113. - {name: sonata.admin, manager_type: manager_type, group: admin, label: class}\n",
  114. file_get_contents($this->file)
  115. );
  116. }
  117. }