From c906eeba0693581dcc71a62a9816e6ab50fb74a8 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Fri, 6 Sep 2024 13:52:54 +0200 Subject: [PATCH] Updated and expanded automated tests to cover config object --- tests/Config/ConfigMapperTest.php | 22 +++++++ tests/Config/config-mapper-test.json | 31 ++++++++++ tests/Console/Commands/ComposeTest.php | 6 -- tests/Integration/ExcludedPackagesTest.php | 67 ++++++++++++++++++++++ tests/Integration/excluded-packages.json | 26 +++++++++ tests/MoverTest.php | 15 +++-- tests/replacers/NamespaceReplacerTest.php | 2 +- 7 files changed, 156 insertions(+), 13 deletions(-) create mode 100644 tests/Config/ConfigMapperTest.php create mode 100644 tests/Config/config-mapper-test.json create mode 100644 tests/Integration/ExcludedPackagesTest.php create mode 100644 tests/Integration/excluded-packages.json diff --git a/tests/Config/ConfigMapperTest.php b/tests/Config/ConfigMapperTest.php new file mode 100644 index 00000000..e548135e --- /dev/null +++ b/tests/Config/ConfigMapperTest.php @@ -0,0 +1,22 @@ +assertInstanceOf(Composer::class, $config); + $this->assertInstanceOf(Mozart::class, $config->getExtra()->getMozart()); + $this->assertCount(4, $config->autoload->getAutoloaders()); + } +} diff --git a/tests/Config/config-mapper-test.json b/tests/Config/config-mapper-test.json new file mode 100644 index 00000000..1f53c4ad --- /dev/null +++ b/tests/Config/config-mapper-test.json @@ -0,0 +1,31 @@ +{ + "require": { + "pimple/pimple": "^3.5" + }, + "autoload": { + "psr-0": { + "Mozart\\RandomDir\\": "old_files/", + "Mozart\\Multiples\\": [ "another_dir", "more_dirs" ] + }, + "psr-4": { + "Mozart\\TestProject\\": "src/", + "Mozart\\MultipleDirs\\": [ "packages/test/src/", "packages/more/src/" ] + } + }, + "extra": { + "mozart": { + "dep_namespace": "Mozart\\TestProject\\Dependencies", + "dep_directory": "/src/dependencies", + "classmap_directory": "/classes/", + "classmap_prefix": "MozartDependency_", + "packages": [ + "pimple/pimple" + ], + "excluded_packages": [ + ], + "override_autoload": { + }, + "delete_vendor_directories": true + } + } + } diff --git a/tests/Console/Commands/ComposeTest.php b/tests/Console/Commands/ComposeTest.php index ea283cfc..325ea3bc 100644 --- a/tests/Console/Commands/ComposeTest.php +++ b/tests/Console/Commands/ComposeTest.php @@ -39,7 +39,6 @@ public function setUp(): void #[Test] public function it_fails_gracefully_when_composer_json_absent(): void { - $inputInterfaceMock = $this->createMock(InputInterface::class); $outputInterfaceMock = $this->createMock(OutputInterface::class); @@ -66,7 +65,6 @@ public function __construct($inputInterfaceMock, $outputInterfaceMock) #[Test] public function it_handles_malformed_json_with_grace(): void { - $badComposerJson = '{ "name": "coenjacobs/mozart", }'; file_put_contents(__DIR__ . '/composer.json', $badComposerJson); @@ -97,7 +95,6 @@ public function __construct($inputInterfaceMock, $outputInterfaceMock) #[Test] public function it_handles_absent_extra_config_with_grace(): void { - $badComposerJson = '{ "name": "coenjacobs/mozart" }'; file_put_contents(__DIR__ . '/composer.json', $badComposerJson); @@ -129,7 +126,6 @@ public function __construct($inputInterfaceMock, $outputInterfaceMock) #[Test] public function it_handles_malformed_extra_config_with_grace(): void { - $badComposerJson = '{ "name": "coenjacobs/mozart", "extra": [] }'; file_put_contents(__DIR__ . '/composer.json', $badComposerJson); @@ -160,7 +156,6 @@ public function __construct($inputInterfaceMock, $outputInterfaceMock) #[Test] public function it_handles_absent_mozart_config_with_grace(): void { - $badComposerJson = '{ "name": "coenjacobs/mozart", "extra": { "moozart": {} } }'; file_put_contents(__DIR__ . '/composer.json', $badComposerJson); @@ -193,7 +188,6 @@ public function __construct($inputInterfaceMock, $outputInterfaceMock) #[Test] public function it_handles_malformed_mozart_config__with_grace(): void { - $badComposerJson = '{ "name": "coenjacobs/mozart", "extra": { "mozart": [] } }'; file_put_contents(__DIR__ . '/composer.json', $badComposerJson); diff --git a/tests/Integration/ExcludedPackagesTest.php b/tests/Integration/ExcludedPackagesTest.php new file mode 100644 index 00000000..6d022b85 --- /dev/null +++ b/tests/Integration/ExcludedPackagesTest.php @@ -0,0 +1,67 @@ +testsWorkingDir = __DIR__ . '/temptestdir'; + if (!file_exists($this->testsWorkingDir)) { + mkdir($this->testsWorkingDir); + } + } + + /** + * @test + */ + #[Test] + public function it_excludes_handling_specified_packages(): void + { + copy(__DIR__ . '/excluded-packages.json', $this->testsWorkingDir . '/composer.json'); + + chdir($this->testsWorkingDir); + + exec('composer update'); + + $inputInterfaceMock = $this->createMock(InputInterface::class); + $outputInterfaceMock = $this->createMock(OutputInterface::class); + + $mozartCompose = new Compose(); + + $result = $mozartCompose->run($inputInterfaceMock, $outputInterfaceMock); + $this->assertEquals(0, $result); + $this->assertDirectoryDoesNotExist($this->testsWorkingDir . '/vendor/pimple/pimple'); + $this->assertDirectoryExists($this->testsWorkingDir . '/vendor/psr/container'); + } + + public function tearDown(): void + { + parent::tearDown(); + + $dir = $this->testsWorkingDir; + + $it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS); + $files = new RecursiveIteratorIterator( + $it, + RecursiveIteratorIterator::CHILD_FIRST + ); + foreach ($files as $file) { + if ($file->isDir()) { + rmdir($file->getRealPath()); + } else { + unlink($file->getRealPath()); + } + } + rmdir($dir); + chdir(__DIR__); + } +} diff --git a/tests/Integration/excluded-packages.json b/tests/Integration/excluded-packages.json new file mode 100644 index 00000000..f0165703 --- /dev/null +++ b/tests/Integration/excluded-packages.json @@ -0,0 +1,26 @@ +{ + "require": { + "pimple/pimple": "^3.5" + }, + "autoload": { + "psr-4": { + "Mozart\\TestProject\\": "src/" + } + }, + "extra": { + "mozart": { + "dep_namespace": "Mozart\\TestProject\\Dependencies", + "dep_directory": "/src/dependencies", + "classmap_directory": "/classes/", + "classmap_prefix": "MozartDependency_", + "packages": [ + "pimple/pimple" + ], + "excluded_packages": [ + "psr/container" + ], + "override_autoload": {}, + "delete_vendor_directories": true + } + } +} diff --git a/tests/MoverTest.php b/tests/MoverTest.php index b0f4a00f..1d712993 100644 --- a/tests/MoverTest.php +++ b/tests/MoverTest.php @@ -1,7 +1,7 @@ extra->mozart settings * - * @var Config + * @var Mozart */ protected $config; @@ -55,7 +54,7 @@ public function setUp(): void ), ); - $this->config = new Config( $configArgs ); + $this->config = Mozart::loadFromString( json_encode($configArgs) ); } /** @@ -135,8 +134,12 @@ public function it_deletes_subdirs_for_packages_about_to_be_moved(): void $testDummyComposerContents = json_encode(new stdClass()); file_put_contents($testDummyComposerPath, $testDummyComposerContents); - $parsedPackage = new Package($testDummyComposerDir, $this->config, $this->config->get('override_autoload')[$packageString]); - $parsedPackage->findAutoloaders(); + + $overrideAutoload = $this->config->get('override_autoload'); + if ( ! empty( $overrideAutoload ) ) { + $overrideAutoload = $overrideAutoload->getByKey( $packageString ); + } + $parsedPackage = new Package($testDummyComposerDir, null, $overrideAutoload ); $packages[] = $parsedPackage; } diff --git a/tests/replacers/NamespaceReplacerTest.php b/tests/replacers/NamespaceReplacerTest.php index f5856ede..7694c44d 100644 --- a/tests/replacers/NamespaceReplacerTest.php +++ b/tests/replacers/NamespaceReplacerTest.php @@ -1,7 +1,7 @@