|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpSchool\PhpWorkshopTest\Process; |
| 4 | + |
| 5 | +use PhpSchool\PhpWorkshop\Process\DockerProcessFactory; |
| 6 | +use PhpSchool\PhpWorkshop\Process\ProcessInput; |
| 7 | +use PhpSchool\PhpWorkshop\Process\ProcessNotFoundException; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Symfony\Component\Process\ExecutableFinder; |
| 10 | + |
| 11 | +class DockerProcessFactoryTest extends TestCase |
| 12 | +{ |
| 13 | + public function testCreateThrowsExceptionIfDockerNotFound(): void |
| 14 | + { |
| 15 | + static::expectException(ProcessNotFoundException::class); |
| 16 | + |
| 17 | + $finder = $this->createMock(ExecutableFinder::class); |
| 18 | + $finder->expects($this->once()) |
| 19 | + ->method('find') |
| 20 | + ->with('docker') |
| 21 | + ->willReturn(null); |
| 22 | + |
| 23 | + $factory = new DockerProcessFactory('/docker-dir', 'php8appreciate', '/composer/cache/dir', $finder); |
| 24 | + $input = new ProcessInput('composer', [], __DIR__, []); |
| 25 | + |
| 26 | + $factory->create($input); |
| 27 | + } |
| 28 | + |
| 29 | + public function testCreate(): void |
| 30 | + { |
| 31 | + $finder = $this->createMock(ExecutableFinder::class); |
| 32 | + $finder->expects($this->once()) |
| 33 | + ->method('find') |
| 34 | + ->with('docker') |
| 35 | + ->willReturn('/usr/local/bin/docker'); |
| 36 | + |
| 37 | + $factory = new DockerProcessFactory('/docker-dir', 'php8appreciate', '/composer/cache/dir', $finder); |
| 38 | + $input = new ProcessInput('php', [], __DIR__, []); |
| 39 | + |
| 40 | + $process = $factory->create($input); |
| 41 | + static::assertSame("'/usr/local/bin/docker' 'compose' '-p' 'php8appreciate' '-f' '.docker/runtime/docker-compose.yml' 'run' '--rm' '-w' '/solution' 'runtime' 'php'", $process->getCommandLine()); |
| 42 | + static::assertSame('/docker-dir', $process->getWorkingDirectory()); |
| 43 | + } |
| 44 | + |
| 45 | + public function testCreateMountsComposerCacheDirIfExecutableIsComposer(): void |
| 46 | + { |
| 47 | + $finder = $this->createMock(ExecutableFinder::class); |
| 48 | + $finder->expects($this->once()) |
| 49 | + ->method('find') |
| 50 | + ->with('docker') |
| 51 | + ->willReturn('/usr/local/bin/docker'); |
| 52 | + |
| 53 | + $factory = new DockerProcessFactory('/docker-dir', 'php8appreciate', '/composer/cache/dir', $finder); |
| 54 | + $input = new ProcessInput('composer', [], __DIR__, []); |
| 55 | + |
| 56 | + $process = $factory->create($input); |
| 57 | + static::assertSame("'/usr/local/bin/docker' 'compose' '-p' 'php8appreciate' '-f' '.docker/runtime/docker-compose.yml' 'run' '--rm' '-w' '/solution' '-v' '/composer/cache/dir:/root/.composer/cache' 'runtime' 'composer'", $process->getCommandLine()); |
| 58 | + static::assertSame('/docker-dir', $process->getWorkingDirectory()); |
| 59 | + } |
| 60 | + |
| 61 | + public function testCreateWithArgs(): void |
| 62 | + { |
| 63 | + $finder = $this->createMock(ExecutableFinder::class); |
| 64 | + $finder->expects($this->once()) |
| 65 | + ->method('find') |
| 66 | + ->with('docker') |
| 67 | + ->willReturn('/usr/local/bin/docker'); |
| 68 | + |
| 69 | + $factory = new DockerProcessFactory('/docker-dir', 'php8appreciate', '/composer/cache/dir', $finder); |
| 70 | + $input = new ProcessInput('php', ['one', 'two'], __DIR__, []); |
| 71 | + |
| 72 | + $process = $factory->create($input); |
| 73 | + static::assertSame("'/usr/local/bin/docker' 'compose' '-p' 'php8appreciate' '-f' '.docker/runtime/docker-compose.yml' 'run' '--rm' '-w' '/solution' 'runtime' 'php' 'one' 'two'", $process->getCommandLine()); |
| 74 | + static::assertSame('/docker-dir', $process->getWorkingDirectory()); |
| 75 | + } |
| 76 | + |
| 77 | + public function testCreateWithEnv(): void |
| 78 | + { |
| 79 | + $finder = $this->createMock(ExecutableFinder::class); |
| 80 | + $finder->expects($this->once()) |
| 81 | + ->method('find') |
| 82 | + ->with('docker') |
| 83 | + ->willReturn('/usr/local/bin/docker'); |
| 84 | + |
| 85 | + $factory = new DockerProcessFactory('/docker-dir', 'php8appreciate', '/composer/cache/dir', $finder); |
| 86 | + $input = new ProcessInput('php', ['one', 'two'], __DIR__, ['SOME_VAR' => 'value']); |
| 87 | + |
| 88 | + $process = $factory->create($input); |
| 89 | + static::assertSame("'/usr/local/bin/docker' 'compose' '-p' 'php8appreciate' '-f' '.docker/runtime/docker-compose.yml' 'run' '--rm' '-e SOME_VAR=value' '-w' '/solution' 'runtime' 'php' 'one' 'two'", $process->getCommandLine()); |
| 90 | + static::assertSame('/docker-dir', $process->getWorkingDirectory()); |
| 91 | + } |
| 92 | + |
| 93 | + public function testWithInput(): void |
| 94 | + { |
| 95 | + $finder = $this->createMock(ExecutableFinder::class); |
| 96 | + $finder->expects($this->once()) |
| 97 | + ->method('find') |
| 98 | + ->with('docker') |
| 99 | + ->willReturn('/usr/local/bin/docker'); |
| 100 | + |
| 101 | + $factory = new DockerProcessFactory('/composer-dir', 'php8appreciate', '/composer/cache/dir', $finder); |
| 102 | + $input = new ProcessInput('php', [], __DIR__, [], 'someinput'); |
| 103 | + |
| 104 | + $process = $factory->create($input); |
| 105 | + static::assertSame("'/usr/local/bin/docker' 'compose' '-p' 'php8appreciate' '-f' '.docker/runtime/docker-compose.yml' 'run' '--rm' '-w' '/solution' 'runtime' 'php'", $process->getCommandLine()); |
| 106 | + static::assertSame('someinput', $process->getInput()); |
| 107 | + } |
| 108 | + |
| 109 | + public function testSolutionDirectoryIsPassedAsEnvVar(): void |
| 110 | + { |
| 111 | + $finder = $this->createMock(ExecutableFinder::class); |
| 112 | + $finder->expects($this->once()) |
| 113 | + ->method('find') |
| 114 | + ->with('docker') |
| 115 | + ->willReturn('/usr/local/bin/docker'); |
| 116 | + |
| 117 | + $factory = new DockerProcessFactory('/docker-dir', 'php8appreciate', '/composer/cache/dir', $finder); |
| 118 | + $input = new ProcessInput('php', ['one', 'two'], __DIR__, ['SOME_VAR' => 'value']); |
| 119 | + |
| 120 | + $process = $factory->create($input); |
| 121 | + static::assertSame("'/usr/local/bin/docker' 'compose' '-p' 'php8appreciate' '-f' '.docker/runtime/docker-compose.yml' 'run' '--rm' '-e SOME_VAR=value' '-w' '/solution' 'runtime' 'php' 'one' 'two'", $process->getCommandLine()); |
| 122 | + static::assertSame('/docker-dir', $process->getWorkingDirectory()); |
| 123 | + static::assertSame(['SOLUTION' => __DIR__], $process->getEnv()); |
| 124 | + } |
| 125 | +} |
0 commit comments