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