‪TYPO3CMS  ‪main
LazyLoadingProxyTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\Test;
26 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
29 
30 final class ‪LazyLoadingProxyTest extends FunctionalTestCase
31 {
32  protected array ‪$coreExtensionsToLoad = ['extbase'];
33  protected array ‪$testExtensionsToLoad = ['typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example'];
34 
35  protected function ‪setUp(): void
36  {
37  parent::setUp();
38  $this->importCSVDataSet(__DIR__ . '/Fixtures/LazyLoadingProxyTestImport.csv');
39  $request = (new ‪ServerRequest())->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
40  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
41  }
42 
43  #[Test]
44  public function ‪serializeAndUnserialize(): void
45  {
46  $blog = new ‪Blog();
47  $blog->_setProperty('administrator', new ‪LazyLoadingProxy($blog, 'administrator', 1));
48 
49  $serialized = serialize($blog->getAdministrator());
50 
51  self::assertFalse(str_contains($serialized, 'dataMapper'), 'Assert that serialized object string does not contain dataMapper');
52 
54  $administratorProxy = unserialize($serialized, ['allowed_classes' => true]);
55  self::assertInstanceOf(LazyLoadingProxy::class, $administratorProxy, 'Assert that $administratorProxy is an instance of LazyLoadingProxy');
56 
58  $administrator = $administratorProxy->_loadRealInstance();
59  self::assertInstanceOf(Administrator::class, $administrator, 'Assert that $administrator is an instance of Administrator');
60 
61  self::assertSame('Blog Admin', $administrator->getUsername());
62  }
63 
64  #[Test]
66  {
67  $lazyLoadingProxy = new ‪LazyLoadingProxy(
68  new ‪Blog(),
69  'administrator',
70  0,
71  GeneralUtility::makeInstance(DataMapper::class)
72  );
73  // Directly using the magic `__get()` method here to avoid PHPStan complaining
74  // about the dynamic property issue and spare an ignore pattern or annotation.
75  // See: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property
76  // This equals to: self::assertNull($lazyLoadingProxy->name);
77  self::assertNull($lazyLoadingProxy->__get('name'));
78  }
79 }
‪TYPO3Tests\BlogExample\Domain\Model\Administrator
Definition: Administrator.php:28
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\LazyLoadingProxyTest\$coreExtensionsToLoad
‪array $coreExtensionsToLoad
Definition: LazyLoadingProxyTest.php:32
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\LazyLoadingProxyTest\setUp
‪setUp()
Definition: LazyLoadingProxyTest.php:35
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_BE
‪const REQUESTTYPE_BE
Definition: SystemEnvironmentBuilder.php:45
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
Definition: DataMapper.php:58
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\LazyLoadingProxyTest
Definition: LazyLoadingProxyTest.php:31
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\LazyLoadingProxyTest\$testExtensionsToLoad
‪array $testExtensionsToLoad
Definition: LazyLoadingProxyTest.php:33
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\LazyLoadingProxyTest\nonExistingLazyLoadedPropertyReturnsNull
‪nonExistingLazyLoadedPropertyReturnsNull()
Definition: LazyLoadingProxyTest.php:65
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence
Definition: AddTest.php:18
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\LazyLoadingProxyTest\serializeAndUnserialize
‪serializeAndUnserialize()
Definition: LazyLoadingProxyTest.php:44
‪TYPO3Tests\BlogExample\Domain\Model\Blog
Definition: Blog.php:30
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy
Definition: LazyLoadingProxy.php:30
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52