Skip to content

Commit 2523993

Browse files
author
aleksandr.shelestov
committed
Test fixes
1 parent 2b6842f commit 2523993

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

Diff for: src/FieldTypes/Validators/RegExpValidator.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55

66
public function __construct(
77
private ?string $pattern = null
8-
) {
9-
}
8+
) {}
109

11-
public function validate($value): bool {
10+
public function validate($value): bool
11+
{
1212
if ($this->pattern === null) {
1313
return true;
1414
}
1515
return preg_match('!' . $this->pattern . '!u', $value) === 1;
1616
}
1717

18-
public function getErrorMessage(): string {
18+
public function getErrorMessage(): string
19+
{
1920
return "The value does not match the required pattern.";
2021
}
2122
}

Diff for: src/Service/FormSubmissionService.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ public function submit(int $formId, array $data): ?array
5151

5252
if (count($errors) === 0) {
5353
$submission = $this->submissionRepository->create($formId, $data);
54-
$this->eventDispatcher->dispatch(new NewSubmissionEvent($submission), 'submission.new');
54+
if ($submission) {
55+
$this->eventDispatcher->dispatch(new NewSubmissionEvent($submission), 'submission.new');
56+
}
5557
}
5658

5759
return [

Diff for: tests/Unit/FieldTypes/Validators/RegExpValidatorTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class RegExpValidatorTest extends TestCase
1010
public function testValidRegExp()
1111
{
1212
// Example with an email pattern
13-
$validator = new RegExpValidator('/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/');
13+
$validator = new RegExpValidator('^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$');
1414

1515
$this->assertTrue($validator->validate('[email protected]'));
1616
$this->assertTrue($validator->validate('[email protected]'));
@@ -19,7 +19,7 @@ public function testValidRegExp()
1919
public function testInvalidRegExp()
2020
{
2121
// Example with an email pattern
22-
$validator = new RegExpValidator('/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,4}$/');
22+
$validator = new RegExpValidator('^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,4}$');
2323

2424
$this->assertFalse($validator->validate('plainaddress'));
2525
$this->assertFalse($validator->validate('@no-local-part.com'));
@@ -29,12 +29,12 @@ public function testInvalidRegExp()
2929
public function testDifferentPatterns()
3030
{
3131
// Example with a numeric pattern
32-
$numericValidator = new RegExpValidator('/^\d+$/');
32+
$numericValidator = new RegExpValidator('^\d+$');
3333
$this->assertTrue($numericValidator->validate('12345'));
3434
$this->assertFalse($numericValidator->validate('abc123'));
3535

3636
// Example with a specific format (e.g., date in YYYY-MM-DD)
37-
$dateValidator = new RegExpValidator('/^\d{4}-\d{2}-\d{2}$/');
37+
$dateValidator = new RegExpValidator('^\d{4}-\d{2}-\d{2}$');
3838
$this->assertTrue($dateValidator->validate('2022-01-01'));
3939
$this->assertFalse($dateValidator->validate('01-01-2022'));
4040
}

Diff for: tests/Unit/Service/FormSubmissionServiceTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Service\FormFieldTypeService;
99
use App\Service\FormSubmissionService;
1010
use PHPUnit\Framework\TestCase;
11+
use Symfony\Component\EventDispatcher\EventDispatcher;
1112

1213
class FormSubmissionServiceTest extends TestCase
1314
{
@@ -322,6 +323,7 @@ public function testUrlFieldTypeValidation()
322323
$formFieldService,
323324
new FormFieldTypeService(new FieldTypesFabric()),
324325
$this->mockSubmissionRepository(),
326+
$this->mockEventDispatcher(),
325327
);
326328

327329
$tests = [
@@ -357,12 +359,21 @@ private function mockSubmissionRepository()
357359
return $submissionRepository;
358360
}
359361

362+
private function mockEventDispatcher()
363+
{
364+
$mock = $this->createMock(EventDispatcher::class);
365+
$mock->method('dispatch')->willReturn((object)[]);
366+
367+
return $mock;
368+
}
369+
360370
private function createSubmissionService(array $formFields): FormSubmissionService
361371
{
362372
return new FormSubmissionService(
363373
$this->mockFormFieldService($formFields),
364374
new FormFieldTypeService(new FieldTypesFabric()),
365375
$this->mockSubmissionRepository(),
376+
$this->mockEventDispatcher(),
366377
);
367378
}
368379
}

0 commit comments

Comments
 (0)