GeneratedFilesTest.php 4.27 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Framework\Code\Test\Unit;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Code\GeneratedFiles;

class GeneratedFilesTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var \Magento\Framework\App\Filesystem\DirectoryList | \PHPUnit_Framework_MockObject_MockObject
     */
    private $directoryList;

    /**
     * @var \Magento\Framework\Filesystem\Directory\WriteInterface | \PHPUnit_Framework_MockObject_MockObject
     */
    private $writeInterface;

    /**
     * @var \Magento\Framework\Code\GeneratedFiles
     */
    private $model;

    protected function setUp()
    {
        $this->directoryList =
            $this->createPartialMock(\Magento\Framework\App\Filesystem\DirectoryList::class, ['getPath']);
        $writeFactory = $this->createMock(\Magento\Framework\Filesystem\Directory\WriteFactory::class);
        $this->writeInterface = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\WriteInterface::class)
            ->setMethods(['getPath', 'delete'])
            ->getMockForAbstractClass();
        $writeFactory->expects($this->once())->method('create')->willReturn($this->writeInterface);
        $this->model = new GeneratedFiles($this->directoryList, $writeFactory);
    }

    /**
     * @param array $getPathMap
     * @param array $isDirectoryMap
     * @param array $deleteMap
     * @dataProvider cleanGeneratedFilesDataProvider
     */
    public function testCleanGeneratedFiles($getPathMap, $isDirectoryMap, $deleteMap)
    {

        $this->writeInterface
            ->expects($this->any())
            ->method('isExist')
            ->with()
            ->willReturnMap([
                [GeneratedFiles::REGENERATE_FLAG, true],
                ['path/to/di', false]
            ]);
        $this->directoryList->expects($this->any())->method('getPath')->willReturnMap($getPathMap);
        $this->writeInterface->expects($this->any())->method('getRelativePath')->willReturnMap($getPathMap);
        $this->writeInterface->expects($this->any())->method('isDirectory')->willReturnMap($isDirectoryMap);
        $this->writeInterface->expects($this->exactly(1))->method('delete')->willReturnMap($deleteMap);
        $this->model->cleanGeneratedFiles();
    }

    /**
     * @return array
     */
    public function cleanGeneratedFilesDataProvider()
    {
        $pathToGeneration = 'path/to/generation';
        $pathToDi = 'path/to/di';
        $pathToCache = 'path/to/di';
        $pathToConfig = 'path/to/config';

        $getPathMap =     [
            [DirectoryList::GENERATED_CODE, $pathToGeneration],
            [DirectoryList::GENERATED_METADATA, $pathToDi],
            [DirectoryList::CACHE, $pathToCache],
            [DirectoryList::CONFIG, $pathToConfig],
        ];

        $deleteMap = [[BP . '/' . $pathToGeneration, true],
            [BP . '/' . $pathToDi, true],
            [BP . GeneratedFiles::REGENERATE_FLAG, true],
        ];

        return [
            'runAll' => [ $getPathMap, [[BP . '/' . $pathToGeneration, true],
                [BP . '/' . $pathToDi, true]], $deleteMap ],
            'noDIfolder' => [ $getPathMap, [[BP . '/' . $pathToGeneration, true],
                [BP . '/' . $pathToDi, false]], $deleteMap],
            'noGenerationfolder' => [$getPathMap, [[BP . '/' . $pathToGeneration, false],
                [BP . '/' . $pathToDi, true]], $deleteMap],
            'nofolders' => [ $getPathMap, [[BP . '/' . $pathToGeneration, false],
                [BP . '/' . $pathToDi, false]], $deleteMap],
        ];
    }

    public function testCleanGeneratedFilesWithNoFlag()
    {
        $this->writeInterface
            ->expects($this->once())
            ->method('isExist')
            ->with(GeneratedFiles::REGENERATE_FLAG)
            ->willReturn(false);
        $this->directoryList->expects($this->never())->method('getPath');
        $this->writeInterface->expects($this->never())->method('getPath');
        $this->writeInterface->expects($this->never())->method('delete');
        $this->model->cleanGeneratedFiles();
    }

    public function testRequestRegeneration()
    {
        $this->writeInterface->expects($this->once())->method("touch");
        $this->model->requestRegeneration();
    }
}