|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\SampleData\Test\Unit\Model; |
| 7 | + |
| 8 | +use Magento\Framework\Component\ComponentRegistrar; |
| 9 | +use Magento\Framework\Component\ComponentRegistrarInterface; |
| 10 | +use Magento\Framework\Composer\ComposerInformation; |
| 11 | +use Magento\Framework\Config\Composer\Package; |
| 12 | +use Magento\Framework\Config\Composer\PackageFactory; |
| 13 | +use Magento\Framework\Exception\FileSystemException; |
| 14 | +use Magento\Framework\Filesystem; |
| 15 | +use Magento\Framework\Phrase; |
| 16 | +use Magento\SampleData\Model\Dependency; |
| 17 | + |
| 18 | +class DependencyTest extends \PHPUnit_Framework_TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @dataProvider dataPackagesFromComposerSuggest |
| 22 | + * @param string[] $moduleDirectories |
| 23 | + * @param callable $composerJsonGenerator |
| 24 | + * @param string[] $suggestionsFromLockFile |
| 25 | + * @param string[] $expectedPackages |
| 26 | + */ |
| 27 | + public function testPackagesFromComposerSuggest( |
| 28 | + array $moduleDirectories, |
| 29 | + callable $composerJsonGenerator, |
| 30 | + array $suggestionsFromLockFile, |
| 31 | + array $expectedPackages |
| 32 | + ) { |
| 33 | + /** @var ComposerInformation|\PHPUnit_Framework_MockObject_MockObject $composerInformation */ |
| 34 | + $composerInformation = $this->getMockBuilder(ComposerInformation::class) |
| 35 | + ->disableOriginalConstructor() |
| 36 | + ->getMock(); |
| 37 | + $composerInformation->method('getSuggestedPackages') |
| 38 | + ->willReturn($suggestionsFromLockFile); |
| 39 | + |
| 40 | + /** @var Filesystem|\PHPUnit_Framework_MockObject_MockObject $filesystem */ |
| 41 | + $filesystem = $this->getMockBuilder(Filesystem::class)->disableOriginalConstructor()->getMock(); |
| 42 | + |
| 43 | + /** @var PackageFactory|\PHPUnit_Framework_MockObject_MockObject $packageFactory */ |
| 44 | + $packageFactory = $this->getMockBuilder(PackageFactory::class) |
| 45 | + ->disableOriginalConstructor() |
| 46 | + ->setMethods(['create']) |
| 47 | + ->getMock(); |
| 48 | + $packageFactory->method('create') |
| 49 | + ->willReturnCallback(function ($args) { |
| 50 | + return new Package($args['json']); |
| 51 | + }); |
| 52 | + |
| 53 | + /** @var ComponentRegistrarInterface|\PHPUnit_Framework_MockObject_MockObject $componentRegistrar */ |
| 54 | + $componentRegistrar = $this->getMockBuilder(ComponentRegistrarInterface::class) |
| 55 | + ->getMockForAbstractClass(); |
| 56 | + $componentRegistrar->method('getPaths') |
| 57 | + ->with(ComponentRegistrar::MODULE) |
| 58 | + ->willReturn( |
| 59 | + $moduleDirectories |
| 60 | + ); |
| 61 | + |
| 62 | + $directoryReadFactory = $this->getMockBuilder(Filesystem\Directory\ReadInterfaceFactory::class) |
| 63 | + ->disableOriginalConstructor() |
| 64 | + ->setMethods(['create']) |
| 65 | + ->getMock(); |
| 66 | + $directoryReadFactory->method('create') |
| 67 | + ->willReturnMap($composerJsonGenerator($this)); |
| 68 | + |
| 69 | + $dependency = new Dependency( |
| 70 | + $composerInformation, |
| 71 | + $filesystem, |
| 72 | + $packageFactory, |
| 73 | + $componentRegistrar, |
| 74 | + $directoryReadFactory |
| 75 | + ); |
| 76 | + $this->assertEquals($expectedPackages, $dependency->getSampleDataPackages()); |
| 77 | + } |
| 78 | + public static function dataPackagesFromComposerSuggest() |
| 79 | + { |
| 80 | + return [ |
| 81 | + [ |
| 82 | + 'moduleDirectories' => [ |
| 83 | + 'app/code/LocalModule', |
| 84 | + 'app/code/LocalModuleWithoutComposerJson', |
| 85 | + 'vendor/company/module', |
| 86 | + 'vendor/company2/module/src' |
| 87 | + ], |
| 88 | + 'composerJsonGenerator' => function (DependencyTest $test) { |
| 89 | + return [ |
| 90 | + [ |
| 91 | + ['path' => 'app/code/LocalModule'], |
| 92 | + $test->stubComposerJsonReader( |
| 93 | + [ |
| 94 | + 'name' => 'local/module', |
| 95 | + 'suggest' => [ |
| 96 | + 'local/module-sample-data' => Dependency::SAMPLE_DATA_SUGGEST . '0.1.0' |
| 97 | + ] |
| 98 | + ] |
| 99 | + ) |
| 100 | + ], |
| 101 | + [ |
| 102 | + ['path' => 'app/code/LocalModuleWithoutComposerJson'], |
| 103 | + $test->stubFileNotFoundReader() |
| 104 | + ], |
| 105 | + [ |
| 106 | + ['path' => 'vendor/company/module'], |
| 107 | + $test->stubComposerJsonReader( |
| 108 | + [ |
| 109 | + 'name' => 'company/module', |
| 110 | + 'suggest' => [ |
| 111 | + 'company/module-sample-data' => Dependency::SAMPLE_DATA_SUGGEST . '1.0.0-beta' |
| 112 | + ] |
| 113 | + ] |
| 114 | + ) |
| 115 | + ], |
| 116 | + [ |
| 117 | + ['path' => 'vendor/company2/module/src/..'], |
| 118 | + $test->stubComposerJsonReader( |
| 119 | + [ |
| 120 | + 'name' => 'company2/module', |
| 121 | + 'suggest' => [ |
| 122 | + 'company2/module-sample-data' => Dependency::SAMPLE_DATA_SUGGEST . '1.10' |
| 123 | + ] |
| 124 | + ] |
| 125 | + ) |
| 126 | + ], |
| 127 | + [ |
| 128 | + ['path' => 'vendor/company2/module/src'], |
| 129 | + $test->stubFileNotFoundReader() |
| 130 | + ], |
| 131 | + [ |
| 132 | + ['path' => 'vendor/company/module/..'], |
| 133 | + $test->stubFileNotFoundReader() |
| 134 | + ], |
| 135 | + [ |
| 136 | + ['path' => 'app/code/LocalModuleWithoutComposerJson/..'], |
| 137 | + $test->stubFileNotFoundReader() |
| 138 | + ], |
| 139 | + [ |
| 140 | + ['path' => 'app/code/LocalModule/..'], |
| 141 | + $test->stubFileNotFoundReader() |
| 142 | + ], |
| 143 | + ]; |
| 144 | + }, |
| 145 | + 'suggestions' => [ |
| 146 | + 'magento/foo-sample-data' => Dependency::SAMPLE_DATA_SUGGEST . '100.0.0', |
| 147 | + 'thirdparty/bar-sample-data' => Dependency::SAMPLE_DATA_SUGGEST . '1.2.3', |
| 148 | + 'thirdparty/something-else' => 'Just a suggested package', |
| 149 | + ], |
| 150 | + 'expectedPackages' => [ |
| 151 | + 'magento/foo-sample-data' => '100.0.0', |
| 152 | + 'thirdparty/bar-sample-data' => '1.2.3', |
| 153 | + 'local/module-sample-data' => '0.1.0', |
| 154 | + 'company/module-sample-data' => '1.0.0-beta', |
| 155 | + 'company2/module-sample-data' => '1.10', |
| 156 | + ] |
| 157 | + ] |
| 158 | + ]; |
| 159 | + } |
| 160 | + |
| 161 | + public function stubComposerJsonReader(array $composerJsonContent) |
| 162 | + { |
| 163 | + $stub = $this->getMockBuilder(Filesystem\Directory\ReadInterface::class) |
| 164 | + ->getMockForAbstractClass(); |
| 165 | + $stub->method('isExist') |
| 166 | + ->with('composer.json') |
| 167 | + ->willReturn(true); |
| 168 | + $stub->method('isReadable') |
| 169 | + ->with('composer.json') |
| 170 | + ->willReturn(true); |
| 171 | + $stub->method('readFile') |
| 172 | + ->with('composer.json') |
| 173 | + ->willReturn(json_encode($composerJsonContent)); |
| 174 | + return $stub; |
| 175 | + } |
| 176 | + |
| 177 | + public function stubFileNotFoundReader() |
| 178 | + { |
| 179 | + $stub = $this->getMockBuilder(Filesystem\Directory\ReadInterface::class) |
| 180 | + ->getMockForAbstractClass(); |
| 181 | + $stub->method('isExist') |
| 182 | + ->with('composer.json') |
| 183 | + ->willReturn(false); |
| 184 | + $stub->method('readFile') |
| 185 | + ->with('composer.json') |
| 186 | + ->willThrowException(new FileSystemException(new Phrase('File not found'))); |
| 187 | + return $stub; |
| 188 | + } |
| 189 | +} |
0 commit comments