ProxyLogicTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Tests\Common\Proxy;
  20. use Doctrine\Common\Proxy\ProxyGenerator;
  21. use Doctrine\Common\Proxy\Proxy;
  22. use Doctrine\Common\Proxy\Exception\UnexpectedValueException;
  23. use Doctrine\Common\Persistence\Mapping\ClassMetadata;
  24. use PHPUnit_Framework_TestCase;
  25. /**
  26. * Test the generated proxies behavior. These tests make assumptions about the structure of LazyLoadableObject
  27. *
  28. * @author Marco Pivetta <ocramius@gmail.com>
  29. */
  30. class ProxyLogicTest extends PHPUnit_Framework_TestCase
  31. {
  32. /**
  33. * @var \PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $proxyLoader;
  36. /**
  37. * @var ClassMetadata
  38. */
  39. protected $lazyLoadableObjectMetadata;
  40. /**
  41. * @var LazyLoadableObject|Proxy
  42. */
  43. protected $lazyObject;
  44. protected $identifier = array(
  45. 'publicIdentifierField' => 'publicIdentifierFieldValue',
  46. 'protectedIdentifierField' => 'protectedIdentifierFieldValue',
  47. );
  48. /**
  49. * @var \PHPUnit_Framework_MockObject_MockObject|Callable
  50. */
  51. protected $initializerCallbackMock;
  52. /**
  53. * {@inheritDoc}
  54. */
  55. public function setUp()
  56. {
  57. $this->proxyLoader = $loader = $this->getMock('stdClass', array('load'), array(), '', false);
  58. $this->initializerCallbackMock = $this->getMock('stdClass', array('__invoke'));
  59. $identifier = $this->identifier;
  60. $this->lazyLoadableObjectMetadata = $metadata = new LazyLoadableObjectClassMetadata();
  61. // emulating what should happen in a proxy factory
  62. $cloner = function (LazyLoadableObject $proxy) use ($loader, $identifier, $metadata) {
  63. /* @var $proxy LazyLoadableObject|Proxy */
  64. if ($proxy->__isInitialized()) {
  65. return;
  66. }
  67. $proxy->__setInitialized(true);
  68. $proxy->__setInitializer(null);
  69. $original = $loader->load($identifier);
  70. if (null === $original) {
  71. throw new UnexpectedValueException();
  72. }
  73. foreach ($metadata->getReflectionClass()->getProperties() as $reflProperty) {
  74. $propertyName = $reflProperty->getName();
  75. if ($metadata->hasField($propertyName) || $metadata->hasAssociation($propertyName)) {
  76. $reflProperty->setAccessible(true);
  77. $reflProperty->setValue($proxy, $reflProperty->getValue($original));
  78. }
  79. }
  80. };
  81. $proxyClassName = 'Doctrine\Tests\Common\ProxyProxy\__CG__\Doctrine\Tests\Common\Proxy\LazyLoadableObject';
  82. // creating the proxy class
  83. if (!class_exists($proxyClassName, false)) {
  84. $proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
  85. $proxyGenerator->generateProxyClass($metadata);
  86. require_once $proxyGenerator->getProxyFileName($metadata->getName());
  87. }
  88. $this->lazyObject = new $proxyClassName($this->getClosure($this->initializerCallbackMock), $cloner);
  89. // setting identifiers in the proxy via reflection
  90. foreach ($metadata->getIdentifierFieldNames() as $idField) {
  91. $prop = $metadata->getReflectionClass()->getProperty($idField);
  92. $prop->setAccessible(true);
  93. $prop->setValue($this->lazyObject, $identifier[$idField]);
  94. }
  95. $this->assertFalse($this->lazyObject->__isInitialized());
  96. }
  97. public function testFetchingPublicIdentifierDoesNotCauseLazyLoading()
  98. {
  99. $this->configureInitializerMock(0);
  100. $this->assertSame('publicIdentifierFieldValue', $this->lazyObject->publicIdentifierField);
  101. }
  102. public function testFetchingIdentifiersViaPublicGetterDoesNotCauseLazyLoading()
  103. {
  104. $this->configureInitializerMock(0);
  105. $this->assertSame('protectedIdentifierFieldValue', $this->lazyObject->getProtectedIdentifierField());
  106. }
  107. public function testCallingMethodCausesLazyLoading()
  108. {
  109. $this->configureInitializerMock(
  110. 1,
  111. array($this->lazyObject, 'testInitializationTriggeringMethod', array()),
  112. function (Proxy $proxy) {
  113. $proxy->__setInitializer(null);
  114. }
  115. );
  116. $this->lazyObject->testInitializationTriggeringMethod();
  117. $this->lazyObject->testInitializationTriggeringMethod();
  118. }
  119. public function testFetchingPublicFieldsCausesLazyLoading()
  120. {
  121. $test = $this;
  122. $this->configureInitializerMock(
  123. 1,
  124. array($this->lazyObject, '__get', array('publicPersistentField')),
  125. function () use ($test) {
  126. $test->setProxyValue('publicPersistentField', 'loadedValue');
  127. }
  128. );
  129. $this->assertSame('loadedValue', $this->lazyObject->publicPersistentField);
  130. $this->assertSame('loadedValue', $this->lazyObject->publicPersistentField);
  131. }
  132. public function testFetchingPublicAssociationCausesLazyLoading()
  133. {
  134. $test = $this;
  135. $this->configureInitializerMock(
  136. 1,
  137. array($this->lazyObject, '__get', array('publicAssociation')),
  138. function () use ($test) {
  139. $test->setProxyValue('publicAssociation', 'loadedAssociation');
  140. }
  141. );
  142. $this->assertSame('loadedAssociation', $this->lazyObject->publicAssociation);
  143. $this->assertSame('loadedAssociation', $this->lazyObject->publicAssociation);
  144. }
  145. public function testFetchingProtectedAssociationViaPublicGetterCausesLazyLoading()
  146. {
  147. $this->configureInitializerMock(
  148. 1,
  149. array($this->lazyObject, 'getProtectedAssociation', array()),
  150. function (Proxy $proxy) {
  151. $proxy->__setInitializer(null);
  152. }
  153. );
  154. $this->assertSame('protectedAssociationValue', $this->lazyObject->getProtectedAssociation());
  155. $this->assertSame('protectedAssociationValue', $this->lazyObject->getProtectedAssociation());
  156. }
  157. public function testLazyLoadingTriggeredOnlyAtFirstPublicPropertyRead()
  158. {
  159. $test = $this;
  160. $this->configureInitializerMock(
  161. 1,
  162. array($this->lazyObject, '__get', array('publicPersistentField')),
  163. function () use ($test) {
  164. $test->setProxyValue('publicPersistentField', 'loadedValue');
  165. $test->setProxyValue('publicAssociation', 'publicAssociationValue');
  166. }
  167. );
  168. $this->assertSame('loadedValue', $this->lazyObject->publicPersistentField);
  169. $this->assertSame('publicAssociationValue', $this->lazyObject->publicAssociation);
  170. }
  171. public function testNoticeWhenReadingNonExistentPublicProperties()
  172. {
  173. $this->configureInitializerMock(0);
  174. $class = get_class($this->lazyObject);
  175. $this->setExpectedException(
  176. 'PHPUnit_Framework_Error_Notice',
  177. 'Undefined property: ' . $class . '::$non_existing_property'
  178. );
  179. $this->lazyObject->non_existing_property;
  180. }
  181. public function testFalseWhenCheckingNonExistentProperty()
  182. {
  183. $this->configureInitializerMock(0);
  184. $this->assertFalse(isset($this->lazyObject->non_existing_property));
  185. }
  186. public function testNoErrorWhenSettingNonExistentProperty()
  187. {
  188. $this->configureInitializerMock(0);
  189. $this->lazyObject->non_existing_property = 'now has a value';
  190. $this->assertSame('now has a value', $this->lazyObject->non_existing_property);
  191. }
  192. public function testCloningCallsClonerWithClonedObject()
  193. {
  194. $lazyObject = $this->lazyObject;
  195. $test = $this;
  196. $cb = $this->getMock('stdClass', array('cb'));
  197. $cb
  198. ->expects($this->once())
  199. ->method('cb')
  200. ->will($this->returnCallback(function (LazyLoadableObject $proxy) use ($lazyObject, $test) {
  201. /* @var $proxy LazyLoadableObject|Proxy */
  202. $test->assertNotSame($proxy, $lazyObject);
  203. $proxy->__setInitializer(null);
  204. $proxy->publicAssociation = 'clonedAssociation';
  205. }));
  206. $this->lazyObject->__setCloner($this->getClosure(array($cb, 'cb')));
  207. $cloned = clone $this->lazyObject;
  208. $this->assertSame('clonedAssociation', $cloned->publicAssociation);
  209. $this->assertNotSame($cloned, $lazyObject, 'a clone of the lazy object is retrieved');
  210. }
  211. public function testFetchingTransientPropertiesWillNotTriggerLazyLoading()
  212. {
  213. $this->configureInitializerMock(0);
  214. $this->assertSame(
  215. 'publicTransientFieldValue',
  216. $this->lazyObject->publicTransientField,
  217. 'fetching public transient field won\'t trigger lazy loading'
  218. );
  219. $property = $this
  220. ->lazyLoadableObjectMetadata
  221. ->getReflectionClass()
  222. ->getProperty('protectedTransientField');
  223. $property->setAccessible(true);
  224. $this->assertSame(
  225. 'protectedTransientFieldValue',
  226. $property->getValue($this->lazyObject),
  227. 'fetching protected transient field via reflection won\'t trigger lazy loading'
  228. );
  229. }
  230. /**
  231. * Provided to guarantee backwards compatibility
  232. */
  233. public function testLoadProxyMethod()
  234. {
  235. $this->configureInitializerMock(2, array($this->lazyObject, '__load', array()));
  236. $this->lazyObject->__load();
  237. $this->lazyObject->__load();
  238. }
  239. public function testLoadingWithPersisterWillBeTriggeredOnlyOnce()
  240. {
  241. $this
  242. ->proxyLoader
  243. ->expects($this->once())
  244. ->method('load')
  245. ->with(
  246. array(
  247. 'publicIdentifierField' => 'publicIdentifierFieldValue',
  248. 'protectedIdentifierField' => 'protectedIdentifierFieldValue',
  249. ),
  250. $this->lazyObject
  251. )
  252. ->will($this->returnCallback(function ($id, LazyLoadableObject $lazyObject) {
  253. // setting a value to verify that the persister can actually set something in the object
  254. $lazyObject->publicAssociation = $id['publicIdentifierField'] . '-test';
  255. return true;
  256. }));
  257. $this->lazyObject->__setInitializer($this->getSuggestedInitializerImplementation());
  258. $this->lazyObject->__load();
  259. $this->lazyObject->__load();
  260. $this->assertSame('publicIdentifierFieldValue-test', $this->lazyObject->publicAssociation);
  261. }
  262. public function testFailedLoadingWillThrowException()
  263. {
  264. $this->proxyLoader->expects($this->any())->method('load')->will($this->returnValue(null));
  265. $this->setExpectedException('UnexpectedValueException');
  266. $this->lazyObject->__setInitializer($this->getSuggestedInitializerImplementation());
  267. $this->lazyObject->__load();
  268. }
  269. public function testCloningWithPersister()
  270. {
  271. $this->lazyObject->publicTransientField = 'should-not-change';
  272. $this
  273. ->proxyLoader
  274. ->expects($this->exactly(2))
  275. ->method('load')
  276. ->with(array(
  277. 'publicIdentifierField' => 'publicIdentifierFieldValue',
  278. 'protectedIdentifierField' => 'protectedIdentifierFieldValue',
  279. ))
  280. ->will($this->returnCallback(function () {
  281. $blueprint = new LazyLoadableObject();
  282. $blueprint->publicPersistentField = 'checked-persistent-field';
  283. $blueprint->publicAssociation = 'checked-association-field';
  284. $blueprint->publicTransientField = 'checked-transient-field';
  285. return $blueprint;
  286. }));
  287. $firstClone = clone $this->lazyObject;
  288. $this->assertSame(
  289. 'checked-persistent-field',
  290. $firstClone->publicPersistentField,
  291. 'Persistent fields are cloned correctly'
  292. );
  293. $this->assertSame(
  294. 'checked-association-field',
  295. $firstClone->publicAssociation,
  296. 'Associations are cloned correctly'
  297. );
  298. $this->assertSame(
  299. 'should-not-change',
  300. $firstClone->publicTransientField,
  301. 'Transient fields are not overwritten'
  302. );
  303. $secondClone = clone $this->lazyObject;
  304. $this->assertSame(
  305. 'checked-persistent-field',
  306. $secondClone->publicPersistentField,
  307. 'Persistent fields are cloned correctly'
  308. );
  309. $this->assertSame(
  310. 'checked-association-field',
  311. $secondClone->publicAssociation,
  312. 'Associations are cloned correctly'
  313. );
  314. $this->assertSame(
  315. 'should-not-change',
  316. $secondClone->publicTransientField,
  317. 'Transient fields are not overwritten'
  318. );
  319. // those should not trigger lazy loading
  320. $firstClone->__load();
  321. $secondClone->__load();
  322. }
  323. public function testNotInitializedProxyUnserialization()
  324. {
  325. $this->configureInitializerMock();
  326. $serialized = serialize($this->lazyObject);
  327. /* @var $unserialized LazyLoadableObject|Proxy */
  328. $unserialized = unserialize($serialized);
  329. $reflClass = $this->lazyLoadableObjectMetadata->getReflectionClass();
  330. $this->assertFalse($unserialized->__isInitialized(), 'serialization didn\'t cause intialization');
  331. // Checking identifiers
  332. $this->assertSame('publicIdentifierFieldValue', $unserialized->publicIdentifierField, 'identifiers are kept');
  333. $protectedIdentifierField = $reflClass->getProperty('protectedIdentifierField');
  334. $protectedIdentifierField->setAccessible(true);
  335. $this->assertSame(
  336. 'protectedIdentifierFieldValue',
  337. $protectedIdentifierField->getValue($unserialized),
  338. 'identifiers are kept'
  339. );
  340. // Checking transient fields
  341. $this->assertSame(
  342. 'publicTransientFieldValue',
  343. $unserialized->publicTransientField,
  344. 'transient fields are kept'
  345. );
  346. $protectedTransientField = $reflClass->getProperty('protectedTransientField');
  347. $protectedTransientField->setAccessible(true);
  348. $this->assertSame(
  349. 'protectedTransientFieldValue',
  350. $protectedTransientField->getValue($unserialized),
  351. 'transient fields are kept'
  352. );
  353. // Checking persistent fields
  354. $this->assertSame(
  355. 'publicPersistentFieldValue',
  356. $unserialized->publicPersistentField,
  357. 'persistent fields are kept'
  358. );
  359. $protectedPersistentField = $reflClass->getProperty('protectedPersistentField');
  360. $protectedPersistentField->setAccessible(true);
  361. $this->assertSame(
  362. 'protectedPersistentFieldValue',
  363. $protectedPersistentField->getValue($unserialized),
  364. 'persistent fields are kept'
  365. );
  366. // Checking associations
  367. $this->assertSame('publicAssociationValue', $unserialized->publicAssociation, 'associations are kept');
  368. $protectedAssociationField = $reflClass->getProperty('protectedAssociation');
  369. $protectedAssociationField->setAccessible(true);
  370. $this->assertSame(
  371. 'protectedAssociationValue',
  372. $protectedAssociationField->getValue($unserialized),
  373. 'associations are kept'
  374. );
  375. }
  376. public function testInitializedProxyUnserialization()
  377. {
  378. // persister will retrieve the lazy object itself, so that we don't have to re-define all field values
  379. $this->proxyLoader->expects($this->once())->method('load')->will($this->returnValue($this->lazyObject));
  380. $this->lazyObject->__setInitializer($this->getSuggestedInitializerImplementation());
  381. $this->lazyObject->__load();
  382. $serialized = serialize($this->lazyObject);
  383. $reflClass = $this->lazyLoadableObjectMetadata->getReflectionClass();
  384. /* @var $unserialized LazyLoadableObject|Proxy */
  385. $unserialized = unserialize($serialized);
  386. $this->assertTrue($unserialized->__isInitialized(), 'serialization didn\'t cause intialization');
  387. // Checking transient fields
  388. $this->assertSame(
  389. 'publicTransientFieldValue',
  390. $unserialized->publicTransientField,
  391. 'transient fields are kept'
  392. );
  393. $protectedTransientField = $reflClass->getProperty('protectedTransientField');
  394. $protectedTransientField->setAccessible(true);
  395. $this->assertSame(
  396. 'protectedTransientFieldValue',
  397. $protectedTransientField->getValue($unserialized),
  398. 'transient fields are kept'
  399. );
  400. // Checking persistent fields
  401. $this->assertSame(
  402. 'publicPersistentFieldValue',
  403. $unserialized->publicPersistentField,
  404. 'persistent fields are kept'
  405. );
  406. $protectedPersistentField = $reflClass->getProperty('protectedPersistentField');
  407. $protectedPersistentField->setAccessible(true);
  408. $this->assertSame(
  409. 'protectedPersistentFieldValue',
  410. $protectedPersistentField->getValue($unserialized),
  411. 'persistent fields are kept'
  412. );
  413. // Checking identifiers
  414. $this->assertSame(
  415. 'publicIdentifierFieldValue',
  416. $unserialized->publicIdentifierField,
  417. 'identifiers are kept'
  418. );
  419. $protectedIdentifierField = $reflClass->getProperty('protectedIdentifierField');
  420. $protectedIdentifierField->setAccessible(true);
  421. $this->assertSame(
  422. 'protectedIdentifierFieldValue',
  423. $protectedIdentifierField->getValue($unserialized),
  424. 'identifiers are kept'
  425. );
  426. // Checking associations
  427. $this->assertSame('publicAssociationValue', $unserialized->publicAssociation, 'associations are kept');
  428. $protectedAssociationField = $reflClass->getProperty('protectedAssociation');
  429. $protectedAssociationField->setAccessible(true);
  430. $this->assertSame(
  431. 'protectedAssociationValue',
  432. $protectedAssociationField->getValue($unserialized),
  433. 'associations are kept'
  434. );
  435. }
  436. public function testInitializationRestoresDefaultPublicLazyLoadedFieldValues()
  437. {
  438. // setting noop persister
  439. $this->proxyLoader->expects($this->once())->method('load')->will($this->returnValue($this->lazyObject));
  440. $this->lazyObject->__setInitializer($this->getSuggestedInitializerImplementation());
  441. $this->assertSame(
  442. 'publicPersistentFieldValue',
  443. $this->lazyObject->publicPersistentField,
  444. 'Persistent field is restored to default value'
  445. );
  446. $this->assertSame(
  447. 'publicAssociationValue',
  448. $this->lazyObject->publicAssociation,
  449. 'Association is restored to default value'
  450. );
  451. }
  452. public function testSettingPublicFieldsCausesLazyLoading()
  453. {
  454. $test = $this;
  455. $this->configureInitializerMock(
  456. 1,
  457. array($this->lazyObject, '__set', array('publicPersistentField', 'newPublicPersistentFieldValue')),
  458. function () use ($test) {
  459. $test->setProxyValue('publicPersistentField', 'overrideValue');
  460. $test->setProxyValue('publicAssociation', 'newAssociationValue');
  461. }
  462. );
  463. $this->lazyObject->publicPersistentField = 'newPublicPersistentFieldValue';
  464. $this->assertSame('newPublicPersistentFieldValue', $this->lazyObject->publicPersistentField);
  465. $this->assertSame('newAssociationValue', $this->lazyObject->publicAssociation);
  466. }
  467. public function testSettingPublicAssociationCausesLazyLoading()
  468. {
  469. $test = $this;
  470. $this->configureInitializerMock(
  471. 1,
  472. array($this->lazyObject, '__set', array('publicAssociation', 'newPublicAssociationValue')),
  473. function () use ($test) {
  474. $test->setProxyValue('publicPersistentField', 'newPublicPersistentFieldValue');
  475. $test->setProxyValue('publicAssociation', 'overrideValue');
  476. }
  477. );
  478. $this->lazyObject->publicAssociation = 'newPublicAssociationValue';
  479. $this->assertSame('newPublicAssociationValue', $this->lazyObject->publicAssociation);
  480. $this->assertSame('newPublicPersistentFieldValue', $this->lazyObject->publicPersistentField);
  481. }
  482. public function testCheckingPublicFieldsCausesLazyLoading()
  483. {
  484. $test = $this;
  485. $this->configureInitializerMock(
  486. 1,
  487. array($this->lazyObject, '__isset', array('publicPersistentField')),
  488. function () use ($test) {
  489. $test->setProxyValue('publicPersistentField', null);
  490. $test->setProxyValue('publicAssociation', 'setPublicAssociation');
  491. }
  492. );
  493. $this->assertFalse(isset($this->lazyObject->publicPersistentField));
  494. $this->assertNull($this->lazyObject->publicPersistentField);
  495. $this->assertTrue(isset($this->lazyObject->publicAssociation));
  496. $this->assertSame('setPublicAssociation', $this->lazyObject->publicAssociation);
  497. }
  498. public function testCheckingPublicAssociationCausesLazyLoading()
  499. {
  500. $test = $this;
  501. $this->configureInitializerMock(
  502. 1,
  503. array($this->lazyObject, '__isset', array('publicAssociation')),
  504. function () use ($test) {
  505. $test->setProxyValue('publicPersistentField', 'newPersistentFieldValue');
  506. $test->setProxyValue('publicAssociation', 'setPublicAssociation');
  507. }
  508. );
  509. $this->assertTrue(isset($this->lazyObject->publicAssociation));
  510. $this->assertSame('setPublicAssociation', $this->lazyObject->publicAssociation);
  511. $this->assertTrue(isset($this->lazyObject->publicPersistentField));
  512. $this->assertSame('newPersistentFieldValue', $this->lazyObject->publicPersistentField);
  513. }
  514. /**
  515. * Converts a given callable into a closure
  516. *
  517. * @param callable $callable
  518. * @return \Closure
  519. */
  520. public function getClosure($callable) {
  521. return function () use ($callable) {
  522. call_user_func_array($callable, func_get_args());
  523. };
  524. }
  525. /**
  526. * Configures the current initializer callback mock with provided matcher params
  527. *
  528. * @param int $expectedCallCount the number of invocations to be expected. If a value< 0 is provided, `any` is used
  529. * @param array $callParamsMatch an ordered array of parameters to be expected
  530. * @param callable $callbackClosure a return callback closure
  531. *
  532. * @return \PHPUnit_Framework_MockObject_MockObject|
  533. */
  534. protected function configureInitializerMock(
  535. $expectedCallCount = 0,
  536. array $callParamsMatch = null,
  537. \Closure $callbackClosure = null
  538. ) {
  539. if (!$expectedCallCount) {
  540. $invocationCountMatcher = $this->exactly((int) $expectedCallCount);
  541. } else {
  542. $invocationCountMatcher = $expectedCallCount < 0 ? $this->any() : $this->exactly($expectedCallCount);
  543. }
  544. $invocationMocker = $this->initializerCallbackMock->expects($invocationCountMatcher)->method('__invoke');
  545. if (null !== $callParamsMatch) {
  546. call_user_func_array(array($invocationMocker, 'with'), $callParamsMatch);
  547. }
  548. if ($callbackClosure) {
  549. $invocationMocker->will($this->returnCallback($callbackClosure));
  550. }
  551. }
  552. /**
  553. * Sets a value in the current proxy object without triggering lazy loading through `__set`
  554. *
  555. * @link https://bugs.php.net/bug.php?id=63463
  556. *
  557. * @param string $property
  558. * @param mixed $value
  559. */
  560. public function setProxyValue($property, $value)
  561. {
  562. $reflectionProperty = new \ReflectionProperty($this->lazyObject, $property);
  563. $initializer = $this->lazyObject->__getInitializer();
  564. // disabling initializer since setting `publicPersistentField` triggers `__set`/`__get`
  565. $this->lazyObject->__setInitializer(null);
  566. $reflectionProperty->setValue($this->lazyObject, $value);
  567. $this->lazyObject->__setInitializer($initializer);
  568. }
  569. /**
  570. * Retrieves the suggested implementation of an initializer that proxy factories in O*M
  571. * are currently following, and that should be used to initialize the current proxy object
  572. *
  573. * @return \Closure
  574. */
  575. protected function getSuggestedInitializerImplementation()
  576. {
  577. $loader = $this->proxyLoader;
  578. $identifier = $this->identifier;
  579. return function (LazyLoadableObject $proxy) use ($loader, $identifier) {
  580. /* @var $proxy LazyLoadableObject|Proxy */
  581. $proxy->__setInitializer(null);
  582. $proxy->__setCloner(null);
  583. if ($proxy->__isInitialized()) {
  584. return;
  585. }
  586. $properties = $proxy->__getLazyProperties();
  587. foreach ($properties as $propertyName => $property) {
  588. if (!isset($proxy->$propertyName)) {
  589. $proxy->$propertyName = $properties[$propertyName];
  590. }
  591. }
  592. $proxy->__setInitialized(true);
  593. if (method_exists($proxy, '__wakeup')) {
  594. $proxy->__wakeup();
  595. }
  596. if (null === $loader->load($identifier, $proxy)) {
  597. throw new \UnexpectedValueException('Couldn\'t load');
  598. }
  599. };
  600. }
  601. }