GeneratedFilesTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Code\Test\Unit;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Code\GeneratedFiles;
  9. class GeneratedFilesTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\Framework\App\Filesystem\DirectoryList | \PHPUnit_Framework_MockObject_MockObject
  13. */
  14. private $directoryList;
  15. /**
  16. * @var \Magento\Framework\Filesystem\Directory\WriteInterface | \PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $writeInterface;
  19. /**
  20. * @var \Magento\Framework\Code\GeneratedFiles
  21. */
  22. private $model;
  23. protected function setUp()
  24. {
  25. $this->directoryList =
  26. $this->createPartialMock(\Magento\Framework\App\Filesystem\DirectoryList::class, ['getPath']);
  27. $writeFactory = $this->createMock(\Magento\Framework\Filesystem\Directory\WriteFactory::class);
  28. $this->writeInterface = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\WriteInterface::class)
  29. ->setMethods(['getPath', 'delete'])
  30. ->getMockForAbstractClass();
  31. $writeFactory->expects($this->once())->method('create')->willReturn($this->writeInterface);
  32. $this->model = new GeneratedFiles($this->directoryList, $writeFactory);
  33. }
  34. /**
  35. * @param array $getPathMap
  36. * @param array $isDirectoryMap
  37. * @param array $deleteMap
  38. * @dataProvider cleanGeneratedFilesDataProvider
  39. */
  40. public function testCleanGeneratedFiles($getPathMap, $isDirectoryMap, $deleteMap)
  41. {
  42. $this->writeInterface
  43. ->expects($this->any())
  44. ->method('isExist')
  45. ->with()
  46. ->willReturnMap([
  47. [GeneratedFiles::REGENERATE_FLAG, true],
  48. ['path/to/di', false]
  49. ]);
  50. $this->directoryList->expects($this->any())->method('getPath')->willReturnMap($getPathMap);
  51. $this->writeInterface->expects($this->any())->method('getRelativePath')->willReturnMap($getPathMap);
  52. $this->writeInterface->expects($this->any())->method('isDirectory')->willReturnMap($isDirectoryMap);
  53. $this->writeInterface->expects($this->exactly(1))->method('delete')->willReturnMap($deleteMap);
  54. $this->model->cleanGeneratedFiles();
  55. }
  56. /**
  57. * @return array
  58. */
  59. public function cleanGeneratedFilesDataProvider()
  60. {
  61. $pathToGeneration = 'path/to/generation';
  62. $pathToDi = 'path/to/di';
  63. $pathToCache = 'path/to/di';
  64. $pathToConfig = 'path/to/config';
  65. $getPathMap = [
  66. [DirectoryList::GENERATED_CODE, $pathToGeneration],
  67. [DirectoryList::GENERATED_METADATA, $pathToDi],
  68. [DirectoryList::CACHE, $pathToCache],
  69. [DirectoryList::CONFIG, $pathToConfig],
  70. ];
  71. $deleteMap = [[BP . '/' . $pathToGeneration, true],
  72. [BP . '/' . $pathToDi, true],
  73. [BP . GeneratedFiles::REGENERATE_FLAG, true],
  74. ];
  75. return [
  76. 'runAll' => [ $getPathMap, [[BP . '/' . $pathToGeneration, true],
  77. [BP . '/' . $pathToDi, true]], $deleteMap ],
  78. 'noDIfolder' => [ $getPathMap, [[BP . '/' . $pathToGeneration, true],
  79. [BP . '/' . $pathToDi, false]], $deleteMap],
  80. 'noGenerationfolder' => [$getPathMap, [[BP . '/' . $pathToGeneration, false],
  81. [BP . '/' . $pathToDi, true]], $deleteMap],
  82. 'nofolders' => [ $getPathMap, [[BP . '/' . $pathToGeneration, false],
  83. [BP . '/' . $pathToDi, false]], $deleteMap],
  84. ];
  85. }
  86. public function testCleanGeneratedFilesWithNoFlag()
  87. {
  88. $this->writeInterface
  89. ->expects($this->once())
  90. ->method('isExist')
  91. ->with(GeneratedFiles::REGENERATE_FLAG)
  92. ->willReturn(false);
  93. $this->directoryList->expects($this->never())->method('getPath');
  94. $this->writeInterface->expects($this->never())->method('getPath');
  95. $this->writeInterface->expects($this->never())->method('delete');
  96. $this->model->cleanGeneratedFiles();
  97. }
  98. public function testRequestRegeneration()
  99. {
  100. $this->writeInterface->expects($this->once())->method("touch");
  101. $this->model->requestRegeneration();
  102. }
  103. }