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