-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathBamarniBinPluginTest.php
More file actions
120 lines (100 loc) · 3.25 KB
/
BamarniBinPluginTest.php
File metadata and controls
120 lines (100 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
declare(strict_types=1);
namespace Bamarni\Composer\Bin\Tests;
use Bamarni\Composer\Bin\BamarniBinPlugin;
use Bamarni\Composer\Bin\CommandForwardingContext;
use Bamarni\Composer\Bin\Config\Config;
use Composer\Composer;
use Composer\IO\ConsoleIO;
use Composer\Package\PackageInterface;
use Composer\Plugin\CommandEvent;
use Composer\Plugin\PluginEvents;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @covers \Bamarni\Composer\Bin\BamarniBinPlugin
*/
final class BamarniBinPluginTest extends TestCase
{
protected function tearDown(): void
{
CommandForwardingContext::setCommandForwardingDisabled(false);
}
public function test_it_does_not_forward_commands_when_the_context_disables_forwarding(): void
{
CommandForwardingContext::setCommandForwardingDisabled(true);
$input = new ArgvInput(['composer', 'update']);
$plugin = new BamarniBinPluginSpy();
$plugin->activate(
$this->createComposer(true),
self::createConsoleIO($input)
);
$event = new CommandEvent(
PluginEvents::COMMAND,
'update',
$input,
new NullOutput()
);
self::assertTrue($plugin->onCommandEvent($event));
self::assertSame(0, $plugin->forwardedCommandCount);
}
public function test_it_keeps_forwarding_commands_when_the_context_allows_forwarding(): void
{
$input = new ArgvInput(['composer', 'update']);
$plugin = new BamarniBinPluginSpy();
$plugin->activate(
$this->createComposer(true),
self::createConsoleIO($input)
);
$event = new CommandEvent(
PluginEvents::COMMAND,
'update',
$input,
new NullOutput()
);
self::assertTrue($plugin->onCommandEvent($event));
self::assertSame(1, $plugin->forwardedCommandCount);
}
private static function createConsoleIO(InputInterface $input): ConsoleIO
{
return new ConsoleIO(
$input,
new NullOutput(),
new HelperSet()
);
}
private function createComposer(bool $forwardCommand): Composer
{
$package = $this->createMock(PackageInterface::class);
$package
->method('getExtra')
->willReturn([
Config::EXTRA_CONFIG_KEY => [
Config::BIN_LINKS_ENABLED => false,
Config::FORWARD_COMMAND => $forwardCommand,
Config::TARGET_DIRECTORY => 'vendor-bin',
],
]);
$composer = $this->createMock(Composer::class);
$composer->method('getPackage')->willReturn($package);
return $composer;
}
}
final class BamarniBinPluginSpy extends BamarniBinPlugin
{
/**
* @var int
*/
public $forwardedCommandCount = 0;
protected function onForwardedCommand(
InputInterface $input,
OutputInterface $output
): bool {
++$this->forwardedCommandCount;
return true;
}
}