|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Theme\Test\Unit\Controller\Result; |
| 9 | + |
| 10 | +use Magento\Theme\Controller\Result\AsyncCssPlugin; |
| 11 | +use Magento\Framework\App\Response\Http; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | +use PHPUnit\Framework\MockObject\MockObject; |
| 14 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 15 | +use Magento\Store\Model\ScopeInterface; |
| 16 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 17 | + |
| 18 | +/** |
| 19 | + * Unit test for Magento\Theme\Test\Unit\Controller\Result\AsyncCssPlugin. |
| 20 | + */ |
| 21 | +class AsyncCssPluginTest extends TestCase |
| 22 | +{ |
| 23 | + const STUB_XML_PATH_USE_CSS_CRITICAL_PATH = 'dev/css/use_css_critical_path'; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var AsyncCssPlugin |
| 27 | + */ |
| 28 | + private $plugin; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var ScopeConfigInterface|MockObject |
| 32 | + */ |
| 33 | + private $scopeConfigMock; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var Http|MockObject |
| 37 | + */ |
| 38 | + private $httpMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * @inheritdoc |
| 42 | + */ |
| 43 | + protected function setUp(): void |
| 44 | + { |
| 45 | + $this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class) |
| 46 | + ->setMethods(['isSetFlag']) |
| 47 | + ->disableOriginalConstructor() |
| 48 | + ->getMockForAbstractClass(); |
| 49 | + |
| 50 | + $this->httpMock = $this->createMock(Http::class); |
| 51 | + |
| 52 | + $objectManager = new ObjectManagerHelper($this); |
| 53 | + $this->plugin = $objectManager->getObject( |
| 54 | + AsyncCssPlugin::class, |
| 55 | + [ |
| 56 | + 'scopeConfig' => $this->scopeConfigMock |
| 57 | + ] |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Data Provider for before send response |
| 63 | + * |
| 64 | + * @return array |
| 65 | + */ |
| 66 | + public function sendResponseDataProvider(): array |
| 67 | + { |
| 68 | + return [ |
| 69 | + [ |
| 70 | + "content" => "<body><h1>Test Title</h1>" . |
| 71 | + "<link rel=\"stylesheet\" href=\"css/critical.css\" />" . |
| 72 | + "<p>Test Content</p></body>", |
| 73 | + "flag" => true, |
| 74 | + "result" => "<body><h1>Test Title</h1>" . |
| 75 | + "<link rel=\"preload\" as=\"style\" media=\"all\"" . |
| 76 | + " onload=\"this.onload=null;this.rel='stylesheet'\" href=\"css/critical.css\" />" . |
| 77 | + "<p>Test Content</p>" . |
| 78 | + "<link rel=\"stylesheet\" href=\"css/critical.css\" />" . |
| 79 | + "\n</body>" |
| 80 | + ], |
| 81 | + [ |
| 82 | + "content" => "<body><p>Test Content</p></body>", |
| 83 | + "flag" => false, |
| 84 | + "result" => "<body><p>Test Content</p></body>" |
| 85 | + ], |
| 86 | + [ |
| 87 | + "content" => "<body><p>Test Content</p></body>", |
| 88 | + "flag" => true, |
| 89 | + "result" => "<body><p>Test Content</p></body>" |
| 90 | + ] |
| 91 | + ]; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Test beforeSendResponse |
| 96 | + * |
| 97 | + * @param string $content |
| 98 | + * @param bool $isSetFlag |
| 99 | + * @param string $result |
| 100 | + * @return void |
| 101 | + * @dataProvider sendResponseDataProvider |
| 102 | + */ |
| 103 | + public function testBeforeSendResponse($content, $isSetFlag, $result): void |
| 104 | + { |
| 105 | + $this->httpMock->expects($this->once()) |
| 106 | + ->method('getContent') |
| 107 | + ->willReturn($content); |
| 108 | + |
| 109 | + $this->scopeConfigMock->expects($this->once()) |
| 110 | + ->method('isSetFlag') |
| 111 | + ->with( |
| 112 | + self::STUB_XML_PATH_USE_CSS_CRITICAL_PATH, |
| 113 | + ScopeInterface::SCOPE_STORE |
| 114 | + ) |
| 115 | + ->willReturn($isSetFlag); |
| 116 | + |
| 117 | + $this->httpMock->expects($this->any()) |
| 118 | + ->method('setContent') |
| 119 | + ->with($result); |
| 120 | + |
| 121 | + $this->plugin->beforeSendResponse($this->httpMock); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Test BeforeSendResponse if content is not a string |
| 126 | + * |
| 127 | + * @return void |
| 128 | + */ |
| 129 | + public function testIfGetContentIsNotAString(): void |
| 130 | + { |
| 131 | + $this->httpMock->expects($this->once()) |
| 132 | + ->method('getContent') |
| 133 | + ->willReturn([]); |
| 134 | + |
| 135 | + $this->scopeConfigMock->expects($this->any()) |
| 136 | + ->method('isSetFlag') |
| 137 | + ->with( |
| 138 | + self::STUB_XML_PATH_USE_CSS_CRITICAL_PATH, |
| 139 | + ScopeInterface::SCOPE_STORE |
| 140 | + ) |
| 141 | + ->willReturn(false); |
| 142 | + |
| 143 | + $this->plugin->beforeSendResponse($this->httpMock); |
| 144 | + } |
| 145 | +} |
0 commit comments