PublicPropertyDeprecationTraitTest.php 4.38 KB
Newer Older
1
<?php
2

3
declare(strict_types=1);
4
5
6
7
8
9
10
11
12
13
14
15
16
17

/*
 * 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!
 */

18
namespace TYPO3\CMS\Core\Tests\UnitDeprecated\Compatibility;
19

20
21
22
use TYPO3\CMS\Core\Compatibility\PublicPropertyDeprecationTrait;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

23
24
25
/**
 * Test case
 */
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class PublicPropertyDeprecationTraitTest extends UnitTestCase
{
    /**
     * @var object Test fixture (anonymous class)
     * @see PublicAccessDeprecationTraitTest::setUp()
     */
    protected $fixture;

    /**
     * Setup
     *
     * Creating the test fixture, an anonymous class with different kinds
     * of properties to test access for.
     */
40
    protected function setUp(): void
41
    {
42
        parent::setUp();
43
        $this->fixture = new class() {
44
45
46
            use PublicPropertyDeprecationTrait;
            private $deprecatedPublicProperties = [
                'taggedProperty' => 'taggedProperty is deprecated',
47
                'unsetTaggedProperty' => 'unsetTaggedProperty is deprecated',
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
80
81
82
83
84
85
86
87
88
            ];

            public $publicProperty = 'publicProperty';

            public $unsetPublicProperty;

            /**
             * @deprecatedPublic
             */
            protected $taggedProperty = 'taggedProperty';

            /**
             * @deprecatedPublic
             */
            protected $unsetTaggedProperty;

            protected $untaggedProperty = 'untaggedProperty';
        };
    }

    /**
     * @return array [[$expected, $property],]
     */
    public function issetDataProvider(): array
    {
        return [
            'public property' => [true, 'publicProperty'],
            'unset public property' => [false, 'unsetPublicProperty'],
            'tagged property' => [true, 'taggedProperty'],
            'unset tagged property' => [false, 'unsetTaggedProperty'],
            'untagged property' => [false, 'untaggedProperty'],
            'unknown property' => [false, 'unknownProperty'],
        ];
    }

    /**
     * @dataProvider issetDataProvider
     * @test
     * @param bool $expected
     * @param string $property
     */
89
    public function issetWorksAsExpected(bool $expected, string $property): void
90
    {
91
        self::assertSame($expected, isset($this->fixture->$property));
92
93
94
95
96
    }

    /**
     * @test
     */
97
    public function unknownPropertyCanBeHandledAsUsual(): void
98
99
    {
        // Uses __isset()
100
        self::assertFalse(isset($this->fixture->unknownProperty));
101
102
103
        // Uses __set()
        $this->fixture->unknownProperty = 23;
        // Don't uses __isset()
104
        self::assertTrue(isset($this->fixture->unknownProperty));
105
        // Don't uses __get()
106
        self::assertSame(23, $this->fixture->unknownProperty);
107
108
109
        // Don't uses __unset()
        unset($this->fixture->unknownProperty);
        // Uses __isset()
110
        self::assertFalse(isset($this->fixture->unknownProperty));
111
112
113
114
115
    }

    /**
     * @test
     */
116
    public function publicPropertyCanBeHandledAsUsual(): void
117
    {
118
        self::assertFalse(isset($this->fixture->unsetPublicProperty));
119
        $this->fixture->unsetPublicProperty = 23;
120
121
        self::assertTrue(isset($this->fixture->unsetPublicProperty));
        self::assertSame(23, $this->fixture->unsetPublicProperty);
122
        unset($this->fixture->unsetPublicProperty);
123
        self::assertFalse(isset($this->fixture->unsetPublicProperty));
124
125
126
127
128
    }

    /**
     * @test
     */
129
    public function taggedPropertyCanBeHandledLikePublicProperty(): void
130
    {
131
        self::assertFalse(isset($this->fixture->unsetTaggedProperty));
132
        $this->fixture->unsetTaggedProperty = 23;
133
134
        self::assertTrue(isset($this->fixture->unsetTaggedProperty));
        self::assertSame(23, $this->fixture->unsetTaggedProperty);
135
        unset($this->fixture->unsetTaggedProperty);
136
        self::assertFalse(isset($this->fixture->unsetTaggedProperty));
137
138
139
140
141
142
143
144
145
146
147
148
149
    }

    /**
     * @return array [[$property],]
     */
    public function invalidPropertiesDataProvider(): array
    {
        return [
            'untagged' => ['untaggedProperty'],
            'unknown' => ['unknownProperty'],
        ];
    }
}