Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DependencyTest.php
Go to the documentation of this file.
1 <?php
7 
12 use Magento\Framework\Config\Composer\PackageFactory;
18 
24 class DependencyTest extends \PHPUnit\Framework\TestCase
25 {
33  public function testPackagesFromComposerSuggest(
34  array $moduleDirectories,
35  callable $composerJsonGenerator,
36  array $suggestionsFromLockFile,
37  array $expectedPackages
38  ) {
40  $composerInformation = $this->getMockBuilder(ComposerInformation::class)
41  ->disableOriginalConstructor()
42  ->getMock();
43  $composerInformation->method('getSuggestedPackages')
44  ->willReturn($suggestionsFromLockFile);
45 
47  $filesystem = $this->getMockBuilder(Filesystem::class)->disableOriginalConstructor()->getMock();
48 
50  $packageFactory = $this->getMockBuilder(PackageFactory::class)
51  ->disableOriginalConstructor()
52  ->setMethods(['create'])
53  ->getMock();
54  $packageFactory->method('create')
55  ->willReturnCallback(function ($args) {
56  return new Package($args['json']);
57  });
58 
60  $componentRegistrar = $this->getMockBuilder(ComponentRegistrarInterface::class)
61  ->getMockForAbstractClass();
62  $componentRegistrar->method('getPaths')
64  ->willReturn(
65  $moduleDirectories
66  );
67 
68  $directoryReadFactory = $this->getMockBuilder(Filesystem\Directory\ReadFactory::class)
69  ->disableOriginalConstructor()
70  ->setMethods(['create'])
71  ->getMock();
72  $directoryReadFactory->method('create')
73  ->willReturnMap($composerJsonGenerator($this));
74 
75  $dependency = new Dependency(
76  $composerInformation,
78  $packageFactory,
80  $directoryReadFactory
81  );
82  $this->assertEquals($expectedPackages, $dependency->getSampleDataPackages());
83  }
84 
88  public static function dataPackagesFromComposerSuggest()
89  {
90  return [
91  [
92  'moduleDirectories' => [
93  'app/code/LocalModule',
94  'app/code/LocalModuleWithoutComposerJson',
95  'vendor/company/module',
96  'vendor/company2/module/src'
97  ],
98  'composerJsonGenerator' => function (DependencyTest $test) {
99  return [
100  [
101  'app/code/LocalModule',
103  $test->stubComposerJsonReader(
104  [
105  'name' => 'local/module',
106  'suggest' => [
107  'local/module-sample-data' => Dependency::SAMPLE_DATA_SUGGEST . '0.1.0'
108  ]
109  ]
110  )
111  ],
112  [
113  'app/code/LocalModuleWithoutComposerJson',
115  $test->stubFileNotFoundReader()
116  ],
117  [
118  'vendor/company/module',
120  $test->stubComposerJsonReader(
121  [
122  'name' => 'company/module',
123  'suggest' => [
124  'company/module-sample-data' => Dependency::SAMPLE_DATA_SUGGEST . '1.0.0-beta'
125  ]
126  ]
127  )
128  ],
129  [
130  'vendor/company2/module/src/..',
132  $test->stubComposerJsonReader(
133  [
134  'name' => 'company2/module',
135  'suggest' => [
136  'company2/module-sample-data' => Dependency::SAMPLE_DATA_SUGGEST . '1.10'
137  ]
138  ]
139  )
140  ],
141  [
142  'vendor/company2/module/src',
144  $test->stubFileNotFoundReader()
145  ],
146  [
147  'vendor/company/module/..',
149  $test->stubFileNotFoundReader()
150  ],
151  [
152  'app/code/LocalModuleWithoutComposerJson/..',
154  $test->stubFileNotFoundReader()
155  ],
156  [
157  'app/code/LocalModule/..',
159  $test->stubFileNotFoundReader()
160  ],
161  ];
162  },
163  'suggestions' => [
164  'magento/foo-sample-data' => Dependency::SAMPLE_DATA_SUGGEST . ' 100.0.0',
165  'thirdparty/bar-sample-data' => Dependency::SAMPLE_DATA_SUGGEST . ' 1.2.3',
166  'thirdparty/something-else' => 'Just a suggested package',
167  ],
168  'expectedPackages' => [
169  'magento/foo-sample-data' => '100.0.0',
170  'thirdparty/bar-sample-data' => '1.2.3',
171  'local/module-sample-data' => '0.1.0',
172  'company/module-sample-data' => '1.0.0-beta',
173  'company2/module-sample-data' => '1.10',
174  ]
175  ]
176  ];
177  }
178 
183  public function stubComposerJsonReader(array $composerJsonContent)
184  {
185  $stub = $this->getMockBuilder(Filesystem\Directory\ReadInterface::class)
186  ->getMockForAbstractClass();
187  $stub->method('isExist')
188  ->with('composer.json')
189  ->willReturn(true);
190  $stub->method('isReadable')
191  ->with('composer.json')
192  ->willReturn(true);
193  $stub->method('readFile')
194  ->with('composer.json')
195  ->willReturn(json_encode($composerJsonContent));
196  return $stub;
197  }
198 
202  public function stubFileNotFoundReader()
203  {
204  $stub = $this->getMockBuilder(Filesystem\Directory\ReadInterface::class)
205  ->getMockForAbstractClass();
206  $stub->method('isExist')
207  ->with('composer.json')
208  ->willReturn(false);
209  $stub->method('readFile')
210  ->with('composer.json')
211  ->willThrowException(new FileSystemException(new Phrase('File not found')));
212  return $stub;
213  }
214 }
$componentRegistrar
Definition: bootstrap.php:23
$filesystem