schemator-php
124 строки · 3.4 Кб
1<?php
2
3declare(strict_types=1);
4
5namespace Smoren\Schemator\Tests\Unit\ObjectAccessHelper;
6
7use Codeception\Test\Unit;
8use Smoren\Schemator\Helpers\ObjectAccessHelper;
9use Smoren\Schemator\Tests\Unit\Fixtures\ClassWithAccessibleProperties;
10use stdClass;
11
12class HasReadablePropertyTest extends Unit
13{
14/**
15* @param stdClass $input
16* @param string $key
17* @return void
18* @dataProvider fromStdClassTrueDataProvider
19*/
20public 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
29public function fromStdClassTrueDataProvider(): array
30{
31$wrap = static function(array $input): object {
32return (object)$input;
33};
34
35return [
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*/
47public 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
56public function fromStdClassFalseDataProvider(): array
57{
58$wrap = static function(array $input): object {
59return (object)$input;
60};
61
62return [
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*/
80public 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
89public function fromObjectTrueDataProvider(): array
90{
91return [
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*/
105public 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
114public function fromObjectFalseDataProvider(): array
115{
116return [
117[new ClassWithAccessibleProperties(), ''],
118[new ClassWithAccessibleProperties(), '0'],
119[new ClassWithAccessibleProperties(), 'unknownProperty'],
120[new ClassWithAccessibleProperties(), 'protectedProperty'],
121[new ClassWithAccessibleProperties(), 'privateProperty'],
122];
123}
124}
125