Skip to content

Commit 6ee9c8d

Browse files
committed
feat: add faker support
1 parent 7d4cce7 commit 6ee9c8d

14 files changed

Lines changed: 735 additions & 11 deletions

composer.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@
1111
"Constructo\\": "src/"
1212
},
1313
"files": [
14-
"functions/cast.php",
15-
"functions/crypt.php",
16-
"functions/json.php",
17-
"functions/notation.php",
18-
"functions/util.php"
14+
"src/_/cast.php",
15+
"src/_/crypt.php",
16+
"src/_/json.php",
17+
"src/_/notation.php",
18+
"src/_/util.php"
1919
]
2020
},
2121
"autoload-dev": {
2222
"psr-4": {
23-
"Constructo\\Test\\": "tests/"
23+
"Constructo\\Test\\": "tests/",
24+
"Constructo\\Test\\_\\": "tests/_/"
2425
}
2526
},
2627
"require": {

src/Support/Metadata/Schema.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,17 @@ public function get(string $name): Field
4141
return $field;
4242
}
4343

44+
/**
45+
* @return array<string, array|string>
46+
*/
4447
public function rules(): array
4548
{
4649
return array_map(fn (Field $field) => $field->rules(), $this->available());
4750
}
4851

52+
/**
53+
* @return array<string, callable(mixed):mixed|string>
54+
*/
4955
public function mappings(): array
5056
{
5157
$mappings = [];
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

tests/Core/Fake/FakerTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,6 @@ public function testShouldUseBrazilianLocaleWhenProvided(): void
170170
$phone = $generator->phoneNumber();
171171
$this->assertMatchesRegularExpression('/^(\+55|\(?\d{2}\)?)\s?[0-9\s\-\(\)]{8,}/', $phone);
172172

173-
$companySuffix = $generator->companySuffix();
174-
$brazilianSuffixes = ['Ltda.', 'S.A.', 'ME', 'EPP', 'EIRELI', 'e Filhos', 'Sociedade Anônima', 'Sociedade Limitada'];
175-
$this->assertContains($companySuffix, $brazilianSuffixes);
176-
177173
$foundBrazilianDomain = false;
178174
for ($i = 0; $i < 10; $i++) {
179175
$domain = $generator->domainName();

tests/Stub/ComplexInput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Constructo\Test\Stub;
66

77
use Hyperf\Validation\Rule;
8-
use Serendipity\Presentation\Input;
8+
use Constructo\Presentation\Input;
99

1010
use function Hyperf\Collection\data_get;
1111
use function preg_replace;

tests/_/CastFunctionsTest.php

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Constructo\Test\_;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use stdClass;
9+
use Stringable;
10+
11+
use function Constructo\Cast\arrayify;
12+
use function Constructo\Cast\boolify;
13+
use function Constructo\Cast\floatify;
14+
use function Constructo\Cast\integerify;
15+
use function Constructo\Cast\mapify;
16+
use function Constructo\Cast\stringify;
17+
18+
final class CastFunctionsTest extends TestCase
19+
{
20+
public function testToArrayReturnsArrayWhenValueIsArray(): void
21+
{
22+
$value = ['key' => 'value'];
23+
$result = arrayify($value);
24+
$this->assertEquals($value, $result);
25+
}
26+
27+
public function testToArrayReturnsDefaultWhenValueIsNotArray(): void
28+
{
29+
$value = 'not an array';
30+
$default = ['default'];
31+
$result = arrayify($value, $default);
32+
$this->assertEquals($default, $result);
33+
}
34+
35+
public function testToStringReturnsStringWhenValueIsString(): void
36+
{
37+
$value = 'string';
38+
$result = stringify($value);
39+
$this->assertEquals($value, $result);
40+
}
41+
42+
public function testToStringReturnsDefaultWhenValueIsNotString(): void
43+
{
44+
$value = new stdClass();
45+
$default = 'default';
46+
$result = stringify($value, $default);
47+
$this->assertEquals($default, $result);
48+
}
49+
50+
public function testToIntReturnsIntWhenValueIsInt(): void
51+
{
52+
$value = 123;
53+
$result = integerify($value);
54+
$this->assertEquals($value, $result);
55+
}
56+
57+
public function testToIntReturnsDefaultWhenValueIsNotInt(): void
58+
{
59+
$value = 'not an int';
60+
$default = 456;
61+
$result = integerify($value, $default);
62+
$this->assertEquals($default, $result);
63+
}
64+
65+
public function testToBoolReturnsBoolWhenValueIsBool(): void
66+
{
67+
$value = true;
68+
$result = boolify($value);
69+
$this->assertEquals($value, $result);
70+
}
71+
72+
public function testToBoolReturnsDefaultWhenValueIsNotBool(): void
73+
{
74+
$value = 'not a bool';
75+
$default = true;
76+
$result = boolify($value, $default);
77+
$this->assertEquals(true, $result);
78+
}
79+
80+
public function testFloatifyReturnsFloatWhenValueIsFloat(): void
81+
{
82+
$value = 123.45;
83+
$result = floatify($value);
84+
$this->assertEquals($value, $result);
85+
}
86+
87+
public function testFloatifyReturnsDefaultWhenValueIsNotFloat(): void
88+
{
89+
$value = 'not a float';
90+
$default = 456.78;
91+
$result = floatify($value, $default);
92+
$this->assertEquals($default, $result);
93+
}
94+
95+
public function testFloatifyConvertsNumericStringsToFloat(): void
96+
{
97+
$value = '123.45';
98+
$result = floatify($value);
99+
$this->assertEquals(123.45, $result);
100+
}
101+
102+
public function testFloatifyConvertsIntegersToFloat(): void
103+
{
104+
$value = 123;
105+
$result = floatify($value);
106+
$this->assertEquals(123.0, $result);
107+
$this->assertIsFloat($result);
108+
}
109+
110+
public function testMapifyConvertsObjectToArray(): void
111+
{
112+
$object = new stdClass();
113+
$object->name = 'John';
114+
$object->age = 30;
115+
$result = mapify($object);
116+
$this->assertEquals(['name' => 'John', 'age' => 30], $result);
117+
}
118+
119+
public function testMapifyReturnsArrayWhenValueIsArray(): void
120+
{
121+
$array = ['key' => 'value', 'number' => 42];
122+
$result = mapify($array);
123+
$this->assertEquals($array, $result);
124+
}
125+
126+
public function testMapifyHandlesNumericKeys(): void
127+
{
128+
$array = [0 => 'first', 1 => 'second', 'name' => 'test'];
129+
$result = mapify($array);
130+
$expected = ['key_0' => 'first', 'key_1' => 'second', 'name' => 'test'];
131+
$this->assertEquals($expected, $result);
132+
}
133+
134+
public function testMapifyReturnsDefaultWhenValueIsNotArrayOrObject(): void
135+
{
136+
$value = 'not an array or object';
137+
$default = ['default' => 'value'];
138+
$result = mapify($value, $default);
139+
$this->assertEquals($default, $result);
140+
}
141+
142+
public function testMapifyReturnsEmptyArrayByDefault(): void
143+
{
144+
$value = 'not an array or object';
145+
$result = mapify($value);
146+
$this->assertEquals([], $result);
147+
}
148+
149+
public function testStringifyWithStringableObject(): void
150+
{
151+
$stringable = new class implements Stringable {
152+
public function __toString(): string
153+
{
154+
return 'stringable object';
155+
}
156+
};
157+
$result = stringify($stringable);
158+
$this->assertEquals('stringable object', $result);
159+
}
160+
161+
public function testStringifyWithToStringMethod(): void
162+
{
163+
$object = new class {
164+
public function __toString(): string
165+
{
166+
return 'object with toString';
167+
}
168+
};
169+
$result = stringify($object);
170+
$this->assertEquals('object with toString', $result);
171+
}
172+
173+
public function testStringifyWithScalarValues(): void
174+
{
175+
$this->assertEquals('123', stringify(123));
176+
$this->assertEquals('123.45', stringify(123.45));
177+
$this->assertEquals('1', stringify(true));
178+
$this->assertEquals('', stringify(false));
179+
}
180+
181+
public function testIntegerifyWithNumericString(): void
182+
{
183+
$this->assertEquals(123, integerify('123'));
184+
$this->assertEquals(123, integerify('123.45'));
185+
$this->assertEquals(0, integerify('not numeric'));
186+
}
187+
}

0 commit comments

Comments
 (0)