generateProxyClass() публичный Метод

Generates a proxy class file.
public generateProxyClass ( Doctrine\Common\Persistence\Mapping\ClassMetadata $class, string | boolean $fileName = false )
$class Doctrine\Common\Persistence\Mapping\ClassMetadata Metadata for the original class.
$fileName string | boolean Filename (full path) for the generated class. If none is given, eval() is used.
Пример #1
0
 /**
  * @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;
 }
Пример #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());
 }
Пример #3
0
 public function testDoctrineProxy()
 {
     $className = 'Saxulum\\Tests\\Accessor\\Fixtures\\Entity\\Entity';
     $proxyDirectory = __DIR__ . '/../proxy/';
     $proxyNamespace = 'Proxy';
     $proxyClassName = $proxyNamespace . '\\__CG__\\' . $className;
     $proxyClassFilename = $proxyDirectory . str_replace('\\', '_', $proxyClassName) . '.php';
     if (!is_dir($proxyDirectory)) {
         mkdir($proxyDirectory, 0777, true);
     }
     $reflectionService = new RuntimeReflectionService();
     $classMetadata = new ClassMetadata(get_class(new Entity()));
     $classMetadata->initializeReflection($reflectionService);
     $proxyGenerator = new ProxyGenerator($proxyDirectory, $proxyNamespace);
     $proxyGenerator->generateProxyClass($classMetadata, $proxyClassFilename);
     require $proxyClassFilename;
     /** @var Entity $proxy */
     $proxy = new $proxyClassName();
     $proxy->setName('test');
     $this->assertEquals('test', $proxy->getName());
     unlink($proxyClassFilename);
     //rmdir($proxyDirectory);
 }
 public function testClassWithInvalidTypeHintOnProxiedMethod()
 {
     $className = 'Doctrine\\Tests\\Common\\Proxy\\InvalidTypeHintClass';
     $metadata = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
     $reflClass = new ReflectionClass($className);
     $metadata->expects($this->any())->method('getReflectionClass')->will($this->returnValue($reflClass));
     $metadata->expects($this->any())->method('getIdentifierFieldNames')->will($this->returnValue(array()));
     $metadata->expects($this->any())->method('getName')->will($this->returnValue($className));
     $proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
     $this->setExpectedException('Doctrine\\Common\\Proxy\\Exception\\UnexpectedValueException', 'The type hint of parameter "foo" in method "invalidTypeHintMethod"' . ' in class "' . $className . '" is invalid.');
     $proxyGenerator->generateProxyClass($metadata);
 }
 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);
 }
Пример #6
0
 public function testUseEvalIfNoFilenameIsGiven()
 {
     $proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
     $className = __NAMESPACE__ . '\\EvalBase';
     $metadata = $this->createClassMetadata($className, array('id'));
     $proxyGenerator->generateProxyClass($metadata);
     $reflClass = new ReflectionClass('Doctrine\\Tests\\Common\\ProxyProxy\\__CG__\\Doctrine\\Tests\\Common\\Proxy\\EvalBase');
     $this->assertContains("eval()'d code", $reflClass->getFileName());
 }