~ubuntu-branches/ubuntu/wily/php-doctrine-common/wily-proposed

« back to all changes in this revision

Viewing changes to tests/Doctrine/Tests/Common/ClassLoaderTest.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2014-06-15 11:26:00 UTC
  • mfrom: (2.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20140615112600-sg4mgpwq9sfg4mre
Tags: 2.4.2-2
* Upload to unstable
* No tests if DEB_BUILD_OPTIONS contains nocheck

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
namespace Doctrine\Tests\Common;
 
4
 
 
5
use Doctrine\Common\ClassLoader;
 
6
 
 
7
class ClassLoaderTest extends \Doctrine\Tests\DoctrineTestCase
 
8
{
 
9
    public function testClassLoader()
 
10
    {
 
11
        $classLoader = new ClassLoader('ClassLoaderTest');
 
12
        $classLoader->setIncludePath(__DIR__);
 
13
        $classLoader->setFileExtension('.class.php');
 
14
        $classLoader->setNamespaceSeparator('_');
 
15
 
 
16
        $this->assertTrue($classLoader->canLoadClass('ClassLoaderTest_ClassA'));
 
17
        $this->assertTrue($classLoader->canLoadClass('ClassLoaderTest_ClassB'));
 
18
        $this->assertTrue($classLoader->canLoadClass('ClassLoaderTest_ClassC'));
 
19
        $this->assertFalse($classLoader->canLoadClass('OtherClass'));
 
20
        $this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassA'), true);
 
21
        $this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassB'), true);
 
22
        $this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassC'), true);
 
23
    }
 
24
 
 
25
    public function testClassExists()
 
26
    {
 
27
        $this->assertFalse(ClassLoader::classExists('ClassLoaderTest\ClassD'));
 
28
        $badLoader = function($className) {
 
29
            require __DIR__ . '/ClassLoaderTest/ClassD.php';
 
30
            return true;
 
31
        };
 
32
        spl_autoload_register($badLoader);
 
33
        $this->assertTrue(ClassLoader::classExists('ClassLoaderTest\ClassD'));
 
34
        spl_autoload_unregister($badLoader);
 
35
    }
 
36
 
 
37
    public function testGetClassLoader()
 
38
    {
 
39
        $cl = new ClassLoader('ClassLoaderTest', __DIR__);
 
40
        $cl->register();
 
41
        $this->assertTrue(ClassLoader::getClassLoader('ClassLoaderTest\ClassD') instanceof \Doctrine\Common\ClassLoader);
 
42
        $this->assertNull(ClassLoader::getClassLoader('This\Class\Does\Not\Exist'));
 
43
        $cl->unregister();
 
44
    }
 
45
 
 
46
    public function testClassExistsWithSilentAutoloader()
 
47
    {
 
48
        $test = $this;
 
49
        $silentLoader = function ($className) use ($test) {
 
50
            $test->assertSame('ClassLoaderTest\ClassE', $className);
 
51
            require __DIR__ . '/ClassLoaderTest/ClassE.php';
 
52
        };
 
53
        $additionalLoader = function () use ($test) {
 
54
            $test->fail('Should not call this loader, class was already loaded');
 
55
        };
 
56
 
 
57
        $this->assertFalse(ClassLoader::classExists('ClassLoaderTest\ClassE'));
 
58
        spl_autoload_register($silentLoader);
 
59
        spl_autoload_register($additionalLoader);
 
60
        $this->assertTrue(ClassLoader::classExists('ClassLoaderTest\ClassE'));
 
61
        spl_autoload_unregister($additionalLoader);
 
62
        spl_autoload_unregister($silentLoader);
 
63
    }
 
64
}