-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61823b2
commit 5e9630b
Showing
4 changed files
with
90 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Yireo\LinkPreload\Test\Integration; | ||
|
||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\TestFramework\TestCase\AbstractController; | ||
|
||
class AbstractTestCase extends AbstractController | ||
{ | ||
protected function assertEnabledValue(int $expectedValue): void | ||
{ | ||
/** @var ScopeConfigInterface $scopeConfig */ | ||
$scopeConfig = $this->_objectManager->get(ScopeConfigInterface::class); | ||
$this->assertEquals($expectedValue, $scopeConfig->getValue('system/yireo_linkpreload/enabled')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Yireo\LinkPreload\Test\Integration; | ||
|
||
class BodyTest extends AbstractTestCase | ||
{ | ||
/** | ||
* @magentoAdminConfigFixture system/yireo_linkpreload/enabled 1 | ||
*/ | ||
public function testIfLinkHeadersExistsWhenModuleIsEnabled() | ||
{ | ||
$this->assertEnabledValue(1); | ||
$this->dispatch('/'); | ||
$body = (string)$this->getResponse()->getBody(); | ||
foreach ((new LinkDataProvider())->getLinks() as $link) { | ||
$this->assertBodyContainsLink($link[0], $link[1], $body); | ||
} | ||
} | ||
|
||
/** | ||
* @magentoAdminConfigFixture system/yireo_linkpreload/enabled 1 | ||
* @magentoCache full_page enabled | ||
* @magentoDbIsolation enabled | ||
* @magentoAppIsolation enabled | ||
*/ | ||
public function testIfLinkHeadersExistsWhenModuleIsEnabledAndWithFullPageCache() | ||
{ | ||
if (constant('TESTS_CLEANUP') === 'disabled') { | ||
$this->markTestSkipped('Test does not work with TESTS_CLEANUP disabled'); | ||
} | ||
|
||
$this->assertEnabledValue(1); | ||
$this->dispatch('/'); | ||
$body = (string)$this->getResponse()->getBody(); | ||
$this->assertNotEmpty($body); | ||
|
||
foreach ((new LinkDataProvider())->getLinks() as $link) { | ||
$this->assertBodyContainsLink($link[0], $link[1], $body); | ||
} | ||
} | ||
|
||
private function assertBodyContainsLink(string $type, string $uri, string $body) | ||
{ | ||
preg_match_all('#<link(.*)rel="preload"(.*)>#', $body, $matches); | ||
|
||
$foundUri = false; | ||
foreach ($matches[0] as $match) { | ||
if (strstr($match, $uri)) { | ||
$foundUri = true; | ||
} | ||
} | ||
|
||
$this->assertTrue($foundUri, 'URI "'.$uri.'" not found in body: '.$body); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Yireo\LinkPreload\Test\Integration; | ||
|
||
class LinkDataProvider | ||
{ | ||
public function getLinks(): array | ||
{ | ||
return [ | ||
['style', 'css/styles-l.css'], | ||
['style', 'css/styles-m.css'], | ||
['style', 'css/print.css'], | ||
['style', 'mage/calendar.css'], | ||
['script', 'requirejs/require.js'], | ||
['script', 'jquery.js'], | ||
['script', 'mage/bootstrap.js'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters