You are here

public function ProxyLogicTest::setUp in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/doctrine/common/tests/Doctrine/Tests/Common/Proxy/ProxyLogicTest.php \Doctrine\Tests\Common\Proxy\ProxyLogicTest::setUp()

Sets up the fixture, for example, open a network connection. This method is called before a test is executed.

Overrides PHPUnit_Framework_TestCase::setUp

File

vendor/doctrine/common/tests/Doctrine/Tests/Common/Proxy/ProxyLogicTest.php, line 63

Class

ProxyLogicTest
Test the generated proxies behavior. These tests make assumptions about the structure of LazyLoadableObject

Namespace

Doctrine\Tests\Common\Proxy

Code

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());
}