getProxyFileName() public method

Generates the Proxy file name.
public getProxyFileName ( string $className, string $baseDirectory = null ) : string
$className string
$baseDirectory string Optional base directory for proxy file name generation. If not specified, the directory configured on the Configuration of the EntityManager will be used by this factory.
return string
 /**
  * @param $className
  *
  * @return string
  */
 private function generateProxyClass($className)
 {
     $proxyClassName = 'Doctrine\\Tests\\Common\\Proxy\\MagicMethodProxy\\__CG__\\' . $className;
     if (class_exists($proxyClassName, false)) {
         return $proxyClassName;
     }
     $metadata = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
     $metadata->expects($this->any())->method('getName')->will($this->returnValue($className));
     $metadata->expects($this->any())->method('getIdentifier')->will($this->returnValue(array('id')));
     $metadata->expects($this->any())->method('getReflectionClass')->will($this->returnValue(new ReflectionClass($className)));
     $metadata->expects($this->any())->method('isIdentifier')->will($this->returnCallback(function ($fieldName) {
         return 'id' === $fieldName;
     }));
     $metadata->expects($this->any())->method('hasField')->will($this->returnCallback(function ($fieldName) {
         return in_array($fieldName, array('id', 'publicField'));
     }));
     $metadata->expects($this->any())->method('hasAssociation')->will($this->returnValue(false));
     $metadata->expects($this->any())->method('getFieldNames')->will($this->returnValue(array('id', 'publicField')));
     $metadata->expects($this->any())->method('getIdentifierFieldNames')->will($this->returnValue(array('id')));
     $metadata->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array()));
     $metadata->expects($this->any())->method('getTypeOfField')->will($this->returnValue('string'));
     $this->proxyGenerator->generateProxyClass($metadata, $this->proxyGenerator->getProxyFileName($className));
     require_once $this->proxyGenerator->getProxyFileName($className);
     return $proxyClassName;
 }
