* @license MIT * * @covers \ProxyManager\Autoloader\Autoloader * @group Coverage */ class AutoloaderTest extends PHPUnit_Framework_TestCase { /** * @var \ProxyManager\Autoloader\Autoloader */ protected $autoloader; /** * @var \ProxyManager\FileLocator\FileLocatorInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $fileLocator; /** * @var \ProxyManager\Inflector\ClassNameInflectorInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $classNameInflector; /** * @covers \ProxyManager\Autoloader\Autoloader::__construct */ public function setUp() { $this->fileLocator = $this->getMock('ProxyManager\\FileLocator\\FileLocatorInterface'); $this->classNameInflector = $this->getMock('ProxyManager\\Inflector\\ClassNameInflectorInterface'); $this->autoloader = new Autoloader($this->fileLocator, $this->classNameInflector); } /** * @covers \ProxyManager\Autoloader\Autoloader::__invoke */ public function testWillNotAutoloadUserClasses() { $className = 'Foo\\' . UniqueIdentifierGenerator::getIdentifier('Bar'); $this ->classNameInflector ->expects($this->once()) ->method('isProxyClassName') ->with($className) ->will($this->returnValue(false)); $this->assertFalse($this->autoloader->__invoke($className)); } /** * @covers \ProxyManager\Autoloader\Autoloader::__invoke */ public function testWillNotAutoloadNonExistingClass() { $className = 'Foo\\' . UniqueIdentifierGenerator::getIdentifier('Bar'); $this ->classNameInflector ->expects($this->once()) ->method('isProxyClassName') ->with($className) ->will($this->returnValue(true)); $this ->fileLocator ->expects($this->once()) ->method('getProxyFileName') ->will($this->returnValue(__DIR__ . '/non-existing')); $this->assertFalse($this->autoloader->__invoke($className)); } /** * @covers \ProxyManager\Autoloader\Autoloader::__invoke */ public function testWillNotAutoloadExistingClass() { $this->assertFalse($this->autoloader->__invoke(__CLASS__)); } /** * @covers \ProxyManager\Autoloader\Autoloader::__invoke */ public function testWillAutoloadExistingFile() { $namespace = 'Foo'; $className = UniqueIdentifierGenerator::getIdentifier('Bar'); $fqcn = $namespace . '\\' . $className; $fileName = sys_get_temp_dir() . '/foo_' . uniqid() . '.php'; file_put_contents($fileName, 'classNameInflector ->expects($this->once()) ->method('isProxyClassName') ->with($fqcn) ->will($this->returnValue(true)); $this ->fileLocator ->expects($this->once()) ->method('getProxyFileName') ->will($this->returnValue($fileName)); $this->assertTrue($this->autoloader->__invoke($fqcn)); $this->assertTrue(class_exists($fqcn, false)); } }