|
| 1 | +<?php |
| 2 | +declare(strict_types = 1); |
| 3 | + |
| 4 | +namespace Tests\Innmind\DependencyGraph\Command; |
| 5 | + |
| 6 | +use Innmind\DependencyGraph\Command\CheckDotInstalled; |
| 7 | +use Innmind\CLI\{ |
| 8 | + Console, |
| 9 | + Command, |
| 10 | + Command\Arguments, |
| 11 | + Command\Options, |
| 12 | + Environment, |
| 13 | +}; |
| 14 | +use Innmind\Server\Control\Server\{ |
| 15 | + Processes, |
| 16 | + Process, |
| 17 | + Process\ExitCode, |
| 18 | + Process\Failed, |
| 19 | + Process\Output\Output, |
| 20 | +}; |
| 21 | +use Innmind\Immutable\{ |
| 22 | + Str, |
| 23 | + Either, |
| 24 | + SideEffect, |
| 25 | + Sequence, |
| 26 | +}; |
| 27 | +use PHPUnit\Framework\TestCase; |
| 28 | +use Innmind\BlackBox\{ |
| 29 | + PHPUnit\BlackBox, |
| 30 | + Set, |
| 31 | +}; |
| 32 | + |
| 33 | +class CheckDotInstalledTest extends TestCase |
| 34 | +{ |
| 35 | + use BlackBox; |
| 36 | + |
| 37 | + public function testCallCommandIfInstalled() |
| 38 | + { |
| 39 | + $this |
| 40 | + ->forAll(Set\Strings::any()) |
| 41 | + ->then(function($usage) { |
| 42 | + $inner = new class($usage) implements Command { |
| 43 | + public function __construct(private string $usage) |
| 44 | + { |
| 45 | + } |
| 46 | + |
| 47 | + public function __invoke(Console $console): Console |
| 48 | + { |
| 49 | + return $console->output(Str::of('all good')); |
| 50 | + } |
| 51 | + |
| 52 | + public function usage(): string |
| 53 | + { |
| 54 | + return $this->usage; |
| 55 | + } |
| 56 | + }; |
| 57 | + $processes = $this->createMock(Processes::class); |
| 58 | + $processes |
| 59 | + ->expects($this->once()) |
| 60 | + ->method('execute') |
| 61 | + ->with($this->callback(static function($command) { |
| 62 | + return $command->toString() === "dot '--help'"; |
| 63 | + })) |
| 64 | + ->willReturn($process = $this->createMock(Process::class)); |
| 65 | + $process |
| 66 | + ->expects($this->once()) |
| 67 | + ->method('wait') |
| 68 | + ->willReturn(Either::right(new SideEffect)); |
| 69 | + $command = new CheckDotInstalled($inner, $processes); |
| 70 | + $console = Console::of( |
| 71 | + Environment\InMemory::of( |
| 72 | + [], |
| 73 | + true, |
| 74 | + [], |
| 75 | + [], |
| 76 | + '/', |
| 77 | + ), |
| 78 | + new Arguments, |
| 79 | + new Options, |
| 80 | + ); |
| 81 | + |
| 82 | + $console = $command($console); |
| 83 | + |
| 84 | + $this->assertSame( |
| 85 | + ['all good'], |
| 86 | + $console->environment()->outputs(), |
| 87 | + ); |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + public function testReturnErrorWhenNotInstalled() |
| 92 | + { |
| 93 | + $this |
| 94 | + ->forAll(Set\Strings::any()) |
| 95 | + ->then(function($usage) { |
| 96 | + $inner = new class($usage) implements Command { |
| 97 | + public function __construct(private string $usage) |
| 98 | + { |
| 99 | + } |
| 100 | + |
| 101 | + public function __invoke(Console $console): Console |
| 102 | + { |
| 103 | + return $console->output(Str::of('all good')); |
| 104 | + } |
| 105 | + |
| 106 | + public function usage(): string |
| 107 | + { |
| 108 | + return $this->usage; |
| 109 | + } |
| 110 | + }; |
| 111 | + $processes = $this->createMock(Processes::class); |
| 112 | + $processes |
| 113 | + ->expects($this->once()) |
| 114 | + ->method('execute') |
| 115 | + ->with($this->callback(static function($command) { |
| 116 | + return $command->toString() === "dot '--help'"; |
| 117 | + })) |
| 118 | + ->willReturn($process = $this->createMock(Process::class)); |
| 119 | + $process |
| 120 | + ->expects($this->once()) |
| 121 | + ->method('wait') |
| 122 | + ->willReturn(Either::left(new Failed( |
| 123 | + new ExitCode(127), |
| 124 | + new Output(Sequence::of()), |
| 125 | + ))); |
| 126 | + $command = new CheckDotInstalled($inner, $processes); |
| 127 | + $console = Console::of( |
| 128 | + Environment\InMemory::of( |
| 129 | + [], |
| 130 | + true, |
| 131 | + [], |
| 132 | + [], |
| 133 | + '/', |
| 134 | + ), |
| 135 | + new Arguments, |
| 136 | + new Options, |
| 137 | + ); |
| 138 | + |
| 139 | + $console = $command($console); |
| 140 | + |
| 141 | + $this->assertSame( |
| 142 | + 1, |
| 143 | + $console->environment()->exitCode()->match( |
| 144 | + static fn($exit) => $exit->toInt(), |
| 145 | + static fn() => null, |
| 146 | + ), |
| 147 | + ); |
| 148 | + $this->assertSame( |
| 149 | + ["Graphviz needs to be installed first\n"], |
| 150 | + $console->environment()->outputs(), |
| 151 | + ); |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + public function testUsage() |
| 156 | + { |
| 157 | + $this |
| 158 | + ->forAll(Set\Strings::any()) |
| 159 | + ->then(function($usage) { |
| 160 | + $inner = new class($usage) implements Command { |
| 161 | + public function __construct(private string $usage) |
| 162 | + { |
| 163 | + } |
| 164 | + |
| 165 | + public function __invoke(Console $console): Console |
| 166 | + { |
| 167 | + return $console; |
| 168 | + } |
| 169 | + |
| 170 | + public function usage(): string |
| 171 | + { |
| 172 | + return $this->usage; |
| 173 | + } |
| 174 | + }; |
| 175 | + $processes = $this->createMock(Processes::class); |
| 176 | + $command = new CheckDotInstalled($inner, $processes); |
| 177 | + |
| 178 | + $this->assertSame( |
| 179 | + $usage, |
| 180 | + $command->usage(), |
| 181 | + ); |
| 182 | + }); |
| 183 | + } |
| 184 | +} |
0 commit comments