-
Notifications
You must be signed in to change notification settings - Fork 0
test: improving code coverage for SatisCommandBuilder and LockProcessor #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
96d94b3
e10ae4f
abb0e37
53dcbfc
6948897
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
|
|
||
| namespace App\Process; | ||
|
|
||
| class EnvironmentProvider | ||
| { | ||
| public function __construct( | ||
| private readonly string $composerHome, | ||
| ) { | ||
| } | ||
|
|
||
| public function getEnv(): array | ||
| { | ||
| $env = []; | ||
|
|
||
| foreach ($_SERVER as $key => $value) { | ||
| if (\is_string($value) && false !== $envValue = \getenv($key)) { | ||
| $env[$key] = $envValue; | ||
| } | ||
| } | ||
|
|
||
| foreach ($_ENV as $key => $value) { | ||
| if (\is_string($value)) { | ||
| $env[$key] = $value; | ||
| } | ||
| } | ||
|
|
||
| $env['COMPOSER_HOME'] ??= $this->composerHome; | ||
| $env['COMPOSER_NO_INTERACTION'] = '1'; | ||
|
|
||
| return $env; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| <?php | ||
|
|
||
| namespace App\Tests\Process; | ||
|
|
||
| use App\Process\EnvironmentProvider; | ||
| use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
| use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | ||
|
|
||
| class EnvironmentProviderTest extends KernelTestCase | ||
| { | ||
| protected function tearDown(): void | ||
| { | ||
| \putenv('TEST_KEY'); | ||
| unset($_SERVER['TEST_KEY'], $_ENV['TEST_ENV_KEY']); | ||
| } | ||
|
|
||
| public function testGetEnvSetsComposerDefaults(): void | ||
| { | ||
| $parameterBag = static::getContainer()->get(ParameterBagInterface::class); | ||
| $provider = new EnvironmentProvider($parameterBag->get('composer.home')); | ||
|
|
||
| $env = $provider->getEnv(); | ||
|
|
||
| $this->assertSame('/app/var/composer', $env['COMPOSER_HOME']); | ||
| $this->assertSame('1', $env['COMPOSER_NO_INTERACTION']); | ||
| } | ||
|
|
||
| public function testGetEnvIncludesServerVariables(): void | ||
| { | ||
| $_SERVER['TEST_KEY'] = 'value'; | ||
| \putenv('TEST_KEY=value'); | ||
|
|
||
| $parameterBag = static::getContainer()->get(ParameterBagInterface::class); | ||
| $provider = new EnvironmentProvider($parameterBag->get('composer.home')); | ||
|
|
||
| $env = $provider->getEnv(); | ||
|
|
||
| $this->assertArrayHasKey('TEST_KEY', $env); | ||
| $this->assertSame('value', $env['TEST_KEY']); | ||
| } | ||
|
|
||
| public function testGetEnvIncludesEnvVariables(): void | ||
| { | ||
| $_ENV['TEST_ENV_KEY'] = 'env_value'; | ||
|
|
||
| $parameterBag = static::getContainer()->get(ParameterBagInterface::class); | ||
| $provider = new EnvironmentProvider($parameterBag->get('composer.home')); | ||
|
|
||
| $env = $provider->getEnv(); | ||
|
|
||
| $this->assertArrayHasKey('TEST_ENV_KEY', $env); | ||
| $this->assertSame('env_value', $env['TEST_ENV_KEY']); | ||
| } | ||
|
|
||
| public function testComposerHomeNotOverwrittenIfProvidedInEnv(): void | ||
| { | ||
| \putenv('COMPOSER_HOME=/app/var/composer'); | ||
| $_SERVER['COMPOSER_HOME'] = '/app/var/composer'; | ||
|
|
||
| $parameterBag = static::getContainer()->get(ParameterBagInterface::class); | ||
| $provider = new EnvironmentProvider($parameterBag->get('composer.home')); | ||
|
|
||
| $env = $provider->getEnv(); | ||
|
|
||
| $this->assertSame('/app/var/composer', $env['COMPOSER_HOME']); | ||
| } | ||
|
Comment on lines
+55
to
+66
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test assertion doesn't prove the "not overwritten" behavior. The test sets public function testComposerHomeNotOverwrittenIfProvidedInEnv(): void
{
- \putenv('COMPOSER_HOME=/app/var/composer');
- $_SERVER['COMPOSER_HOME'] = '/app/var/composer';
+ \putenv('COMPOSER_HOME=/custom/composer/path');
+ $_SERVER['COMPOSER_HOME'] = '/custom/composer/path';
$parameterBag = static::getContainer()->get(ParameterBagInterface::class);
$provider = new EnvironmentProvider($parameterBag->get('composer.home'));
$env = $provider->getEnv();
- $this->assertSame('/app/var/composer', $env['COMPOSER_HOME']);
+ $this->assertSame('/custom/composer/path', $env['COMPOSER_HOME']);
}🤖 Prompt for AI Agents |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| <?php | ||
|
|
||
| namespace App\Tests\Process; | ||
|
|
||
| use App\Process\EnvironmentProvider; | ||
| use App\Process\ProcessFactory; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\Process\Process; | ||
|
|
||
| class ProcessFactoryTest extends TestCase | ||
| { | ||
| public function testCreateReturnsConfiguredProcess(): void | ||
| { | ||
| $env = ['A' => 'B']; | ||
|
|
||
| $provider = $this->createMock(EnvironmentProvider::class); | ||
| $provider->method('getEnv')->willReturn($env); | ||
|
|
||
| $factory = new ProcessFactory('/root', $provider); | ||
|
|
||
| $process = $factory->create(['bin/tool', 'arg1', 'arg2'], 123); | ||
|
|
||
| $this->assertInstanceOf(Process::class, $process); | ||
| $this->assertSame('\'/root/bin/tool\' \'arg1\' \'arg2\'', $process->getCommandLine()); | ||
| $this->assertSame('/root', $process->getWorkingDirectory()); | ||
| $this->assertSame($env, $process->getEnv()); | ||
| $this->assertSame(123.0, $process->getTimeout()); | ||
| } | ||
|
|
||
| public function testCreateThrowsOnEmptyCommand(): void | ||
| { | ||
| $provider = $this->createMock(EnvironmentProvider::class); | ||
| $factory = new ProcessFactory('/root', $provider); | ||
|
|
||
| $this->expectException(\InvalidArgumentException::class); | ||
|
|
||
| $factory->create([]); | ||
| } | ||
|
|
||
| public function testCommandIsPrefixedWithRootPath(): void | ||
| { | ||
| $provider = $this->createMock(EnvironmentProvider::class); | ||
| $provider->method('getEnv')->willReturn([]); | ||
|
|
||
| $factory = new ProcessFactory('/abc', $provider); | ||
|
|
||
| $process = $factory->create(['vendor/bin/satis']); | ||
|
|
||
| $this->assertSame('\'/abc/vendor/bin/satis\'', $process->getCommandLine()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| <?php | ||
|
|
||
| namespace App\Tests\Service; | ||
|
|
||
| use App\DTO\Repository; | ||
| use App\Service\LockProcessor; | ||
| use App\Service\RepositoryManager; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class LockProcessorTest extends TestCase | ||
| { | ||
| public function testProcessFileAddsRepositories(): void | ||
| { | ||
| $json = <<<JSON | ||
| { | ||
| "packages": [ | ||
| { | ||
| "name": "vendor/package1", | ||
| "source": { "url": "https://example.com/repo1.git", "type": "git" } | ||
| } | ||
| ], | ||
| "packages-dev": [ | ||
| { | ||
| "name": "vendor/package2", | ||
| "source": { "url": "https://example.com/repo2.git", "type": "git" } | ||
| } | ||
| ] | ||
| } | ||
| JSON; | ||
|
|
||
| $tmpFile = \tmpfile(); | ||
| \fwrite($tmpFile, $json); | ||
| $path = \stream_get_meta_data($tmpFile)['uri']; | ||
| $file = new \SplFileObject($path); | ||
|
|
||
| $manager = $this->createMock(RepositoryManager::class); | ||
| $manager->expects($this->once()) | ||
| ->method('addAll') | ||
| ->with($this->callback(function ($repositories) { | ||
| return 2 === \count($repositories) | ||
| && $repositories[0] instanceof Repository | ||
| && 'https://example.com/repo1.git' === $repositories[0]->getUrl() | ||
| && 'git' === $repositories[0]->getType() | ||
| && $repositories[1] instanceof Repository | ||
| && 'https://example.com/repo2.git' === $repositories[1]->getUrl() | ||
| && 'git' === $repositories[1]->getType(); | ||
| })); | ||
|
|
||
| $processor = new LockProcessor($manager); | ||
| $processor->processFile($file); | ||
|
|
||
| \fclose($tmpFile); | ||
| } | ||
|
|
||
| public function testGetRepositoriesFiltersInvalidPackages(): void | ||
| { | ||
| $manager = $this->createMock(RepositoryManager::class); | ||
| $processor = new LockProcessor($manager); | ||
|
|
||
| $packages = [ | ||
| (object) [ | ||
| 'name' => 'vendor/valid', | ||
| 'source' => (object) ['url' => 'https://example.com/repo.git', 'type' => 'git'], | ||
| ], | ||
| (object) [ | ||
| 'name' => 'vendor/invalid', | ||
| 'source' => (object) ['url' => '', 'type' => ''], | ||
| ], | ||
| (object) [ | ||
| 'name' => 'vendor/nosource', | ||
| ], | ||
| ]; | ||
|
|
||
| $method = new \ReflectionMethod(LockProcessor::class, 'getRepositories'); | ||
| $repositories = $method->invoke($processor, $packages); | ||
|
|
||
| $this->assertCount(1, $repositories); | ||
| $this->assertSame('https://example.com/repo.git', $repositories[0]->getUrl()); | ||
| $this->assertSame('git', $repositories[0]->getType()); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing cleanup for
COMPOSER_HOMEin tearDown.testComposerHomeNotOverwrittenIfProvidedInEnvsetsCOMPOSER_HOMEviaputenv()and$_SERVER(lines 57-58), buttearDown()doesn't clean these up. This could leak state to subsequent tests.protected function tearDown(): void { \putenv('TEST_KEY'); - unset($_SERVER['TEST_KEY'], $_ENV['TEST_ENV_KEY']); + \putenv('COMPOSER_HOME'); + unset($_SERVER['TEST_KEY'], $_SERVER['COMPOSER_HOME'], $_ENV['TEST_ENV_KEY']); }📝 Committable suggestion
🤖 Prompt for AI Agents