schemator-php

Форк
0
/
HasPublicPropertyTest.php 
130 строк · 3.6 Кб
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Smoren\Schemator\Tests\Unit\ObjectAccessHelper;
6

7
use Codeception\Test\Unit;
8
use Smoren\Schemator\Helpers\ObjectAccessHelper;
9
use Smoren\Schemator\Tests\Unit\Fixtures\ClassWithAccessibleProperties;
10
use stdClass;
11

12
class HasPublicPropertyTest extends Unit
13
{
14
    /**
15
     * @param stdClass $input
16
     * @param string $key
17
     * @return void
18
     * @dataProvider fromStdClassTrueDataProvider
19
     */
20
    public function testFromStdClassTrue(stdClass $input, string $key): void
21
    {
22
        // When
23
        $result = ObjectAccessHelper::hasPublicProperty($input, $key);
24

25
        // Then
26
        $this->assertTrue($result);
27
    }
28

29
    public function fromStdClassTrueDataProvider(): array
30
    {
31
        $wrap = static function(array $input): object {
32
            return (object)$input;
33
        };
34

35
        return [
36
            [$wrap(['a' => 1, 'b' => 2]), 'a'],
37
            [$wrap(['a' => 1, 'b' => 2]), 'b'],
38
        ];
39
    }
40

41
    /**
42
     * @param stdClass $input
43
     * @param string $key
44
     * @return void
45
     * @dataProvider fromStdClassFalseDataProvider
46
     */
47
    public function testFromStdClassFalse(stdClass $input, string $key): void
48
    {
49
        // When
50
        $result = ObjectAccessHelper::hasPublicProperty($input, $key);
51

52
        // Then
53
        $this->assertFalse($result);
54
    }
55

56
    public function fromStdClassFalseDataProvider(): array
57
    {
58
        $wrap = static function(array $input): object {
59
            return (object)$input;
60
        };
61

62
        return [
63
            [$wrap([]), ''],
64
            [$wrap([]), ''],
65
            [$wrap([]), '0'],
66
            [$wrap([]), '0'],
67
            [$wrap([]), 'a'],
68
            [$wrap([]), 'b'],
69
            [$wrap(['a' => 1, 'b' => 2]), ''],
70
            [$wrap(['a' => 1, 'b' => 2]), ''],
71
            [$wrap(['a' => 1, 'b' => 2]), '0'],
72
            [$wrap(['a' => 1, 'b' => 2]), '0'],
73
            [$wrap(['a' => 1, 'b' => 2]), '1'],
74
            [$wrap(['a' => 1, 'b' => 2]), '1'],
75
            [$wrap(['a' => 1, 'b' => 2]), '2'],
76
            [$wrap(['a' => 1, 'b' => 2]), '2'],
77
        ];
78
    }
79

80
    /**
81
     * @param object $input
82
     * @param string $key
83
     * @return void
84
     * @dataProvider fromObjectTrueDataProvider
85
     */
86
    public function testFromObjectTrue(object $input, string $key): void
87
    {
88
        // When
89
        $result = ObjectAccessHelper::hasPublicProperty($input, $key);
90

91
        // Then
92
        $this->assertTrue($result);
93
    }
94

95
    public function fromObjectTrueDataProvider(): array
96
    {
97
        return [
98
            [new ClassWithAccessibleProperties(), 'publicProperty'],
99
            [new ClassWithAccessibleProperties(), 'publicPropertyWithMethodsAccess'],
100
        ];
101
    }
102

103
    /**
104
     * @param object $input
105
     * @param string $key
106
     * @return void
107
     * @dataProvider fromObjectFalseDataProvider
108
     */
109
    public function testFromObjectFalse(object $input, string $key): void
110
    {
111
        // When
112
        $result = ObjectAccessHelper::hasPublicProperty($input, $key);
113

114
        // Then
115
        $this->assertFalse($result);
116
    }
117

118
    public function fromObjectFalseDataProvider(): array
119
    {
120
        return [
121
            [new ClassWithAccessibleProperties(), ''],
122
            [new ClassWithAccessibleProperties(), '0'],
123
            [new ClassWithAccessibleProperties(), 'unknownProperty'],
124
            [new ClassWithAccessibleProperties(), 'protectedProperty'],
125
            [new ClassWithAccessibleProperties(), 'protectedPropertyWithMethodsAccess'],
126
            [new ClassWithAccessibleProperties(), 'privateProperty'],
127
            [new ClassWithAccessibleProperties(), 'privatePropertyWithMethodsAccess'],
128
        ];
129
    }
130
}
131

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.