CommentParserTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Test\Unit\DeploymentConfig;
  7. use Magento\Framework\App\DeploymentConfig\CommentParser;
  8. use Magento\Framework\Filesystem;
  9. use Magento\Framework\App\Filesystem\DirectoryList;
  10. use Magento\Framework\Config\File\ConfigFilePool;
  11. use Magento\Framework\Filesystem\Directory\ReadInterface;
  12. class CommentParserTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var Filesystem|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $filesystemMock;
  18. /**
  19. * @var ConfigFilePool|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $configFilePoolMock;
  22. /**
  23. * @var ReadInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $readDirectoryMock;
  26. /**
  27. * @var CommentParser
  28. */
  29. private $commentParser;
  30. /**
  31. * {@inheritdoc}
  32. */
  33. protected function setUp()
  34. {
  35. $this->configFilePoolMock = $this->getMockBuilder(ConfigFilePool::class)
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->readDirectoryMock = $this->getMockBuilder(ReadInterface::class)
  39. ->getMockForAbstractClass();
  40. $this->filesystemMock = $this->getMockBuilder(Filesystem::class)
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $this->filesystemMock->expects($this->once())
  44. ->method('getDirectoryRead')
  45. ->with(DirectoryList::CONFIG)
  46. ->willReturn($this->readDirectoryMock);
  47. $this->commentParser = new CommentParser($this->filesystemMock, $this->configFilePoolMock);
  48. }
  49. public function testExecuteFileDoesNotExist()
  50. {
  51. $file = 'config.php';
  52. $expectedResult = [];
  53. $this->readDirectoryMock->expects($this->once())
  54. ->method('isExist')
  55. ->with($file)
  56. ->willReturn(false);
  57. $this->assertSame($expectedResult, $this->commentParser->execute($file));
  58. }
  59. public function testExecute()
  60. {
  61. $file = 'config.php';
  62. $content = <<<TEXT
  63. <?php
  64. return array (
  65. 'ns1' =>
  66. array (
  67. 's1' =>
  68. array (
  69. 0 => 's11',
  70. 1 => 's12',
  71. ),
  72. 's2' =>
  73. array (
  74. 0 => 's21',
  75. 1 => 's22',
  76. ),
  77. ),
  78. /**
  79. * comment for namespace 2.
  80. * Next comment for' namespace 2
  81. */
  82. 'ns2' =>
  83. array (
  84. 's1' =>
  85. array (
  86. 0 => 's11',
  87. ),
  88. ),
  89. // This comment will be ignored
  90. 'ns3' => 'just text',
  91. /**
  92. * comment for namespace 4
  93. * second line
  94. * For the section: ns4
  95. */
  96. 'ns4' => 'just text',
  97. /**
  98. * For the section: ns5
  99. * *comment for namespace *5*
  100. */
  101. 'ns5' => 'just text',
  102. # This comment will be ignored
  103. 'ns6' => 'just text',
  104. );
  105. TEXT;
  106. $expectedResult = [
  107. 'ns4' => "comment for namespace 4\n second line",
  108. 'ns5' => '*comment for namespace *5*',
  109. ];
  110. $this->readDirectoryMock->expects($this->once())
  111. ->method('isExist')
  112. ->with($file)
  113. ->willReturn(true);
  114. $this->readDirectoryMock->expects($this->once())
  115. ->method('readFile')
  116. ->with($file)
  117. ->willReturn($content);
  118. $this->assertEquals($expectedResult, $this->commentParser->execute($file));
  119. }
  120. }