CleanupFilesTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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\State;
  7. use \Magento\Framework\App\State\CleanupFiles;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. use Magento\Framework\Filesystem\DriverPool;
  10. class CleanupFilesTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \PHPUnit_Framework_MockObject_MockObject
  14. */
  15. private $filesystem;
  16. /**
  17. * @var CleanupFiles
  18. */
  19. private $object;
  20. protected function setUp()
  21. {
  22. $this->filesystem = $this->createMock(\Magento\Framework\Filesystem::class);
  23. $this->object = new CleanupFiles($this->filesystem);
  24. }
  25. public function testClearCodeGeneratedClasses()
  26. {
  27. $dir1 = $this->getDirectoryCleanMock();
  28. $dir2 = $this->getDirectoryCleanMock();
  29. $this->filesystem->expects($this->exactly(2))
  30. ->method('getDirectoryWrite')
  31. ->will(
  32. $this->returnValueMap(
  33. [
  34. [DirectoryList::GENERATED_CODE, DriverPool::FILE, $dir1],
  35. [DirectoryList::GENERATED_METADATA, DriverPool::FILE, $dir2],
  36. ]
  37. )
  38. );
  39. $this->object->clearCodeGeneratedClasses();
  40. }
  41. public function testClearMaterializedViewFiles()
  42. {
  43. $static = $this->getDirectoryCleanMock();
  44. $var = $this->getDirectoryCleanMock(DirectoryList::TMP_MATERIALIZATION_DIR);
  45. $this->filesystem->expects($this->exactly(2))->method('getDirectoryWrite')->will($this->returnValueMap([
  46. [DirectoryList::STATIC_VIEW, DriverPool::FILE, $static],
  47. [DirectoryList::VAR_DIR, DriverPool::FILE, $var],
  48. ]));
  49. $this->object->clearMaterializedViewFiles();
  50. }
  51. /**
  52. * Gets a mock of directory with expectation to be cleaned
  53. *
  54. * @param string|null $subPath
  55. * @return \PHPUnit_Framework_MockObject_MockObject
  56. */
  57. private function getDirectoryCleanMock($subPath = null)
  58. {
  59. $dir = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\WriteInterface::class);
  60. $dir->expects($this->once())->method('search')->with('*', $subPath)->willReturn(['one', 'two']);
  61. $dir->expects($this->exactly(2))->method('delete');
  62. $dir->expects($this->once())->method('isExist')->will($this->returnValue(true));
  63. return $dir;
  64. }
  65. }