Skip to content

Commit

Permalink
Updated and expanded automated tests to cover config object
Browse files Browse the repository at this point in the history
  • Loading branch information
coenjacobs committed Sep 6, 2024
1 parent eb99bd7 commit c906eeb
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 13 deletions.
22 changes: 22 additions & 0 deletions tests/Config/ConfigMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use CoenJacobs\Mozart\Config\Composer;
use CoenJacobs\Mozart\Config\Mozart;
use PHPUnit\Framework\TestCase;

class ConfigMapperTest extends TestCase
{
/**
* @test
*/
#[Test]
public function it_creates_a_valid_config_object_based_on_composer_file()
{
$config = Composer::loadFromFile(__DIR__ . '/config-mapper-test.json');
$this->assertInstanceOf(Composer::class, $config);
$this->assertInstanceOf(Mozart::class, $config->getExtra()->getMozart());
$this->assertCount(4, $config->autoload->getAutoloaders());
}
}
31 changes: 31 additions & 0 deletions tests/Config/config-mapper-test.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
6 changes: 0 additions & 6 deletions tests/Console/Commands/ComposeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
67 changes: 67 additions & 0 deletions tests/Integration/ExcludedPackagesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);

use CoenJacobs\Mozart\Console\Commands\Compose;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ExcludedPackagesTest extends TestCase {
private string $testsWorkingDir;

public function setUp(): void
{
parent::setUp();

$this->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__);
}
}
26 changes: 26 additions & 0 deletions tests/Integration/excluded-packages.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
15 changes: 9 additions & 6 deletions tests/MoverTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

use CoenJacobs\Mozart\Composer\Config;
use CoenJacobs\Mozart\Config\Mozart;
use CoenJacobs\Mozart\Composer\Package;
use CoenJacobs\Mozart\Console\Commands\Compose;
use CoenJacobs\Mozart\Mover;
Expand All @@ -12,7 +12,6 @@

class MoverTest extends TestCase
{

/**
* A temporary directory for creating and deleting files for these tests.
*
Expand All @@ -23,7 +22,7 @@ class MoverTest extends TestCase
/**
* composer->extra->mozart settings
*
* @var Config
* @var Mozart
*/
protected $config;

Expand Down Expand Up @@ -55,7 +54,7 @@ public function setUp(): void
),
);

$this->config = new Config( $configArgs );
$this->config = Mozart::loadFromString( json_encode($configArgs) );
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/replacers/NamespaceReplacerTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

use CoenJacobs\Mozart\Composer\Autoload\Psr0;
use CoenJacobs\Mozart\Config\Psr0;
use CoenJacobs\Mozart\Replace\NamespaceReplacer;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\Test;
Expand Down

0 comments on commit c906eeb

Please sign in to comment.