schemator-php
130 строк · 3.6 Кб
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 HasPublicPropertyTest 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::hasPublicProperty($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::hasPublicProperty($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([]), ''],
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*/
86public 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
95public function fromObjectTrueDataProvider(): array
96{
97return [
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*/
109public 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
118public function fromObjectFalseDataProvider(): array
119{
120return [
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