44 * @copyright Copyright (C) Ibexa AS. All rights reserved.
55 * @license For full copyright and license information view LICENSE file distributed with this source code.
66 */
7+ declare (strict_types=1 );
78
89namespace Ibexa \Tests \Core \Repository \ContentValidator ;
910
1011use Ibexa \Contracts \Core \Repository \Exceptions \InvalidArgumentException ;
1112use Ibexa \Contracts \Core \Repository \Validator \ContentValidator ;
1213use Ibexa \Contracts \Core \Repository \Values \ValueObject ;
14+ use Ibexa \Core \FieldType \ValidationError ;
1315use Ibexa \Core \Repository \Strategy \ContentValidator \ContentValidatorStrategy ;
1416use Ibexa \Core \Repository \Values \ObjectState \ObjectState ;
1517use PHPUnit \Framework \TestCase ;
1618
17- class ContentValidatorStrategyTest extends TestCase
19+ final class ContentValidatorStrategyTest extends TestCase
1820{
1921 public function testUnknownValidationObject (): void
2022 {
21- $ this ->expectException (InvalidArgumentException::class);
22- $ this ->expectExceptionMessage ('Argument \'$object \' is invalid: Validator for Ibexa\Core\Repository\Values\ObjectState\ObjectState type not found. ' );
23-
2423 $ contentValidatorStrategy = new ContentValidatorStrategy ([]);
24+
25+ $ this ->expectException (InvalidArgumentException::class);
26+ $ this ->expectExceptionMessage (
27+ 'Argument \'$object \' is invalid: Validator for Ibexa\Core\Repository\Values\ObjectState\ObjectState type not found. '
28+ );
2529 $ contentValidatorStrategy ->validate (new ObjectState ());
2630 }
2731
2832 public function testKnownValidationObject (): void
2933 {
3034 $ contentValidatorStrategy = new ContentValidatorStrategy ([
31- $ this ->buildContentValidator (ObjectState::class, [' test ' ]),
35+ $ this ->buildContentValidator (ObjectState::class, [1 => [ ' eng-GB ' => ' test '] ]),
3236 ]);
3337
3438 $ errors = $ contentValidatorStrategy ->validate (new ObjectState ());
35- self ::assertEquals ([' test ' ], $ errors );
39+ self ::assertEquals ([1 => [ ' eng-GB ' => new ValidationError ( ' test ')] ], $ errors );
3640 }
3741
3842 public function testSupportsUnknownValidationObject (): void
@@ -43,10 +47,10 @@ public function testSupportsUnknownValidationObject(): void
4347 self ::assertFalse ($ supports );
4448 }
4549
46- public function testSuportsKnownValidationObject (): void
50+ public function testSupportsKnownValidationObject (): void
4751 {
4852 $ contentValidatorStrategy = new ContentValidatorStrategy ([
49- $ this ->buildContentValidator (ObjectState::class, [' test ' ]),
53+ $ this ->buildContentValidator (ObjectState::class, [1 => [ ' eng-GB ' => ' test '] ]),
5054 ]);
5155
5256 $ supports = $ contentValidatorStrategy ->supports (new ObjectState ());
@@ -73,34 +77,31 @@ public function testMergeValidationErrors(): void
7377
7478 $ errors = $ contentValidatorStrategy ->validate (new ObjectState ());
7579 self ::assertEquals ([
76- 123 => ['eng-GB ' => '123-eng-GB ' ],
77- 321 => ['pol-PL ' => '321-pol-PL ' ],
80+ 123 => ['eng-GB ' => new ValidationError ( '123-eng-GB ' ) ],
81+ 321 => ['pol-PL ' => new ValidationError ( '321-pol-PL ' ) ],
7882 456 => [
79- 'pol-PL ' => '456-pol-PL ' ,
80- 'eng-GB ' => '456-eng-GB ' ,
83+ 'pol-PL ' => new ValidationError ( '456-pol-PL ' ) ,
84+ 'eng-GB ' => new ValidationError ( '456-eng-GB ' ) ,
8185 ],
82- 2345 => ['eng-GB ' => '2345-eng-GB ' ],
86+ 2345 => ['eng-GB ' => new ValidationError ( '2345-eng-GB ' ) ],
8387 ], $ errors );
8488 }
8589
90+ /**
91+ * @phpstan-param array<int, array<string, string>> $validationReturn
92+ */
8693 private function buildContentValidator (
8794 string $ classSupport ,
8895 array $ validationReturn
8996 ): ContentValidator {
90- return new class ($ classSupport , $ validationReturn ) implements ContentValidator {
91- /** @var string */
92- private $ classSupport ;
93-
94- /** @var array */
95- private $ validationReturn ;
96-
97+ return new readonly class ($ classSupport , $ validationReturn ) implements ContentValidator {
98+ /**
99+ * @param array<int, array<string, string>> $validationReturn
100+ */
97101 public function __construct (
98- string $ classSupport ,
99- array $ validationReturn
100- ) {
101- $ this ->classSupport = $ classSupport ;
102- $ this ->validationReturn = $ validationReturn ;
103- }
102+ private string $ classSupport ,
103+ private array $ validationReturn
104+ ) {}
104105
105106 public function supports (ValueObject $ object ): bool
106107 {
@@ -112,7 +113,14 @@ public function validate(
112113 array $ context = [],
113114 ?array $ fieldIdentifiers = null
114115 ): array {
115- return $ this ->validationReturn ;
116+ // map validation message string to an expected instance of ValidationError
117+ return array_map (
118+ static fn (array $ errors ): array => array_map (
119+ static fn (string $ error ): ValidationError => new ValidationError ($ error ),
120+ $ errors
121+ ),
122+ $ this ->validationReturn
123+ );
116124 }
117125 };
118126 }
0 commit comments