Skip to content

Commit cac5933

Browse files
committed
Creating BaseSchemaTest
1 parent 465b8a1 commit cac5933

File tree

2 files changed

+107
-2
lines changed

2 files changed

+107
-2
lines changed

src/Schemas/BaseSchema.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ public function validate(mixed $value): bool
8585

8686
if (!is_null($this->callback)) {
8787
$callback = $this->callback;
88-
if (!$callback($value)) {
89-
$this->errorMessages[] = $this->customMessages['refine'];
88+
$result = $callback($value);
89+
90+
if (!$result) {
91+
$this->error($this->customMessages['refine']);
9092
}
9193
}
9294

tests/Schemas/BaseSchemaTest.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Webdevcave\SchemaValidator\Tests\Schemas;
4+
5+
use BadMethodCallException;
6+
use PHPUnit\Framework\Attributes\CoversClass;
7+
use PHPUnit\Framework\TestCase;
8+
use Webdevcave\SchemaValidator\Schemas\BaseSchema;
9+
use Webdevcave\SchemaValidator\Validator;
10+
11+
class DummySchema extends BaseSchema
12+
{
13+
protected ?string $equals = null;
14+
15+
protected function validateSchema(mixed $value): void
16+
{
17+
if (!is_null($this->equals) && $this->equals !== $value) {
18+
$this->error($this->customMessages['equals'] ?? 'not equals');
19+
}
20+
}
21+
}
22+
23+
#[CoversClass(Validator::class)]
24+
#[CoversClass(BaseSchema::class)]
25+
class BaseSchemaTest extends TestCase
26+
{
27+
private ?BaseSchema $schema = null;
28+
29+
protected function setUp(): void
30+
{
31+
$this->schema = new DummySchema();
32+
}
33+
34+
public function testUndefinedChecksShouldThrowException(): void
35+
{
36+
$this->expectException(BadMethodCallException::class);
37+
$this->schema->inexistent('check');
38+
}
39+
40+
public function testSetGetRule(): void
41+
{
42+
$value = 'value';
43+
44+
$this->schema->equals($value);
45+
$this->assertEquals(
46+
$value, $this->schema->equals(),
47+
'Schema should be able to set and get a rule value'
48+
);
49+
}
50+
51+
public function testFailingConditions(): void
52+
{
53+
$customMessage = 'must be different';
54+
$this->schema->equals('1', $customMessage);
55+
56+
$successful = $this->schema->validate('2');
57+
$errors = $this->schema->errorMessages();
58+
$expected = [$customMessage];
59+
60+
$this->assertFalse($successful, 'Value should not be valid');
61+
$this->assertEquals(
62+
$expected,
63+
$errors,
64+
'Error messages does not match'
65+
);
66+
}
67+
68+
public function testOptionalValidation(): void
69+
{
70+
$this->schema->equals('value')->optional();
71+
72+
$this->assertTrue(
73+
$this->schema->validate(null),
74+
'Optional validation should be able to assert null'
75+
);
76+
}
77+
78+
public function testRequiredValidation(): void
79+
{
80+
$this->schema->equals('value');
81+
82+
$this->assertFalse(
83+
$this->schema->validate(null),
84+
'Requied validation should not be able to assert null'
85+
);
86+
}
87+
88+
public function testRefineValidation(): void
89+
{
90+
$this->schema->refine(function ($value) {
91+
return $value === 'Carlos';
92+
}, 'Incorrect');
93+
94+
$this->assertFalse(
95+
$this->schema->validate('Carl'),
96+
'Refine verification should not validate the current value'
97+
);
98+
$this->assertTrue(
99+
$this->schema->validate('Carlos'),
100+
'Value does not match to the one specified in refine method'
101+
);
102+
}
103+
}

0 commit comments

Comments
 (0)