0

Follow up question from Autoloader for distributed PHP plugins that includes composer packages

I am writing an application that publishes distributed extensions for a CMS. Think of each distributed extension as a zip that contains the extension code and all its dependencies (which are copied composer packages).

In the package, there's an autoloader that loads it's dependencies.

I am using PHPUnit to test if the dependencies are loading correctly. But when instantiating the class in the test class, the dependencies are already loaded via composer.

I execute composer exec phpunit TestAutoloader in the test below. How can I write a test so that the dependency does not load from composer?

Test code

class TestAutoloader extends TestCase
{
    /**
     * @test
     */
    public function test_can_load_dependencies() {
        $myClass = new MyClass();

        $reflection = new \ReflectionClass($myClass->client());
        echo $reflection->getFileName(); // This is loading from vendors folder instead of uploads/dependency

        //$this->assertTrue(class_exists('GuzzleHttp\Client'));
    }
}

The problem illustrated

/upload/MyClass.php

should load

/upload/dependencies/example/src/MyDependency.php

but the test is loading from

/vendor/example/src/MyDependency.php
Community
  • 1
  • 1
JC Lee
  • 2,337
  • 2
  • 18
  • 25
  • Can't you make phpunit use a bootstrap (``), and define your own custom autoloader in the bootstrap file (instead of it using composer by default) – John Joseph Jan 31 '17 at 11:19
  • This sounds plausible but wouldn't that affect all other tests? Is it possible to target this specific test? – JC Lee Jan 31 '17 at 11:45

0 Answers0