Skip to content

Commit

Permalink
Add a test to check Infection works with PSR-0 compliant autoloader (#…
Browse files Browse the repository at this point in the history
…579)

* Add a test to check Infection works with PSR-0 compliant autoloader

* Add PSR-0
autloading to e2e tests
  • Loading branch information
maks-rafalko committed Dec 15, 2018
1 parent e9e4c93 commit d5e5eb2
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 1 deletion.
15 changes: 14 additions & 1 deletion tests/Console/E2ETest.php
Expand Up @@ -216,7 +216,7 @@ private function installComposerDeps(): void
*/

/*
* E2E tests are expected to follow PSR-4.
* E2E tests are expected to follow PSR-0 or PSR-4.
*
* We exploit this to autoload only classes belonging to the test,
* but not to vendored deps (so we don't need them here, but to run
Expand All @@ -243,6 +243,19 @@ private function installComposerDeps(): void
$loader->setPsr4($namespace, $paths);
}

$mapPsr0 = require 'vendor/composer/autoload_namespaces.php';

foreach ($mapPsr0 as $namespace => $paths) {
foreach ($paths as $path) {
if (strpos($path, $vendorDir) !== false) {
// Skip known dependency from autoloading
continue 2;
}
}

$loader->set($namespace, $paths);
}

$loader->register($prepend = false); // Note: not prepending, but appending to our autoloader

$this->previousLoader = $loader;
Expand Down
5 changes: 5 additions & 0 deletions tests/Fixtures/e2e/PSR_0_Autoloader/README.md
@@ -0,0 +1,5 @@
# PSR-0 test

* Related to https://github.com/infection/infection/issues/564

This test checks that Infection correctly works with PSR-0 compliant autoloader
15 changes: 15 additions & 0 deletions tests/Fixtures/e2e/PSR_0_Autoloader/composer.json
@@ -0,0 +1,15 @@
{
"require-dev": {
"phpunit/phpunit": "^6.5"
},
"autoload": {
"psr-0": {
"PSR_0_Autoloader": "src/"
}
},
"autoload-dev": {
"psr-0": {
"PSR_0_Autoloader\\Tests": "tests/"
}
}
}
6 changes: 6 additions & 0 deletions tests/Fixtures/e2e/PSR_0_Autoloader/expected-output.txt
@@ -0,0 +1,6 @@
Total: 1
Killed: 1
Errored: 0
Escaped: 0
Timed Out: 0
Not Covered: 0
12 changes: 12 additions & 0 deletions tests/Fixtures/e2e/PSR_0_Autoloader/infection.json
@@ -0,0 +1,12 @@
{
"timeout": 25,
"source": {
"directories": [
"src"
]
},
"logs": {
"summary": "infection.log"
},
"tmpDir": "."
}
18 changes: 18 additions & 0 deletions tests/Fixtures/e2e/PSR_0_Autoloader/phpunit.xml
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="./vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
</phpunit>
@@ -0,0 +1,11 @@
<?php

namespace PSR_0_Autoloader;

class SourceClass
{
public function hello(): string
{
return 'hello';
}
}
@@ -0,0 +1,15 @@
<?php

namespace PSR_0_Autoloader\Test;

use PSR_0_Autoloader\SourceClass;
use PHPUnit\Framework\TestCase;

class SourceClassTest extends TestCase
{
public function test_hello()
{
$sourceClass = new SourceClass();
$this->assertSame('hello', $sourceClass->hello());
}
}

0 comments on commit d5e5eb2

Please sign in to comment.