WebLinkExtensionTest.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\Bridge\Twig\Tests\Extension;
  11. use Fig\Link\Link;
  12. use PHPUnit\Framework\TestCase;
  13. use Symfony\Bridge\Twig\Extension\WebLinkExtension;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. /**
  17. * @author Kévin Dunglas <dunglas@gmail.com>
  18. */
  19. class WebLinkExtensionTest extends TestCase
  20. {
  21. /**
  22. * @var Request
  23. */
  24. private $request;
  25. /**
  26. * @var WebLinkExtension
  27. */
  28. private $extension;
  29. protected function setUp()
  30. {
  31. $this->request = new Request();
  32. $requestStack = new RequestStack();
  33. $requestStack->push($this->request);
  34. $this->extension = new WebLinkExtension($requestStack);
  35. }
  36. public function testLink()
  37. {
  38. $this->assertEquals('/foo.css', $this->extension->link('/foo.css', 'preload', array('as' => 'style', 'nopush' => true)));
  39. $link = (new Link('preload', '/foo.css'))->withAttribute('as', 'style')->withAttribute('nopush', true);
  40. $this->assertEquals(array($link), array_values($this->request->attributes->get('_links')->getLinks()));
  41. }
  42. public function testPreload()
  43. {
  44. $this->assertEquals('/foo.css', $this->extension->preload('/foo.css', array('as' => 'style', 'crossorigin' => true)));
  45. $link = (new Link('preload', '/foo.css'))->withAttribute('as', 'style')->withAttribute('crossorigin', true);
  46. $this->assertEquals(array($link), array_values($this->request->attributes->get('_links')->getLinks()));
  47. }
  48. public function testDnsPrefetch()
  49. {
  50. $this->assertEquals('/foo.css', $this->extension->dnsPrefetch('/foo.css', array('as' => 'style', 'crossorigin' => true)));
  51. $link = (new Link('dns-prefetch', '/foo.css'))->withAttribute('as', 'style')->withAttribute('crossorigin', true);
  52. $this->assertEquals(array($link), array_values($this->request->attributes->get('_links')->getLinks()));
  53. }
  54. public function testPreconnect()
  55. {
  56. $this->assertEquals('/foo.css', $this->extension->preconnect('/foo.css', array('as' => 'style', 'crossorigin' => true)));
  57. $link = (new Link('preconnect', '/foo.css'))->withAttribute('as', 'style')->withAttribute('crossorigin', true);
  58. $this->assertEquals(array($link), array_values($this->request->attributes->get('_links')->getLinks()));
  59. }
  60. public function testPrefetch()
  61. {
  62. $this->assertEquals('/foo.css', $this->extension->prefetch('/foo.css', array('as' => 'style', 'crossorigin' => true)));
  63. $link = (new Link('prefetch', '/foo.css'))->withAttribute('as', 'style')->withAttribute('crossorigin', true);
  64. $this->assertEquals(array($link), array_values($this->request->attributes->get('_links')->getLinks()));
  65. }
  66. public function testPrerender()
  67. {
  68. $this->assertEquals('/foo.css', $this->extension->prerender('/foo.css', array('as' => 'style', 'crossorigin' => true)));
  69. $link = (new Link('prerender', '/foo.css'))->withAttribute('as', 'style')->withAttribute('crossorigin', true);
  70. $this->assertEquals(array($link), array_values($this->request->attributes->get('_links')->getLinks()));
  71. }
  72. }