From 1b3a2ed6f62908952da14d96d163cdfe481f6b8d Mon Sep 17 00:00:00 2001 From: Giulio <84206373+giulio-Joshi@users.noreply.github.com> Date: Tue, 25 Oct 2022 16:38:01 +0200 Subject: [PATCH] test: Coverage for ValidateCommand (#11133) Co-authored-by: giulio-Joshi Co-authored-by: Jordi Boggiano --- .../Test/Command/ValidateCommandTest.php | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 tests/Composer/Test/Command/ValidateCommandTest.php diff --git a/tests/Composer/Test/Command/ValidateCommandTest.php b/tests/Composer/Test/Command/ValidateCommandTest.php new file mode 100644 index 000000000..b305edcae --- /dev/null +++ b/tests/Composer/Test/Command/ValidateCommandTest.php @@ -0,0 +1,150 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + + +namespace Composer\Test\Command; + +use Composer\Test\TestCase; +use Composer\Util\Platform; + +class ValidateCommandTest extends TestCase +{ + /** + * @dataProvider provideValidateTests + * @param array $composerJson + * @param array $command + */ + public function testValidate(array $composerJson, array $command, string $expected): void + { + $this->initTempComposer($composerJson); + + $appTester = $this->getApplicationTester(); + $appTester->run(array_merge(['command' => 'validate'], $command)); + + $this->assertSame(trim($expected), trim($appTester->getDisplay(true))); + } + + public function testValidateOnFileIssues(): void + { + $directory = $this->initTempComposer(self::MINIMAL_VALID_CONFIGURATION); + unlink($directory.'/composer.json'); + + $appTester = $this->getApplicationTester(); + $appTester->run(['command' => 'validate']); + $expected = './composer.json not found.'; + + $this->assertSame($expected, trim($appTester->getDisplay(true))); + } + + public function testWithComposerLock(): void + { + $this->initTempComposer(self::MINIMAL_VALID_CONFIGURATION); + $this->createComposerLock(); + + $appTester = $this->getApplicationTester(); + $appTester->run(['command' => 'validate']); + $expected = <<assertSame(trim($expected), trim($appTester->getDisplay(true))); + } + + public function testUnaccessibleFile(): void + { + if (Platform::isWindows()) { + $this->markTestSkipped('Does not run on windows'); + } + + $directory = $this->initTempComposer(self::MINIMAL_VALID_CONFIGURATION); + chmod($directory.'/composer.json', 0200); + + $appTester = $this->getApplicationTester(); + $appTester->run(['command' => 'validate']); + $expected = './composer.json is not readable.'; + + $this->assertSame($expected, trim($appTester->getDisplay(true))); + $this->assertSame(3, $appTester->getStatusCode()); + chmod($directory.'/composer.json', 0700); + } + + private const MINIMAL_VALID_CONFIGURATION = [ + 'name' => 'test/suite', + 'type' => 'library', + 'description' => 'A generical test suite', + 'license' => 'MIT', + 'repositories' => [ + 'packages' => [ + 'type' => 'package', + 'package' => [ + ['name' => 'root/req', 'version' => '1.0.0', 'require' => ['dep/pkg' => '^1']], + ['name' => 'dep/pkg', 'version' => '1.0.0'], + ['name' => 'dep/pkg', 'version' => '1.0.1'], + ['name' => 'dep/pkg', 'version' => '1.0.2'], + ], + ], + ], + 'require' => [ + 'root/req' => '1.*', + ], + ]; + + public function provideValidateTests(): \Generator + { + + yield 'validation passing' => [ + self::MINIMAL_VALID_CONFIGURATION, + [], + './composer.json is valid', + ]; + + $publishDataStripped= array_diff_key( + self::MINIMAL_VALID_CONFIGURATION, + ['name' => true, 'type' => true, 'description' => true, 'license' => true] + ); + + yield 'passing but with warnings' => [ + $publishDataStripped, + [], + <<See https://getcomposer.org/doc/04-schema.md for details on the schema +# Publish errors +- name : The property name is required +- description : The property description is required +# General warnings +- No license specified, it is recommended to do so. For closed-source software you may use "proprietary" as license. +OUTPUT + ]; + + yield 'passing without publish-check' => [ + $publishDataStripped, + [ '--no-check-publish' => true], + <<See https://getcomposer.org/doc/04-schema.md for details on the schema +# General warnings +- No license specified, it is recommended to do so. For closed-source software you may use "proprietary" as license. +# Publish warnings +- name : The property name is required +- description : The property description is required +OUTPUT + ]; + } +}