ValidatorTest.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Url\Test\Unit;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. class ValidatorTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /** @var \Magento\Framework\Url\Validator */
  11. protected $object;
  12. /** @var \Zend\Validator\Uri */
  13. protected $zendValidator;
  14. /** @var string[] */
  15. protected $expectedValidationMessages = ['invalidUrl' => "Invalid URL '%value%'."];
  16. protected function setUp()
  17. {
  18. $objectManager = new ObjectManager($this);
  19. $this->zendValidator = $this->createMock(\Zend\Validator\Uri::class);
  20. $this->object = $objectManager->getObject(
  21. \Magento\Framework\Url\Validator::class,
  22. ['validator' => $this->zendValidator]
  23. );
  24. }
  25. public function testConstruct()
  26. {
  27. $this->assertEquals($this->expectedValidationMessages, $this->object->getMessageTemplates());
  28. }
  29. public function testIsValidWhenValid()
  30. {
  31. $this->zendValidator
  32. ->method('isValid')
  33. ->with('http://example.com')
  34. ->willReturn(true);
  35. $this->assertTrue($this->object->isValid('http://example.com'));
  36. $this->assertEquals([], $this->object->getMessages());
  37. }
  38. public function testIsValidWhenInvalid()
  39. {
  40. $this->zendValidator
  41. ->method('isValid')
  42. ->with('%value%')
  43. ->willReturn(false);
  44. $this->assertFalse($this->object->isValid('%value%'));
  45. $this->assertEquals($this->expectedValidationMessages, $this->object->getMessages());
  46. }
  47. }