ImageTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DomCrawler\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DomCrawler\Image;
  13. class ImageTest extends TestCase
  14. {
  15. /**
  16. * @expectedException \LogicException
  17. */
  18. public function testConstructorWithANonImgTag()
  19. {
  20. $dom = new \DOMDocument();
  21. $dom->loadHTML('<html><div><div></html>');
  22. new Image($dom->getElementsByTagName('div')->item(0), 'http://www.example.com/');
  23. }
  24. public function testBaseUriIsOptionalWhenImageUrlIsAbsolute()
  25. {
  26. $dom = new \DOMDocument();
  27. $dom->loadHTML('<html><img alt="foo" src="https://example.com/foo" /></html>');
  28. $image = new Image($dom->getElementsByTagName('img')->item(0));
  29. $this->assertSame('https://example.com/foo', $image->getUri());
  30. }
  31. /**
  32. * @expectedException \InvalidArgumentException
  33. */
  34. public function testAbsoluteBaseUriIsMandatoryWhenImageUrlIsRelative()
  35. {
  36. $dom = new \DOMDocument();
  37. $dom->loadHTML('<html><img alt="foo" src="/foo" /></html>');
  38. $image = new Image($dom->getElementsByTagName('img')->item(0), 'example.com');
  39. $image->getUri();
  40. }
  41. /**
  42. * @dataProvider getGetUriTests
  43. */
  44. public function testGetUri($url, $currentUri, $expected)
  45. {
  46. $dom = new \DOMDocument();
  47. $dom->loadHTML(sprintf('<html><img alt="foo" src="%s" /></html>', $url));
  48. $image = new Image($dom->getElementsByTagName('img')->item(0), $currentUri);
  49. $this->assertEquals($expected, $image->getUri());
  50. }
  51. public function getGetUriTests()
  52. {
  53. return [
  54. ['/foo.png', 'http://localhost/bar/foo/', 'http://localhost/foo.png'],
  55. ['foo.png', 'http://localhost/bar/foo/', 'http://localhost/bar/foo/foo.png'],
  56. ];
  57. }
  58. }