You are here

class ClassLoaderTest in X Autoload 7.3

Same name and namespace in other branches
  1. 7.5 tests/src/ClassLoaderTest.php \Drupal\xautoload\Tests\ClassLoaderTest
  2. 7.4 tests/lib/ClassLoaderTest.php \Drupal\xautoload\Tests\ClassLoaderTest

Hierarchy

  • class \Drupal\xautoload\Tests\ClassLoaderTest extends \Drupal\xautoload\Tests\PHPUnit_Framework_TestCase

Expanded class hierarchy of ClassLoaderTest

File

tests/Drupal/xautoload/Tests/ClassLoaderTest.php, line 5

Namespace

Drupal\xautoload\Tests
View source
class ClassLoaderTest extends \PHPUnit_Framework_TestCase {

  /**
   * @var DummyFilesystem
   */
  protected $filesystem;
  function setUp() {
    parent::setUp();
    $this->filesystem = StreamWrapper::register('test');
  }
  function tearDown() {
    stream_wrapper_unregister('test');
    parent::tearDown();
  }

  //                                                                Test methods
  // ---------------------------------------------------------------------------

  /**
   * Test PSR-4-like namespaces.
   */
  function testPsr4() {

    // Prepare the class finder.
    $finder = new \xautoload_ClassFinder_NamespaceOrPrefix();
    $loader = new \xautoload_ClassLoader_NoCache($finder);
    $finder
      ->registerNamespacePlugin('Drupal\\ex_ample\\', new \xautoload_FinderPlugin_Psr4(), 'test://base/lib');
    $this
      ->assertLoadClass($loader, 'Drupal\\ex_ample\\Psr4\\Foo_Bar', 'test://base/lib/Psr4/Foo_Bar.php');
  }

  /**
   * Test PSR-0-like namespaces.
   */
  function testNamespaces() {

    // Prepare the class finder.
    $finder = new \xautoload_ClassFinder_NamespaceOrPrefix();
    $loader = new \xautoload_ClassLoader_NoCache($finder);
    $finder
      ->registerNamespaceDeep('Drupal\\ex_ample', 'test://base/lib');
    $finder
      ->registerNamespaceRoot('Drupal\\ex_ample', 'test://base/vendor');
    $this
      ->assertLoadClass($loader, 'Drupal\\ex_ample\\Shallow\\Foo_Bar', 'test://base/lib/Shallow/Foo/Bar.php');
    $this
      ->assertLoadClass($loader, 'Drupal\\ex_ample\\Sub\\Foo_Bar', 'test://base/vendor/Drupal/ex_ample/Sub/Foo/Bar.php');
  }

  /**
   * Test PEAR-like prefixes.
   */
  function testPrefixes() {

    // Prepare the class finder.
    $finder = new \xautoload_ClassFinder_NamespaceOrPrefix();
    $loader = new \xautoload_ClassLoader_NoCache($finder);
    $finder
      ->registerPrefixDeep('ex_ample', 'test://base/lib');
    $finder
      ->registerPrefixRoot('ex_ample', 'test://base/vendor');
    $this
      ->assertloadClass($loader, 'ex_ample_Sub_Foo', 'test://base/lib/Sub/Foo.php');
    $this
      ->assertloadClass($loader, 'ex_ample_Sub_Bar', 'test://base/vendor/ex/ample/Sub/Bar.php');
  }

  //                                                           Assertion helpers
  // ---------------------------------------------------------------------------

  /**
   * @param \xautoload_ClassLoader_Interface $loader
   * @param string $class
   * @param string $file
   */
  protected function assertLoadClass($loader, $class, $file) {

    // Register the class file in the virtual filesystem.
    $this->filesystem
      ->addClass($file, $class);

    // Check that the class is not already defined.
    $this
      ->assertFalse(class_exists($class, FALSE));

    // Trigger the class loader.
    $loader
      ->loadClass($class);

    // Check that the class is defined after the class loader has done its job.
    $this
      ->assertTrue(class_exists($class, FALSE));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ClassLoaderTest::$filesystem protected property
ClassLoaderTest::assertLoadClass protected function
ClassLoaderTest::setUp function
ClassLoaderTest::tearDown function
ClassLoaderTest::testNamespaces function Test PSR-0-like namespaces.
ClassLoaderTest::testPrefixes function Test PEAR-like prefixes.
ClassLoaderTest::testPsr4 function Test PSR-4-like namespaces.