04e30262ad5a469f1f7881cee4165064be4d3485
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / UnitDeprecated / Compatibility / PublicPropertyDeprecationTraitTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Compatibility;
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
18 use TYPO3\CMS\Core\Compatibility\PublicPropertyDeprecationTrait;
19 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
20
21 class PublicPropertyDeprecationTraitTest extends UnitTestCase
22 {
23 /**
24 * @var object Test fixture (anonymous class)
25 * @see PublicAccessDeprecationTraitTest::setUp()
26 */
27 protected $fixture;
28
29 /**
30 * Setup
31 *
32 * Creating the test fixture, an anonymous class with different kinds
33 * of properties to test access for.
34 */
35 protected function setUp()
36 {
37 $this->fixture = new class {
38 use PublicPropertyDeprecationTrait;
39 private $deprecatedPublicProperties = [
40 'taggedProperty' => 'taggedProperty is deprecated',
41 'unsetTaggedProperty' => 'unsetTaggedProperty is deprecated'
42 ];
43
44 public $publicProperty = 'publicProperty';
45
46 public $unsetPublicProperty;
47
48 /**
49 * @deprecatedPublic
50 */
51 protected $taggedProperty = 'taggedProperty';
52
53 /**
54 * @deprecatedPublic
55 */
56 protected $unsetTaggedProperty;
57
58 protected $untaggedProperty = 'untaggedProperty';
59 };
60 }
61
62 /**
63 * @return array [[$expected, $property],]
64 */
65 public function issetDataProvider(): array
66 {
67 return [
68 'public property' => [true, 'publicProperty'],
69 'unset public property' => [false, 'unsetPublicProperty'],
70 'tagged property' => [true, 'taggedProperty'],
71 'unset tagged property' => [false, 'unsetTaggedProperty'],
72 'untagged property' => [false, 'untaggedProperty'],
73 'unknown property' => [false, 'unknownProperty'],
74 ];
75 }
76
77 /**
78 * @dataProvider issetDataProvider
79 * @test
80 * @param bool $expected
81 * @param string $property
82 */
83 public function issetWorksAsExpected(bool $expected, string $property)
84 {
85 $this->assertSame($expected, isset($this->fixture->$property));
86 }
87
88 /**
89 * @test
90 */
91 public function unknownPropertyCanBeHandledAsUsual()
92 {
93 // Uses __isset()
94 $this->assertFalse(isset($this->fixture->unknownProperty));
95 // Uses __set()
96 $this->fixture->unknownProperty = 23;
97 // Don't uses __isset()
98 $this->assertTrue(isset($this->fixture->unknownProperty));
99 // Don't uses __get()
100 $this->assertSame(23, $this->fixture->unknownProperty);
101 // Don't uses __unset()
102 unset($this->fixture->unknownProperty);
103 // Uses __isset()
104 $this->assertFalse(isset($this->fixture->unknownProperty));
105 }
106
107 /**
108 * @test
109 */
110 public function publicPropertyCanBeHandledAsUsual()
111 {
112 $this->assertFalse(isset($this->fixture->unsetPublicProperty));
113 $this->fixture->unsetPublicProperty = 23;
114 $this->assertTrue(isset($this->fixture->unsetPublicProperty));
115 $this->assertSame(23, $this->fixture->unsetPublicProperty);
116 unset($this->fixture->unsetPublicProperty);
117 $this->assertFalse(isset($this->fixture->unsetPublicProperty));
118 }
119
120 /**
121 * @test
122 */
123 public function taggedPropertyCanBeHandledLikePublicProperty()
124 {
125 $this->assertFalse(isset($this->fixture->unsetTaggedProperty));
126 $this->fixture->unsetTaggedProperty = 23;
127 $this->assertTrue(isset($this->fixture->unsetTaggedProperty));
128 $this->assertSame(23, $this->fixture->unsetTaggedProperty);
129 unset($this->fixture->unsetTaggedProperty);
130 $this->assertFalse(isset($this->fixture->unsetTaggedProperty));
131 }
132
133 /**
134 * @return array [[$property],]
135 */
136 public function invalidPropertiesDataProvider(): array
137 {
138 return [
139 'untagged' => ['untaggedProperty'],
140 'unknown' => ['unknownProperty'],
141 ];
142 }
143 }