Beispiel #2
0
 /**
  * {@inheritDoc}
  */
 public function setUp()
 {
     $this->proxyLoader = $loader = $this->getMock('stdClass', array('load'), array(), '', false);
     $this->initializerCallbackMock = $this->getMock('stdClass', array('__invoke'));
     $identifier = $this->identifier;
     $this->lazyLoadableObjectMetadata = $metadata = new LazyLoadableObjectClassMetadata();
     // emulating what should happen in a proxy factory
     $cloner = function (LazyLoadableObject $proxy) use($loader, $identifier, $metadata) {
         /* @var $proxy LazyLoadableObject|Proxy */
         if ($proxy->__isInitialized()) {
             return;
         }
         $proxy->__setInitialized(true);
         $proxy->__setInitializer(null);
         $original = $loader->load($identifier);
         if (null === $original) {
             throw new UnexpectedValueException();
         }
         foreach ($metadata->getReflectionClass()->getProperties() as $reflProperty) {
             $propertyName = $reflProperty->getName();
             if ($metadata->hasField($propertyName) || $metadata->hasAssociation($propertyName)) {
                 $reflProperty->setAccessible(true);
                 $reflProperty->setValue($proxy, $reflProperty->getValue($original));
             }
         }
     };
     $proxyClassName = 'Doctrine\\Tests\\Common\\ProxyProxy\\__CG__\\Doctrine\\Tests\\Common\\Proxy\\LazyLoadableObject';
     // creating the proxy class
     if (!class_exists($proxyClassName, false)) {
         $proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
         $proxyGenerator->generateProxyClass($metadata);
         require_once $proxyGenerator->getProxyFileName($metadata->getName());
     }
     $this->lazyObject = new $proxyClassName($this->getClosure($this->initializerCallbackMock), $cloner);
     // setting identifiers in the proxy via reflection
     foreach ($metadata->getIdentifierFieldNames() as $idField) {
         $prop = $metadata->getReflectionClass()->getProperty($idField);
         $prop->setAccessible(true);
         $prop->setValue($this->lazyObject, $identifier[$idField]);
     }
     $this->assertFalse($this->lazyObject->__isInitialized());
 }
 public function testClassWithCallableTypeHintOnProxiedMethod()
 {
     if (PHP_VERSION_ID < 50400) {
         $this->markTestSkipped('`callable` is only supported in PHP >=5.4.0');
     }
     if (!class_exists('Doctrine\\Tests\\Common\\ProxyProxy\\__CG__\\CallableTypeHintClass', false)) {
         $metadata = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
         $reflClass = new ReflectionClass('Doctrine\\Tests\\Common\\Proxy\\CallableTypeHintClass');
         $proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
         $metadata->expects($this->any())->method('getReflectionClass')->will($this->returnValue($reflClass));
         $metadata->expects($this->any())->method('getIdentifierFieldNames')->will($this->returnValue(array('id')));
         $metadata->expects($this->any())->method('getName')->will($this->returnValue($reflClass->getName()));
         $proxyGenerator->generateProxyClass($metadata);
         require_once $proxyGenerator->getProxyFileName($metadata->getName());
     }
     $classCode = file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyCallableTypeHintClass.php');
     $this->assertEquals(1, substr_count($classCode, 'call(callable $foo)'));
 }
 public function testCallingVariadicMethodCausesLazyLoading()
 {
     if (PHP_VERSION_ID < 50600) {
         $this->markTestSkipped('Test applies only to PHP 5.6+');
     }
     $proxyClassName = 'Doctrine\\Tests\\Common\\ProxyProxy\\__CG__\\Doctrine\\Tests\\Common\\Proxy\\VariadicTypeHintClass';
     /* @var $metadata \Doctrine\Common\Persistence\Mapping\ClassMetadata|\PHPUnit_Framework_MockObject_MockObject */
     $metadata = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
     $metadata->expects($this->any())->method('getName')->will($this->returnValue('Doctrine\\Tests\\Common\\Proxy\\VariadicTypeHintClass'));
     $metadata->expects($this->any())->method('getReflectionClass')->will($this->returnValue(new \ReflectionClass('Doctrine\\Tests\\Common\\Proxy\\VariadicTypeHintClass')));
     // creating the proxy class
     if (!class_exists($proxyClassName, false)) {
         $proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
         $proxyGenerator->generateProxyClass($metadata);
         require_once $proxyGenerator->getProxyFileName($metadata->getName());
     }
     /* @var $invocationMock callable|\PHPUnit_Framework_MockObject_MockObject */
     $invocationMock = $this->getMock('stdClass', array('__invoke'));
     /* @var $lazyObject \Doctrine\Tests\Common\Proxy\VariadicTypeHintClass */
     $lazyObject = new $proxyClassName(function ($proxy, $method, $parameters) use($invocationMock) {
         $invocationMock($proxy, $method, $parameters);
     }, function () {
     });
     $invocationMock->expects($this->at(0))->method('__invoke')->with($lazyObject, 'addType', array(array('type1', 'type2')));
     $invocationMock->expects($this->at(1))->method('__invoke')->with($lazyObject, 'addTypeWithMultipleParameters', array('foo', 'bar', array('baz1', 'baz2')));
     $lazyObject->addType('type1', 'type2');
     $this->assertSame(array('type1', 'type2'), $lazyObject->types);
     $lazyObject->addTypeWithMultipleParameters('foo', 'bar', 'baz1', 'baz2');
     $this->assertSame('foo', $lazyObject->foo);
     $this->assertSame('bar', $lazyObject->bar);
     $this->assertSame(array('baz1', 'baz2'), $lazyObject->baz);
 }
 public function testNonNamespacedProxyGeneration()
 {
     $classCode = file_get_contents($this->proxyGenerator->getProxyFileName($this->metadata->getName()));
     $this->assertNotContains("class LazyLoadableObject extends \\\\" . $this->metadata->getName(), $classCode);
     $this->assertContains("class LazyLoadableObject extends \\" . $this->metadata->getName(), $classCode);
 }