ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
InArrayTransformationTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use PHPUnit\Framework\TestCase;
27use UnexpectedValueException;
28use PHPUnit\Framework\Attributes\DataProvider;
29
30class InArrayTransformationTest extends TestCase
31{
32 public function testConstruct(): void
33 {
34 $language = $this->getMockBuilder(Language::class)->disableOriginalConstructor()->getMock();
35 $this->assertInstanceOf(InArrayTransformation::class, new InArrayTransformation([], $language));
36 }
37
38 #[DataProvider('memberProvider')]
39 public function testAccept(string $value, bool $successful): void
40 {
41 $language = $this->getMockBuilder(Language::class)->disableOriginalConstructor()->getMock();
42 $transformation = new InArrayTransformation(['foo', 'bar'], $language);
43
44 $this->assertSame($successful, $transformation->accepts($value));
45 }
46
47 #[DataProvider('memberProvider')]
48 public function testTransform(string $value, bool $successful): void
49 {
50 if (!$successful) {
51 $this->expectException(UnexpectedValueException::class);
52 }
53
54 $language = $this->getMockBuilder(Language::class)->disableOriginalConstructor()->getMock();
55 $transformation = new InArrayTransformation(['foo', 'bar'], $language);
56
57 $this->assertSame($value, $transformation->transform($value));
58 }
59
60 #[DataProvider('memberProvider')]
61 public function testApplyTo(string $value, bool $successful): void
62 {
63 $language = $this->getMockBuilder(Language::class)->disableOriginalConstructor()->getMock();
64 $transformation = new InArrayTransformation(['foo', 'bar'], $language);
65
66 $result = $transformation->applyTo(new Ok($value));
67 $this->assertSame($successful, $result->isOk());
68 if ($successful) {
69 $this->assertSame($value, $result->value());
70 } else {
71 $this->assertInstanceOf(UnexpectedValueException::class, $result->error());
72 }
73 }
74
75 public static function memberProvider(): array
76 {
77 return [
78 'Invalid member.' => ['hej', false],
79 'Valid member.' => ['foo', true],
80 ];
81 }
82}
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:31
Validates that the value to be transformed is in the set given to this transformation.