Skip to content

Commit 36d4157

Browse files
committed
Adding tests
Signed-off-by: RJ Garcia <[email protected]>
1 parent 9911430 commit 36d4157

File tree

6 files changed

+51
-6
lines changed

6 files changed

+51
-6
lines changed

src/PhpIncPlugin.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,27 @@ public function onPreAutoloadDump(Event $event) {
4949
$package = $event->getComposer()->getPackage();
5050

5151
$rootDir = dirname($event->getComposer()->getConfig()->get('vendor-dir'));
52-
$phpIncConfig = $package->getExtra()['php-inc'] ?? $this->createDefaultConfig();
52+
$phpIncConfig = \array_merge($this->createDefaultConfig(), $package->getExtra()['php-inc'] ?? []);
5353

5454
$rootPrefix = $rootDir . '/';
5555

5656
$srcPath = $phpIncConfig['src-path'] ?? null;
57+
$srcDir = $srcPath ? $rootDir . '/' . $srcPath : null;
5758
$testPath = $phpIncConfig['test-path'] ?? null;
59+
$testDir = $testPath ? $rootDir . '/' . $testPath : null;
5860
$matches = $phpIncConfig['matches'] ?? null;
5961
$devSrcMatches = $phpIncConfig['matches-dev-src'] ?? null;
6062
$devTestMatches = $phpIncConfig['matches-dev-test'] ?? null;
6163

6264
$additionalAutoloadFiles = [];
6365
$additionalAutoloadDevFiles = [];
6466

65-
if ($srcPath) {
67+
if ($srcDir && \file_exists($srcDir)) {
6668
$sourceDir = $rootDir . '/' . $srcPath;
6769
$additionalAutoloadFiles = $this->matchFiles($rootPrefix, $matches, $sourceDir);
6870
$additionalAutoloadDevFiles = $this->matchFiles($rootPrefix, $devSrcMatches, $sourceDir);
6971
}
70-
if ($testPath) {
72+
if ($testPath && \file_exists($testDir)) {
7173
$testDir = $rootDir . '/' . $testPath;
7274
$additionalAutoloadDevFiles = \array_merge($additionalAutoloadDevFiles, $this->matchFiles($rootPrefix, $devTestMatches, $testDir));
7375
}
@@ -82,7 +84,7 @@ public function onPreAutoloadDump(Event $event) {
8284
$autoload['files'] = \array_unique(\array_merge($autoload['files'] ?? [], $additionalAutoloadFiles));
8385
$package->setAutoload($autoload);
8486
$autoloadDev = $package->getDevAutoload();
85-
$autoloadDev['files'] = \array_unique(\array_merge($autoload['files'] ?? [], $additionalAutoloadDevFiles));
87+
$autoloadDev['files'] = \array_unique(\array_merge($autoloadDev['files'] ?? [], $additionalAutoloadDevFiles));
8688
$package->setDevAutoload($autoloadDev);
8789
}
8890

@@ -135,7 +137,6 @@ private function createDefaultConfig(): array {
135137
]
136138
}
137139
}
138-
JSON
139-
);
140+
JSON, true);
140141
}
141142
}

test/fixtures/src/A.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php

test/fixtures/src/Tests/c.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php

test/fixtures/src/b.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php

test/fixtures/tests/d.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+

test/php-inc.spec.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,42 @@
3131
expect($matchedFiles)->equal($expectedFiles);
3232
});
3333
});
34+
35+
describe('PhpIncPlugin', function() {
36+
describe('preAutoLoadDump listener', function() {
37+
beforeEach(function() {
38+
$this->event = new \Composer\Script\Event(
39+
'preAutoLoadDump',
40+
(function() {
41+
$composer = new \Composer\Composer();
42+
$composer->setConfig((function() {
43+
$config = new \Composer\Config();
44+
$config->merge(['config' => ['vendor-dir' => __DIR__ . '/fixtures/vendor']]);
45+
return $config;
46+
})());
47+
$composer->setPackage(new \Composer\Package\RootPackage('Test Package', 'v0.1', 'Version 0.1'));
48+
return $composer;
49+
})(),
50+
new \Composer\IO\NullIO()
51+
);
52+
});
53+
54+
it('appends autoload and autoload-dev according to matched files', function() {
55+
$plugin = new PhpIncPlugin();
56+
$this->event->getComposer()->getPackage()->setAutoload(['files' => ['src/foo.php']]);
57+
$this->event->getComposer()->getPackage()->setDevAutoload(['files' => ['tests/bar.php']]);
58+
$plugin->onPreAutoloadDump($this->event);
59+
$package = $this->event->getComposer()->getPackage();
60+
expect($package->getAutoload()['files'])->equal(['src/foo.php', 'src/b.php']);
61+
expect($package->getDevAutoload()['files'])->equal(['tests/bar.php', 'src/Tests/c.php', 'tests/d.php']);
62+
});
63+
it('does not include if dir does not exist', function() {
64+
$plugin = new PhpIncPlugin();
65+
$this->event->getComposer()->getPackage()->setExtra(['php-inc' => ['test-path' => 'bad']]);
66+
$plugin->onPreAutoloadDump($this->event);
67+
$package = $this->event->getComposer()->getPackage();
68+
expect($package->getAutoload()['files'])->equal(['src/b.php']);
69+
expect($package->getDevAutoload()['files'])->equal(['src/Tests/c.php']);
70+
});
71+
});
72+
});

0 commit comments

Comments
 (0)