schemator-php

Форк
0
/
HasReadablePropertyTest.php 
124 строки · 3.4 Кб
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 HasReadablePropertyTest 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::hasReadableProperty($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::hasReadableProperty($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([]), '0'],
65
            [$wrap([]), 'a'],
66
            [$wrap([]), 'b'],
67
            [$wrap(['a' => 1, 'b' => 2]), ''],
68
            [$wrap(['a' => 1, 'b' => 2]), '0'],
69
            [$wrap(['a' => 1, 'b' => 2]), '1'],
70
            [$wrap(['a' => 1, 'b' => 2]), '2'],
71
        ];
72
    }
73

74
    /**
75
     * @param object $input
76
     * @param string $key
77
     * @return void
78
     * @dataProvider fromObjectTrueDataProvider
79
     */
80
    public function testFromObjectTrue(object $input, string $key): void
81
    {
82
        // When
83
        $result = ObjectAccessHelper::hasReadableProperty($input, $key);
84

85
        // Then
86
        $this->assertTrue($result);
87
    }
88

89
    public function fromObjectTrueDataProvider(): array
90
    {
91
        return [
92
            [new ClassWithAccessibleProperties(), 'publicProperty'],
93
            [new ClassWithAccessibleProperties(), 'publicPropertyWithMethodsAccess'],
94
            [new ClassWithAccessibleProperties(), 'protectedPropertyWithMethodsAccess'],
95
            [new ClassWithAccessibleProperties(), 'privatePropertyWithMethodsAccess'],
96
        ];
97
    }
98

99
    /**
100
     * @param object $input
101
     * @param string $key
102
     * @return void
103
     * @dataProvider fromObjectFalseDataProvider
104
     */
105
    public function testFromObjectFalse(object $input, string $key): void
106
    {
107
        // When
108
        $result = ObjectAccessHelper::hasReadableProperty($input, $key);
109

110
        // Then
111
        $this->assertFalse($result);
112
    }
113

114
    public function fromObjectFalseDataProvider(): array
115
    {
116
        return [
117
            [new ClassWithAccessibleProperties(), ''],
118
            [new ClassWithAccessibleProperties(), '0'],
119
            [new ClassWithAccessibleProperties(), 'unknownProperty'],
120
            [new ClassWithAccessibleProperties(), 'protectedProperty'],
121
            [new ClassWithAccessibleProperties(), 'privateProperty'],
122
        ];
123
    }
124
}
125

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

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

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

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