ServicesManipulatorTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 }
  52. public: true\n",
  53. file_get_contents($this->file)
  54. );
  55. $this->servicesManipulator->addResource(
  56. 'another_service_id',
  57. 'another_class',
  58. 'another_admin_class',
  59. 'another_controller_name',
  60. 'another_manager_type'
  61. );
  62. $this->assertSame(
  63. "services:
  64. service_id:
  65. class: admin_class
  66. arguments: [~, class, controller_name]
  67. tags:
  68. - { name: sonata.admin, manager_type: manager_type, group: admin, label: class }
  69. public: true
  70. another_service_id:
  71. class: another_admin_class
  72. arguments: [~, another_class, another_controller_name]
  73. tags:
  74. - { name: sonata.admin, manager_type: another_manager_type, group: admin, label: another_class }
  75. public: true\n",
  76. file_get_contents($this->file)
  77. );
  78. }
  79. /**
  80. * @expectedException \RuntimeException
  81. * @expectedExceptionMessage The service "service_id" is already defined
  82. */
  83. public function testAddResourceShouldThrowException()
  84. {
  85. $this->servicesManipulator->addResource(
  86. 'service_id',
  87. 'class',
  88. 'admin_class',
  89. 'controller_name',
  90. 'manager_type'
  91. );
  92. $this->servicesManipulator->addResource(
  93. 'service_id',
  94. 'class',
  95. 'admin_class',
  96. 'controller_name',
  97. 'manager_type'
  98. );
  99. }
  100. public function testAddResourceWithEmptyServices()
  101. {
  102. file_put_contents($this->file, 'services:');
  103. $this->servicesManipulator->addResource(
  104. 'service_id',
  105. 'class',
  106. 'admin_class',
  107. 'controller_name',
  108. 'manager_type'
  109. );
  110. $this->assertSame(
  111. "services:
  112. service_id:
  113. class: admin_class
  114. arguments: [~, class, controller_name]
  115. tags:
  116. - { name: sonata.admin, manager_type: manager_type, group: admin, label: class }
  117. public: true\n",
  118. file_get_contents($this->file)
  119. );
  120. }
  121. }