Skip to content

Commit

Permalink
Add cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Mar 4, 2024
1 parent 61823b2 commit 5e9630b
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 6 deletions.
16 changes: 16 additions & 0 deletions Test/Integration/AbstractTestCase.php
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'));
}
}
55 changes: 55 additions & 0 deletions Test/Integration/BodyTest.php
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);
}
}
19 changes: 19 additions & 0 deletions Test/Integration/LinkDataProvider.php
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'],
];
}
}
6 changes: 0 additions & 6 deletions etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,4 @@
<type name="Magento\Framework\App\ResponseInterface">
<plugin name="linkpreload-response" type="Yireo\LinkPreload\Plugin\ResponsePlugin" sortOrder="99"/>
</type>

<!--
<type name="Magento\Framework\View\Result\Layout">
<plugin name="linkpreload-result" type="Yireo\LinkPreload\Plugin\LayoutPlugin" sortOrder="99"/>
</type>
-->
</config>

0 comments on commit 5e9630b

Please sign in to comment.