From fdadf3ba07ccb01f980c017d393043c6e6451105 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 14 Jan 2023 22:37:19 +0100 Subject: [PATCH] Created unit test for DeviceLongUrlsValidator --- .../DeviceLongUrlsValidatorTest.php | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 module/Core/test/ShortUrl/Model/Validation/DeviceLongUrlsValidatorTest.php diff --git a/module/Core/test/ShortUrl/Model/Validation/DeviceLongUrlsValidatorTest.php b/module/Core/test/ShortUrl/Model/Validation/DeviceLongUrlsValidatorTest.php new file mode 100644 index 00000000..42ad720b --- /dev/null +++ b/module/Core/test/ShortUrl/Model/Validation/DeviceLongUrlsValidatorTest.php @@ -0,0 +1,75 @@ +attach(new NotEmpty()); + + $this->validator = new DeviceLongUrlsValidator($longUrlValidators); + } + + /** + * @test + * @dataProvider provideNonArrayValues + */ + public function nonArrayValuesAreNotValid(mixed $invalidValue): void + { + self::assertFalse($this->validator->isValid($invalidValue)); + self::assertEquals(['NOT_ARRAY' => 'Provided value is not an array.'], $this->validator->getMessages()); + } + + public function provideNonArrayValues(): iterable + { + yield 'int' => [0]; + yield 'float' => [100.45]; + yield 'string' => ['foo']; + yield 'boolean' => [true]; + yield 'object' => [new stdClass()]; + yield 'null' => [null]; + } + + /** @test */ + public function unrecognizedKeysAreNotValid(): void + { + self::assertFalse($this->validator->isValid(['foo' => 'bar'])); + self::assertEquals( + ['INVALID_DEVICE' => 'You have provided at least one invalid device identifier.'], + $this->validator->getMessages(), + ); + } + + /** @test */ + public function everyUrlMustMatchLongUrlValidator(): void + { + self::assertFalse($this->validator->isValid([DeviceType::ANDROID->value => ''])); + self::assertEquals( + ['INVALID_LONG_URL' => 'At least one of the long URLs are invalid.'], + $this->validator->getMessages(), + ); + } + + /** @test */ + public function validValuesResultInValidResult(): void + { + self::assertTrue($this->validator->isValid([ + DeviceType::ANDROID->value => 'foo', + DeviceType::IOS->value => 'bar', + DeviceType::DESKTOP->value => 'baz', + ])); + } +}