<?php declare(strict_types=1);
/**
 * Copyright 2016 Adobe
 * All Rights Reserved.
 */
namespace Magento\Developer\Test\Unit\Model\Config\Backend;

use Magento\Developer\Model\Config\Backend\WorkflowType;
use Magento\Developer\Model\Config\Source\WorkflowType as SourceWorkflowType;
use Magento\Framework\App\State;
use Magento\Framework\App\State\CleanupFiles;
use Magento\Framework\Model\Context;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class WorkflowTypeTest extends TestCase
{
    /**
     * @var WorkflowType
     */
    private $model;

    /**
     * @var State|MockObject
     */
    private $appStateMock;

    /**
     * @var CleanupFiles|MockObject
     */
    private $cleanerMock;

    /**
     * @var ObjectManager
     */
    protected $objectManagerHelper;

    protected function setUp(): void
    {
        $this->appStateMock = $this->createMock(State::class);
        $this->objectManagerHelper = new ObjectManager($this);
        $contextArgs = $this->objectManagerHelper->getConstructArguments(
            Context::class,
            ['appState' => $this->appStateMock]
        );

        $this->cleanerMock = $this->createMock(CleanupFiles::class);

        $this->model = $this->objectManagerHelper->getObject(
            WorkflowType::class,
            [
                'context' => $this->objectManagerHelper->getObject(Context::class, $contextArgs),
                'cleaner' => $this->cleanerMock
            ]
        );

        parent::setUp();
    }

    public function testBeforeSaveSwitchedToClientSideInProductionShouldThrowException()
    {
        $this->expectException('Magento\Framework\Exception\LocalizedException');
        $this->expectExceptionMessage('Client side compilation doesn\'t work in production mode');
        $this->appStateMock->expects($this->once())
            ->method('getMode')
            ->willReturn(State::MODE_PRODUCTION);

        $this->model->setValue(SourceWorkflowType::CLIENT_SIDE_COMPILATION);
        $this->model->beforeSave();
    }

    public function testAfterSaveValueIsChangedShouldCleanViewFiles()
    {
        $this->model->setValue(SourceWorkflowType::SERVER_SIDE_COMPILATION);
        $this->cleanerMock->expects($this->once())->method('clearMaterializedViewFiles');
        $this->model->afterSave();
    }
}
