DeployManagerTest.php 6.05 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Deploy\Test\Unit\Model;

use Magento\Deploy\Console\Command\DeployStaticOptionsInterface as Options;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class DeployManagerTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Deploy\Model\DeployStrategyProviderFactory
     */
    private $deployStrategyProviderFactoryMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject|\Symfony\Component\Console\Output\OutputInterface
     */
    private $outputMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\View\Deployment\Version\StorageInterface
     */
    private $versionStorageMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Deploy\Model\Deploy\TemplateMinifier
     */
    private $minifierTemplateMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Deploy\Model\ProcessQueueManagerFactory
     */
    private $processQueueManagerFactoryMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\State
     */
    private $stateMock;

    protected function setUp()
    {
        $this->deployStrategyProviderFactoryMock = $this->getMock(
            \Magento\Deploy\Model\DeployStrategyProviderFactory::class,
            ['create'],
            [],
            '',
            false
        );
        $this->versionStorageMock = $this->getMock(
            \Magento\Framework\App\View\Deployment\Version\StorageInterface::class,
            [],
            [],
            '',
            false
        );
        $this->minifierTemplateMock = $this->getMock(
            \Magento\Deploy\Model\Deploy\TemplateMinifier::class,
            [],
            [],
            '',
            false
        );
        $this->processQueueManagerFactoryMock = $this->getMock(
            \Magento\Deploy\Model\ProcessQueueManagerFactory::class,
            [],
            [],
            '',
            false
        );
        $this->stateMock = $this->getMockBuilder(\Magento\Framework\App\State::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->outputMock = $this->getMock(\Symfony\Component\Console\Output\OutputInterface::class, [], [], '', false);
    }

    public function testSaveDeployedVersion()
    {
        $version = (new \DateTime())->getTimestamp();
        $this->outputMock->expects($this->once())->method('writeln')->with("New version of deployed files: {$version}");
        $this->versionStorageMock->expects($this->once())->method('save')->with($version);

        $this->assertEquals(
            \Magento\Framework\Console\Cli::RETURN_SUCCESS,
            $this->getModel([Options::NO_HTML_MINIFY => true])->deploy()
        );
    }

    public function testSaveDeployedVersionDryRun()
    {
        $options = [Options::DRY_RUN => true, Options::NO_HTML_MINIFY => true];

        $this->outputMock->expects(self::once())->method('writeln')->with(
            'Dry run. Nothing will be recorded to the target directory.'
        );
        $this->versionStorageMock->expects($this->never())->method('save');

        $this->getModel($options)->deploy();
    }

    public function testMinifyTemplates()
    {
        $this->minifierTemplateMock->expects($this->once())->method('minifyTemplates')->willReturn(2);
        $this->outputMock->expects($this->atLeastOnce())->method('writeln')->withConsecutive(
            ["=== Minify templates ==="],
            ["\nSuccessful: 2 files modified\n---\n"]
        );

        $this->getModel([Options::NO_HTML_MINIFY => false])->deploy();
    }

    public function testMinifyTemplatesNoHtmlMinify()
    {
        $version = (new \DateTime())->getTimestamp();
        $this->outputMock->expects($this->once())->method('writeln')->with("New version of deployed files: {$version}");
        $this->versionStorageMock->expects($this->once())->method('save')->with($version);

        $this->getModel([Options::NO_HTML_MINIFY => true])->deploy();
    }

    public function testDeploy()
    {
        $area = 'frontend';
        $themePath = 'themepath';
        $locale = 'en_US';
        $options = [Options::NO_HTML_MINIFY => true];
        $strategyProviderMock = $this->getMock(\Magento\Deploy\Model\DeployStrategyProvider::class, [], [], '', false);
        $deployStrategyMock = $this->getMock(\Magento\Deploy\Model\Deploy\DeployInterface::class, [], [], '', false);

        $model = $this->getModel($options);
        $model->addPack($area, $themePath, $locale);
        $this->deployStrategyProviderFactoryMock->expects($this->once())->method('create')->with(
            ['output' => $this->outputMock, 'options' => $options]
        )->willReturn($strategyProviderMock);
        $strategyProviderMock->expects($this->once())->method('getDeployStrategies')->with($area, $themePath, [$locale])
            ->willReturn([$locale => $deployStrategyMock]);
        $this->stateMock->expects(self::once())->method('emulateAreaCode')
            ->with($area, [$deployStrategyMock, 'deploy'], [$area, $themePath, $locale])
            ->willReturn(\Magento\Framework\Console\Cli::RETURN_SUCCESS);

        $version = (new \DateTime())->getTimestamp();
        $this->outputMock->expects(self::once())->method('writeln')->with("New version of deployed files: {$version}");
        $this->versionStorageMock->expects($this->once())->method('save')->with($version);

        $this->assertEquals(\Magento\Framework\Console\Cli::RETURN_SUCCESS, $model->deploy());
    }

    /**
     * @param array $options
     * @return \Magento\Deploy\Model\DeployManager
     */
    private function getModel(array $options)
    {
        return new \Magento\Deploy\Model\DeployManager(
            $this->outputMock,
            $this->versionStorageMock,
            $this->deployStrategyProviderFactoryMock,
            $this->processQueueManagerFactoryMock,
            $this->minifierTemplateMock,
            $this->stateMock,
            $options
        );
    }
}