Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from Dec 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}

sanmai marked this conversation as resolved.
Show resolved Hide resolved
$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
Copy link
Member

@sanmai sanmai Nov 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's just as same as the current PSR-4. I think if test best to test a really old-style Namespace_Class_Name style project.

For example, I could not run Infection on pear/OLE. It gives:

Processing source code files: 4/7
In CodeCoverageClassIgnoreVisitor.php line 63:
                                 
  Class OLE_Test does not exist  

Even though composer.json has all relevant definitions:

"autoload-dev": {
    "psr-0": {
        "OLE": "tests/"
    }
},

I even made a simple test to verify that indeed composer picks up OLE_Test:

class OLE_AutoloadTest extends PHPUnit_Framework_TestCase
{
    public function testOtherTestExists()
    {
		$this->assertTrue(class_exists(OLE_Test::class));
    }
}

On subsequent runs, for some very interesting reason:

In JsonValidationException.php line 51:
                                                                                            
  "OLE/infection.json.dist" does not match the expected JSON schema:  
   - source.excludes : Object value found, but an array is required                         

The last error possibly related to these configuration steps:

Any directories to exclude from within your source directories?: tests
Any directories to exclude from within your source directories?: coverage
Any directories to exclude from within your source directories?: build
Any directories to exclude from within your source directories?: vendor
Any directories to exclude from within your source directories?: 

infection.json has this:

 "excludes": {
     "0": "vendor",
     "1": "tests",
     "3": "coverage",
     "4": "build"
 }

Filed #580

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, that's not an unknown issue with "Class OLE_Test does not exist", just #576

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, that's not an unknown issue with "Class OLE_Test does not exist", just #576

ok, I'm merging this one since it's not related to any issue, this is only a new test

{
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());
}
}