Issues (17)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

test/PackageVersionsTest/InstallerTest.php (14 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace PackageVersionsTest;
4
5
use Composer\Composer;
6
use Composer\Config;
7
use Composer\EventDispatcher\EventDispatcher;
8
use Composer\Installer\InstallationManager;
9
use Composer\IO\IOInterface;
10
use Composer\Package\Locker;
11
use Composer\Package\RootAliasPackage;
12
use Composer\Package\RootPackage;
13
use Composer\Package\RootPackageInterface;
14
use Composer\Repository\InstalledRepositoryInterface;
15
use Composer\Repository\RepositoryManager;
16
use Composer\Script\Event;
17
use PackageVersions\Installer;
18
use PHPUnit_Framework_TestCase;
19
20
/**
21
 * @covers \PackageVersions\Installer
22
 */
23
final class InstallerTest extends PHPUnit_Framework_TestCase
24
{
25
    /**
26
     * @var Composer|\PHPUnit_Framework_MockObject_MockObject
27
     */
28
    private $composer;
29
30
    /**
31
     * @var EventDispatcher|\PHPUnit_Framework_MockObject_MockObject
32
     */
33
    private $eventDispatcher;
34
35
    /**
36
     * @var IOInterface|\PHPUnit_Framework_MockObject_MockObject
37
     */
38
    private $io;
39
40
    /**
41
     * @var Installer
42
     */
43
    private $installer;
44
45
    /**
46
     * {@inheritDoc}
47
     * 
48
     * @throws \PHPUnit_Framework_Exception
49
     */
50
    protected function setUp()
51
    {
52
        parent::setUp();
53
54
        \PHPUnit_Framework_Error_Deprecated::$enabled = FALSE;
55
56
        $this->installer       = new Installer();
57
        $this->io              = $this->getMock(IOInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
58
        $this->composer        = $this->getMock(Composer::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
59
        $this->eventDispatcher = $this->getMockBuilder(EventDispatcher::class)->disableOriginalConstructor()->getMock();
60
61
        $this->composer->expects(self::any())->method('getEventDispatcher')->willReturn($this->eventDispatcher);
62
    }
63
64
    public function testActivate()
65
    {
66
        $this->eventDispatcher->expects(self::once())->method('addSubscriber')->with($this->installer);
0 ignored issues
show
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Composer\EventDispatcher\EventDispatcher.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
67
68
        $this->installer->activate($this->composer, $this->io);
69
    }
70
71
    public function testGetSubscribedEvents()
72
    {
73
        $events = Installer::getSubscribedEvents();
74
75
        self::assertSame(
76
            [
77
                'post-install-cmd' => 'dumpVersionsClass',
78
                'post-update-cmd'  => 'dumpVersionsClass',
79
            ],
80
            $events
81
        );
82
83
        foreach ($events as $callback) {
84
            self::assertInternalType('callable', [$this->installer, $callback]);
85
        }
86
    }
87
88
    public function testDumpVersionsClass()
89
    {
90
        $config            = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
91
        $locker            = $this->getMockBuilder(Locker::class)->disableOriginalConstructor()->getMock();
92
        $repositoryManager = $this->getMockBuilder(RepositoryManager::class)->disableOriginalConstructor()->getMock();
93
        $installManager    = $this->getMockBuilder(InstallationManager::class)->disableOriginalConstructor()->getMock();
94
        $repository        = $this->getMock(InstalledRepositoryInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
95
        $package           = $this->getMock(RootPackageInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
96
97
        $vendorDir = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true);
98
99
        $expectedPath = $vendorDir . '/samsonasik/package-versions/src/PackageVersions';
100
101
        mkdir($expectedPath, 0777, true);
102
103
        $locker
104
            ->expects(self::any())
105
            ->method('getLockData')
106
            ->willReturn([
107
                'packages' => [
108
                    [
109
                        'name'    => 'foo/bar',
110
                        'version' => '1.2.3',
111
                        'source'  => [
112
                            'reference' => 'abc123',
113
                        ],
114
                    ],
115
                    [
116
                        'name'    => 'baz/tab',
117
                        'version' => '4.5.6',
118
                        'source'  => [
119
                            'reference' => 'def456',
120
                        ],
121
                    ],
122
                ],
123
                'packages-dev' => [
124
                    [
125
                        'name'    => 'tar/taz',
126
                        'version' => '7.8.9',
127
                        'source'  => [
128
                            'reference' => 'ghi789',
129
                        ],
130
                    ]
131
                ],
132
            ]);
133
134
        $repositoryManager->expects(self::any())->method('getLocalRepository')->willReturn($repository);
135
136
        $this->composer->expects(self::any())->method('getConfig')->willReturn($config);
0 ignored issues
show
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Composer\Composer.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
137
        $this->composer->expects(self::any())->method('getLocker')->willReturn($locker);
138
        $this->composer->expects(self::any())->method('getRepositoryManager')->willReturn($repositoryManager);
139
        $this->composer->expects(self::any())->method('getPackage')->willReturn($package);
140
        $this->composer->expects(self::any())->method('getInstallationManager')->willReturn($installManager);
141
142
        $package->expects(self::any())->method('getName')->willReturn('root/package');
143
        $package->expects(self::any())->method('getVersion')->willReturn('1.3.5');
144
        $package->expects(self::any())->method('getSourceReference')->willReturn('aaabbbcccddd');
145
146
        $config->expects(self::any())->method('get')->with('vendor-dir')->willReturn($vendorDir);
147
148
        Installer::dumpVersionsClass(new Event(
149
            'post-install-cmd',
150
            $this->composer,
151
            $this->io
152
        ));
153
154
        $expectedSource = <<<'PHP'
155
<?php
156
157
namespace PackageVersions;
158
159
/**
160
 * This class is generated by samsonasik/package-versions, specifically by
161
 * @see \PackageVersions\Installer
162
 *
163
 * This file is overwritten at every run of `composer install` or `composer update`.
164
 */
165
final class Versions
166
{
167
    const VERSIONS = array (
168
  'foo/bar' => '1.2.3@abc123',
169
  'baz/tab' => '4.5.6@def456',
170
  'tar/taz' => '7.8.9@ghi789',
171
  'root/package' => '1.3.5@aaabbbcccddd',
172
);
173
174
    private function __construct()
175
    {
176
    }
177
178
    /**
179
     * @throws \OutOfBoundsException if a version cannot be located
180
     */
181
    public static function getVersion($packageName)
182
    {
183
        $selfVersion = self::VERSIONS;
184
185
        if (! isset($selfVersion[$packageName])) {
186
            throw new \OutOfBoundsException(
187
                'Required package "' . $packageName . '" is not installed: cannot detect its version'
188
            );
189
        }
190
191
        return $selfVersion[$packageName];
192
    }
193
194
    /**
195
     * @throws \OutOfBoundsException if a version cannot be located
196
     */
197
    public static function getShortVersion($packageName)
198
    {
199
        return explode('@', static::getVersion($packageName))[0];
200
    }
201
202
    /**
203
     * @throws \OutOfBoundsException if a version cannot be located
204
     */
205
    public static function getMajorVersion($packageName)
206
    {
207
        return explode('.', static::getShortVersion($packageName))[0];
208
    }
209
}
210
211
PHP;
212
213
        self::assertSame($expectedSource, file_get_contents($expectedPath . '/Versions.php'));
214
215
        $this->rmDir($vendorDir);
216
    }
217
218
    public function testDumpVersionsClassNoDev()
219
    {
220
        $config            = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
221
        $locker            = $this->getMockBuilder(Locker::class)->disableOriginalConstructor()->getMock();
222
        $repositoryManager = $this->getMockBuilder(RepositoryManager::class)->disableOriginalConstructor()->getMock();
223
        $installManager    = $this->getMockBuilder(InstallationManager::class)->disableOriginalConstructor()->getMock();
224
        $repository        = $this->getMock(InstalledRepositoryInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
225
        $package           = $this->getMock(RootPackageInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
226
227
        $vendorDir = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true);
228
229
        $expectedPath = $vendorDir . '/samsonasik/package-versions/src/PackageVersions';
230
231
        mkdir($expectedPath, 0777, true);
232
233
        $locker
234
            ->expects(self::any())
235
            ->method('getLockData')
236
            ->willReturn([
237
                'packages' => [
238
                    [
239
                        'name'    => 'foo/bar',
240
                        'version' => '1.2.3',
241
                        'source'  => [
242
                            'reference' => 'abc123',
243
                        ],
244
                    ],
245
                    [
246
                        'name'    => 'baz/tab',
247
                        'version' => '4.5.6',
248
                        'source'  => [
249
                            'reference' => 'def456',
250
                        ],
251
                    ],
252
                ],
253
            ]);
254
255
        $repositoryManager->expects(self::any())->method('getLocalRepository')->willReturn($repository);
256
257
        $this->composer->expects(self::any())->method('getConfig')->willReturn($config);
0 ignored issues
show
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Composer\Composer.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
258
        $this->composer->expects(self::any())->method('getLocker')->willReturn($locker);
259
        $this->composer->expects(self::any())->method('getRepositoryManager')->willReturn($repositoryManager);
260
        $this->composer->expects(self::any())->method('getPackage')->willReturn($package);
261
        $this->composer->expects(self::any())->method('getInstallationManager')->willReturn($installManager);
262
263
        $package->expects(self::any())->method('getName')->willReturn('root/package');
264
        $package->expects(self::any())->method('getVersion')->willReturn('1.3.5');
265
        $package->expects(self::any())->method('getSourceReference')->willReturn('aaabbbcccddd');
266
267
        $config->expects(self::any())->method('get')->with('vendor-dir')->willReturn($vendorDir);
268
269
        Installer::dumpVersionsClass(new Event(
270
            'post-install-cmd',
271
            $this->composer,
272
            $this->io
273
        ));
274
275
        $expectedSource = <<<'PHP'
276
<?php
277
278
namespace PackageVersions;
279
280
/**
281
 * This class is generated by samsonasik/package-versions, specifically by
282
 * @see \PackageVersions\Installer
283
 *
284
 * This file is overwritten at every run of `composer install` or `composer update`.
285
 */
286
final class Versions
287
{
288
    const VERSIONS = array (
289
  'foo/bar' => '1.2.3@abc123',
290
  'baz/tab' => '4.5.6@def456',
291
  'root/package' => '1.3.5@aaabbbcccddd',
292
);
293
294
    private function __construct()
295
    {
296
    }
297
298
    /**
299
     * @throws \OutOfBoundsException if a version cannot be located
300
     */
301
    public static function getVersion($packageName)
302
    {
303
        $selfVersion = self::VERSIONS;
304
305
        if (! isset($selfVersion[$packageName])) {
306
            throw new \OutOfBoundsException(
307
                'Required package "' . $packageName . '" is not installed: cannot detect its version'
308
            );
309
        }
310
311
        return $selfVersion[$packageName];
312
    }
313
314
    /**
315
     * @throws \OutOfBoundsException if a version cannot be located
316
     */
317
    public static function getShortVersion($packageName)
318
    {
319
        return explode('@', static::getVersion($packageName))[0];
320
    }
321
322
    /**
323
     * @throws \OutOfBoundsException if a version cannot be located
324
     */
325
    public static function getMajorVersion($packageName)
326
    {
327
        return explode('.', static::getShortVersion($packageName))[0];
328
    }
329
}
330
331
PHP;
332
333
        self::assertSame($expectedSource, file_get_contents($expectedPath . '/Versions.php'));
334
335
        $this->rmDir($vendorDir);
336
    }
337
338
    /**
339
     * @group #12
340
     * 
341
     * @throws \RuntimeException
342
     */
343
    public function testDumpVersionsWithoutPackageSourceDetails()
344
    {
345
        $config            = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
346
        $locker            = $this->getMockBuilder(Locker::class)->disableOriginalConstructor()->getMock();
347
        $repositoryManager = $this->getMockBuilder(RepositoryManager::class)->disableOriginalConstructor()->getMock();
348
        $installManager    = $this->getMockBuilder(InstallationManager::class)->disableOriginalConstructor()->getMock();
349
        $repository        = $this->getMock(InstalledRepositoryInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
350
        $package           = $this->getMock(RootPackageInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
351
352
        $vendorDir = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true);
353
354
        $expectedPath = $vendorDir . '/samsonasik/package-versions/src/PackageVersions';
355
356
        mkdir($expectedPath, 0777, true);
357
358
        $locker
359
            ->expects(self::any())
360
            ->method('getLockData')
361
            ->willReturn([
362
                'packages' => [
363
                    [
364
                        'name'    => 'foo/bar',
365
                        'version' => '1.2.3',
366
                        'dist'  => [
367
                            'reference' => 'abc123', // version defined in the dist, this time
368
                        ],
369
                    ],
370
                    [
371
                        'name'    => 'baz/tab',
372
                        'version' => '4.5.6', // source missing
373
                    ],
374
                ],
375
            ]);
376
377
        $repositoryManager->expects(self::any())->method('getLocalRepository')->willReturn($repository);
378
379
        $this->composer->expects(self::any())->method('getConfig')->willReturn($config);
0 ignored issues
show
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Composer\Composer.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
380
        $this->composer->expects(self::any())->method('getLocker')->willReturn($locker);
381
        $this->composer->expects(self::any())->method('getRepositoryManager')->willReturn($repositoryManager);
382
        $this->composer->expects(self::any())->method('getPackage')->willReturn($package);
383
        $this->composer->expects(self::any())->method('getInstallationManager')->willReturn($installManager);
384
385
        $package->expects(self::any())->method('getName')->willReturn('root/package');
386
        $package->expects(self::any())->method('getVersion')->willReturn('1.3.5');
387
        $package->expects(self::any())->method('getSourceReference')->willReturn('aaabbbcccddd');
388
389
        $config->expects(self::any())->method('get')->with('vendor-dir')->willReturn($vendorDir);
390
391
        Installer::dumpVersionsClass(new Event(
392
            'post-install-cmd',
393
            $this->composer,
394
            $this->io
395
        ));
396
397
        $expectedSource = <<<'PHP'
398
<?php
399
400
namespace PackageVersions;
401
402
/**
403
 * This class is generated by samsonasik/package-versions, specifically by
404
 * @see \PackageVersions\Installer
405
 *
406
 * This file is overwritten at every run of `composer install` or `composer update`.
407
 */
408
final class Versions
409
{
410
    const VERSIONS = array (
411
  'foo/bar' => '1.2.3@abc123',
412
  'baz/tab' => '4.5.6@',
413
  'root/package' => '1.3.5@aaabbbcccddd',
414
);
415
416
    private function __construct()
417
    {
418
    }
419
420
    /**
421
     * @throws \OutOfBoundsException if a version cannot be located
422
     */
423
    public static function getVersion($packageName)
424
    {
425
        $selfVersion = self::VERSIONS;
426
427
        if (! isset($selfVersion[$packageName])) {
428
            throw new \OutOfBoundsException(
429
                'Required package "' . $packageName . '" is not installed: cannot detect its version'
430
            );
431
        }
432
433
        return $selfVersion[$packageName];
434
    }
435
436
    /**
437
     * @throws \OutOfBoundsException if a version cannot be located
438
     */
439
    public static function getShortVersion($packageName)
440
    {
441
        return explode('@', static::getVersion($packageName))[0];
442
    }
443
444
    /**
445
     * @throws \OutOfBoundsException if a version cannot be located
446
     */
447
    public static function getMajorVersion($packageName)
448
    {
449
        return explode('.', static::getShortVersion($packageName))[0];
450
    }
451
}
452
453
PHP;
454
455
        self::assertSame($expectedSource, file_get_contents($expectedPath . '/Versions.php'));
456
457
        $this->rmDir($vendorDir);
458
    }
459
460
    /**
461
     * @dataProvider rootPackageProvider
462
     *
463
     * @param RootPackageInterface $rootPackage
464
     * @param bool                 $inVendor
465
     *
466
     * @throws \RuntimeException
467
     */
468
    public function testDumpsVersionsClassToSpecificLocation(RootPackageInterface $rootPackage, $inVendor)
469
    {
470
        $config            = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
471
        $locker            = $this->getMockBuilder(Locker::class)->disableOriginalConstructor()->getMock();
472
        $repositoryManager = $this->getMockBuilder(RepositoryManager::class)->disableOriginalConstructor()->getMock();
473
        $installManager    = $this->getMockBuilder(InstallationManager::class)->disableOriginalConstructor()->getMock();
474
        $repository        = $this->getMock(InstalledRepositoryInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
475
476
        $vendorDir = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true) . '/vendor';
477
478
        mkdir($vendorDir, 0777, true);
479
480
        $expectedPath = $inVendor
481
            ? $vendorDir . '/samsonasik/package-versions/src/PackageVersions'
482
            : realpath($vendorDir . '/..') . '/src/PackageVersions';
483
484
        mkdir($expectedPath, 0777, true);
485
486
        $locker
487
            ->expects(self::any())
488
            ->method('getLockData')
489
            ->willReturn([
490
                'packages' => [],
491
                'packages-dev' => [],
492
            ]);
493
494
        $repositoryManager->expects(self::any())->method('getLocalRepository')->willReturn($repository);
495
496
        $this->composer->expects(self::any())->method('getConfig')->willReturn($config);
0 ignored issues
show
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Composer\Composer.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
497
        $this->composer->expects(self::any())->method('getLocker')->willReturn($locker);
498
        $this->composer->expects(self::any())->method('getRepositoryManager')->willReturn($repositoryManager);
499
        $this->composer->expects(self::any())->method('getPackage')->willReturn($rootPackage);
500
        $this->composer->expects(self::any())->method('getInstallationManager')->willReturn($installManager);
501
502
        $config->expects(self::any())->method('get')->with('vendor-dir')->willReturn($vendorDir);
503
504
        Installer::dumpVersionsClass(new Event(
505
            'post-install-cmd',
506
            $this->composer,
507
            $this->io
508
        ));
509
510
        self::assertStringMatchesFormat(
511
            '%Aclass Versions%A1.2.3@%A',
512
            file_get_contents($expectedPath . '/Versions.php')
513
        );
514
515
        $this->rmDir($vendorDir);
516
    }
517
518
    /**
519
     * @return bool[][]|RootPackageInterface[][] the root package and whether the versions class is to be generated in
520
     *                                           the vendor dir or not
521
     */
522
    public function rootPackageProvider()
523
    {
524
        $baseRootPackage                         = new RootPackage('root/package', '1.2.3', '1.2.3');
525
        $aliasRootPackage                        = new RootAliasPackage($baseRootPackage, '1.2.3', '1.2.3');
526
        $indirectAliasRootPackage                = new RootAliasPackage($aliasRootPackage, '1.2.3', '1.2.3');
527
        $packageVersionsRootPackage              = new RootPackage('samsonasik/package-versions', '1.2.3', '1.2.3');
528
        $aliasPackageVersionsRootPackage         = new RootAliasPackage($packageVersionsRootPackage, '1.2.3', '1.2.3');
529
        $indirectAliasPackageVersionsRootPackage = new RootAliasPackage(
530
            $aliasPackageVersionsRootPackage,
531
            '1.2.3',
532
            '1.2.3'
533
        );
534
535
        return [
536
            'root package is not samsonasik/package-versions' => [
537
                $baseRootPackage,
538
                true
539
            ],
540
            'alias root package is not samsonasik/package-versions' => [
541
                $aliasRootPackage,
542
                true
543
            ],
544
            'indirect alias root package is not samsonasik/package-versions' => [
545
                $indirectAliasRootPackage,
546
                true
547
            ],
548
            'root package is samsonasik/package-versions' => [
549
                $packageVersionsRootPackage,
550
                false
551
            ],
552
            'alias root package is samsonasik/package-versions' => [
553
                $aliasPackageVersionsRootPackage,
554
                false
555
            ],
556
            'indirect alias root package is samsonasik/package-versions' => [
557
                $indirectAliasPackageVersionsRootPackage,
558
                false
559
            ],
560
        ];
561
    }
562
563
    /**
564
     * @param string $directory
565
     *
566
     * @return void
567
     */
568
    private function rmDir($directory)
569
    {
570
        if (! is_dir($directory)) {
571
            unlink($directory);
572
573
            return;
574
        }
575
576
        array_map(
577
            function ($item) use ($directory) {
578
                $this->rmDir($directory . '/' . $item);
579
            },
580
            array_filter(
581
                scandir($directory),
582
                function ($dirItem) {
583
                    return ! in_array($dirItem, ['.', '..'], true);
584
                }
585
            )
586
        );
587
    }
588
589
    /**
590
     * @group composer/composer#5237
591
     */
592
    public function testWillEscapeRegexParsingOfClassDefinitions()
593
    {
594
        self::assertSame(
595
            1,
596
            preg_match_all(
597
                '{^((?:final\s+)?(?:\s*))class\s+(\S+)}mi',
598
                file_get_contents((new \ReflectionClass(Installer::class))->getFileName())
599
            )
600
        );
601
    }
602
}
603