|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\SampleData\Test\Unit\Console\Command; |
| 7 | + |
| 8 | +use Composer\Console\Application; |
| 9 | +use Composer\Console\ApplicationFactory; |
| 10 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 11 | +use Magento\Framework\Filesystem; |
| 12 | +use Magento\Framework\Filesystem\Directory\ReadInterface; |
| 13 | +use Magento\Framework\Filesystem\Directory\WriteInterface; |
| 14 | +use Magento\SampleData\Model\Dependency; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\Component\Console\Input\ArrayInput; |
| 17 | +use Symfony\Component\Console\Input\ArrayInputFactory; |
| 18 | + |
| 19 | +/** |
| 20 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 21 | + */ |
| 22 | +abstract class AbstractSampleDataCommandTest extends TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var ReadInterface|\PHPUnit_Framework_MockObject_MockObject |
| 26 | + */ |
| 27 | + protected $directoryReadMock; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var WriteInterface|\PHPUnit_Framework_MockObject_MockObject |
| 31 | + */ |
| 32 | + protected $directoryWriteMock; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var Filesystem|\PHPUnit_Framework_MockObject_MockObject |
| 36 | + */ |
| 37 | + protected $filesystemMock; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var Dependency|\PHPUnit_Framework_MockObject_MockObject |
| 41 | + */ |
| 42 | + protected $sampleDataDependencyMock; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var ArrayInputFactory|\PHPUnit_Framework_MockObject_MockObject |
| 46 | + */ |
| 47 | + protected $arrayInputFactoryMock; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var Application|\PHPUnit_Framework_MockObject_MockObject |
| 51 | + */ |
| 52 | + protected $applicationMock; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var ApplicationFactory|\PHPUnit_Framework_MockObject_MockObject |
| 56 | + */ |
| 57 | + protected $applicationFactoryMock; |
| 58 | + |
| 59 | + /** |
| 60 | + * @return void |
| 61 | + */ |
| 62 | + protected function setUp() |
| 63 | + { |
| 64 | + $this->directoryReadMock = $this->createMock(ReadInterface::class); |
| 65 | + $this->directoryWriteMock = $this->createMock(WriteInterface::class); |
| 66 | + $this->filesystemMock = $this->createMock(Filesystem::class); |
| 67 | + $this->sampleDataDependencyMock = $this->createMock(Dependency::class); |
| 68 | + $this->arrayInputFactoryMock = $this->createMock(ArrayInputFactory::class); |
| 69 | + $this->applicationMock = $this->createMock(Application::class); |
| 70 | + $this->applicationFactoryMock = $this->createPartialMock(ApplicationFactory::class, ['create']); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param array $sampleDataPackages Array in form [package_name => version_constraint] |
| 75 | + * @param string $pathToComposerJson Fake path to composer.json |
| 76 | + * @param int $appRunResult Composer exit code |
| 77 | + * @param array $additionalComposerArgs Additional arguments that composer expects |
| 78 | + */ |
| 79 | + protected function setupMocks( |
| 80 | + $sampleDataPackages, |
| 81 | + $pathToComposerJson, |
| 82 | + $appRunResult, |
| 83 | + $additionalComposerArgs = [] |
| 84 | + ) { |
| 85 | + $this->directoryReadMock->expects($this->any())->method('getAbsolutePath')->willReturn($pathToComposerJson); |
| 86 | + $this->filesystemMock->expects($this->any())->method('getDirectoryRead')->with(DirectoryList::ROOT)->willReturn( |
| 87 | + $this->directoryReadMock |
| 88 | + ); |
| 89 | + $this->sampleDataDependencyMock->expects($this->any())->method('getSampleDataPackages')->willReturn( |
| 90 | + $sampleDataPackages |
| 91 | + ); |
| 92 | + $this->arrayInputFactoryMock->expects($this->never())->method('create'); |
| 93 | + |
| 94 | + $this->applicationMock->expects($this->any()) |
| 95 | + ->method('run') |
| 96 | + ->with( |
| 97 | + new ArrayInput( |
| 98 | + array_merge( |
| 99 | + $this->expectedComposerArguments( |
| 100 | + $sampleDataPackages, |
| 101 | + $pathToComposerJson |
| 102 | + ), |
| 103 | + $additionalComposerArgs |
| 104 | + ) |
| 105 | + ), |
| 106 | + $this->anything() |
| 107 | + ) |
| 108 | + ->willReturn($appRunResult); |
| 109 | + |
| 110 | + if (($appRunResult !== 0) && !empty($sampleDataPackages)) { |
| 111 | + $this->applicationMock->expects($this->once())->method('resetComposer')->willReturnSelf(); |
| 112 | + } |
| 113 | + |
| 114 | + $this->applicationFactoryMock->expects($this->any()) |
| 115 | + ->method('create') |
| 116 | + ->willReturn($this->applicationMock); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Expected arguments for composer based on sample data packages and composer.json path |
| 121 | + * |
| 122 | + * @param array $sampleDataPackages |
| 123 | + * @param string $pathToComposerJson |
| 124 | + * @return array |
| 125 | + */ |
| 126 | + abstract protected function expectedComposerArguments( |
| 127 | + array $sampleDataPackages, |
| 128 | + string $pathToComposerJson |
| 129 | + ) : array; |
| 130 | +} |
0 commit comments