Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare tests for running in process isolation #329

Merged
merged 1 commit into from Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .phpcs.xml.dist
Expand Up @@ -40,6 +40,11 @@
<exclude-pattern>src/Validator/Spec/Tag/*_.php</exclude-pattern>
</rule>

<!-- Method names for overrides need to stick with their signature independently of the standard. -->
<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
<exclude-pattern>tests/Validator/Spec/Section/*.php</exclude-pattern>
</rule>

<!-- WordPress Coding Standards for Inline Documentation and Comments -->
<rule ref="WordPress-Docs">
<!-- This does not allow for useful @see and @todo tags with surrounding remarks. -->
Expand Down
67 changes: 35 additions & 32 deletions tests/Dom/DocumentTest.php
Expand Up @@ -874,76 +874,79 @@ public function testHasInitialAmpDevMode($document, $hasInitialDevMode)
*/
public function getGetElementIdData()
{
$elementFactory = static function ($dom, $id = null) {
$element = $dom->createElement('div');

if ($id) {
$element->setAttribute('id', $id);
}

$dom->body->appendChild($element);

return $element;
};

return [
'single check with existing ID' => [
[
[ $elementFactory, 'my-id', 'some-prefix', 'my-id' ],
[ true, 'my-id', 'some-prefix', 'my-id' ],
],
],

'single check without existing ID' => [
[
[ $elementFactory, null, 'some-prefix', 'some-prefix-0' ],
[ true, null, 'some-prefix', 'some-prefix-0' ],
],
],

'consecutive checks count upwards' => [
[
[ $elementFactory, null, 'some-prefix', 'some-prefix-0' ],
[ $elementFactory, null, 'some-prefix', 'some-prefix-1' ],
[ true, null, 'some-prefix', 'some-prefix-0' ],
[ true, null, 'some-prefix', 'some-prefix-1' ],
],
],

'consecutive checks for same element return same ID' => [
[
[ $elementFactory, null, 'some-prefix', 'some-prefix-0' ],
[ null, null, 'some-prefix', 'some-prefix-0' ],
[ true, null, 'some-prefix', 'some-prefix-0' ],
[ false, null, 'some-prefix', 'some-prefix-0' ],
],
],

'mixing prefixes keeps counts separate' => [
[
[ $elementFactory, 'my-id', 'some-prefix', 'my-id' ],
[ $elementFactory, null, 'some-prefix', 'some-prefix-0' ],
[ $elementFactory, null, 'some-prefix', 'some-prefix-1' ],
[ $elementFactory, null, 'other-prefix', 'other-prefix-0' ],
[ $elementFactory, null, 'other-prefix', 'other-prefix-1' ],
[ $elementFactory, null, 'some-prefix', 'some-prefix-2' ],
[ $elementFactory, 'another-id', 'some-prefix', 'another-id' ],
[ $elementFactory, null, 'some-prefix', 'some-prefix-3' ],
[ null, null, 'some-prefix', 'some-prefix-3' ],
[ true, 'my-id', 'some-prefix', 'my-id' ],
[ true, null, 'some-prefix', 'some-prefix-0' ],
[ true, null, 'some-prefix', 'some-prefix-1' ],
[ true, null, 'other-prefix', 'other-prefix-0' ],
[ true, null, 'other-prefix', 'other-prefix-1' ],
[ true, null, 'some-prefix', 'some-prefix-2' ],
[ true, 'another-id', 'some-prefix', 'another-id' ],
[ true, null, 'some-prefix', 'some-prefix-3' ],
[ false, null, 'some-prefix', 'some-prefix-3' ],
],
],
];
}

private function elementFactory($dom, $id = null)
{
$element = $dom->createElement('div');

if ($id) {
$element->setAttribute('id', $id);
}

$dom->body->appendChild($element);

return $element;
}

/**
* Test Document::getElementId().
*
* @dataProvider getGetElementIdData
* @covers \AmpProject\Dom\Document::getElementId()
*
* @param array $checks Checks to perform. Each check is an array containing an element, a prefix and an expected ID.
* @param array $checks Checks to perform. Each check is an array containing a boolean whether create a new element,
* a prefix and an expected ID.
*/
public function testGetElementId($checks)
{
$dom = new Document();
foreach ($checks as list($elementFactory, $id, $prefix, $expected)) {
$dom = new Document();
$element = null;
foreach ($checks as list($newElement, $id, $prefix, $expected)) {
// If no element factory was passed, just reuse the previous element.
if ($elementFactory) {
$element = $elementFactory($dom, $id);
if ($newElement) {
$element = $this->elementFactory($dom, $id);
}

$actual = $dom->getElementId($element, $prefix);
Expand Down
107 changes: 65 additions & 42 deletions tests/Optimizer/Transformer/TransformedIdentifierTest.php
Expand Up @@ -28,57 +28,23 @@ final class TransformedIdentifierTest extends TestCase
*/
public function dataTransform()
{
$input = static function ($html) {
return TestMarkup::DOCTYPE . $html . '<head>'
. TestMarkup::META_CHARSET . TestMarkup::META_VIEWPORT . TestMarkup::SCRIPT_AMPRUNTIME
. TestMarkup::LINK_FAVICON . TestMarkup::LINK_CANONICAL . TestMarkup::STYLE_AMPBOILERPLATE . TestMarkup::NOSCRIPT_AMPBOILERPLATE
. '</head><body></body></html>';
};

$expected = static function ($html) {
return TestMarkup::DOCTYPE . $html . '<head>'
. TestMarkup::META_CHARSET . TestMarkup::META_VIEWPORT . TestMarkup::SCRIPT_AMPRUNTIME
. TestMarkup::LINK_FAVICON . TestMarkup::LINK_CANONICAL . TestMarkup::STYLE_AMPBOILERPLATE . TestMarkup::NOSCRIPT_AMPBOILERPLATE
. '</head><body></body></html>';
};

return [
'adds identifier with default version to html tag' => [
$input('<html ⚡>'),
$expected('<html ⚡ transformed="self;v=1">'),
'<html ⚡>',
'<html ⚡ transformed="self;v=1">',
],

'adds identifier with custom version to html tag' => [
$input('<html ⚡>'),
$expected('<html ⚡ transformed="self;v=5">'),
'<html ⚡>',
'<html ⚡ transformed="self;v=5">',
[ 'version' => 5 ],
],

'adds identifier without version to html tag' => [
$input('<html ⚡>'),
$expected('<html ⚡ transformed="self">'),
'<html ⚡>',
'<html ⚡ transformed="self">',
[ 'version' => 0 ],
],

'enforces CSS max byte count by default' => [
$input('<html amp style="color:black">'),
$expected('<html amp style="color:black" transformed="self;v=1">'),
[],
function (Document $document) {
$this->assertTrue($document->isCssMaxByteCountEnforced());
$this->assertEquals(AmpTransformed::SPEC[SpecRule::MAX_BYTES] - strlen('color:black'), $document->getRemainingCustomCssSpace());
}
],

'allows skipping enforcement of CSS max byte count' => [
$input('<html data-ampdevmode>'),
$expected('<html data-ampdevmode transformed="self;v=1">'),
[ 'enforcedCssMaxByteCount' => false ],
function (Document $document) {
$this->assertFalse($document->isCssMaxByteCountEnforced());
$this->assertEquals(PHP_INT_MAX, $document->getRemainingCustomCssSpace());
}
],
];
}

Expand All @@ -95,15 +61,72 @@ function (Document $document) {
*/
public function testTransform($source, $expectedHtml, $config = [], $assert = null)
{
$document = Document::fromHtml($source);
$document = Document::fromHtml($this->fullHtml($source));
$transformer = new TransformedIdentifier(new TransformedIdentifierConfiguration($config));
$errors = new ErrorCollection();

$transformer->transform($document, $errors);

$this->assertEqualMarkup($expectedHtml, $document->saveHTML());
$this->assertEqualMarkup($this->fullHtml($expectedHtml), $document->saveHTML());
if ($assert) {
$assert($document);
}
}

/**
* @covers \AmpProject\Optimizer\Transformer\TransformedIdentifier::transform()
*/
public function testItEnforcesCssMaxByteCountByDefault()
{
$document = Document::fromHtml($this->fullHtml('<html amp style="color:black">'));
$transformer = new TransformedIdentifier(new TransformedIdentifierConfiguration([]));
$errors = new ErrorCollection();

$transformer->transform($document, $errors);

$this->assertEqualMarkup(
$this->fullHtml('<html amp style="color:black" transformed="self;v=1">'),
$document->saveHTML()
);
$this->assertTrue($document->isCssMaxByteCountEnforced());
$this->assertEquals(
AmpTransformed::SPEC[SpecRule::MAX_BYTES] - strlen('color:black'),
$document->getRemainingCustomCssSpace()
);
}

/**
* @covers \AmpProject\Optimizer\Transformer\TransformedIdentifier::transform()
*/
public function testItAllowsSkippingEnforcementOfCssMaxByteCount()
{
$document = Document::fromHtml($this->fullHtml('<html data-ampdevmode>'));
$transformer = new TransformedIdentifier(
new TransformedIdentifierConfiguration([ 'enforcedCssMaxByteCount' => false ])
);
$errors = new ErrorCollection();

$transformer->transform($document, $errors);

$this->assertEqualMarkup(
$this->fullHtml('<html data-ampdevmode transformed="self;v=1">'),
$document->saveHTML()
);
$this->assertFalse($document->isCssMaxByteCountEnforced());
$this->assertEquals(PHP_INT_MAX, $document->getRemainingCustomCssSpace());
}

/**
* Get the full HTML document based on the HTML tag to use.
*
* @param string $html HTML tag to use.
* @return string Full HTML document to use.
*/
private function fullHtml($html)
{
return TestMarkup::DOCTYPE . $html . '<head>'
. TestMarkup::META_CHARSET . TestMarkup::META_VIEWPORT . TestMarkup::SCRIPT_AMPRUNTIME
. TestMarkup::LINK_FAVICON . TestMarkup::LINK_CANONICAL . TestMarkup::STYLE_AMPBOILERPLATE . TestMarkup::NOSCRIPT_AMPBOILERPLATE
. '</head><body></body></html>';
}
}
13 changes: 10 additions & 3 deletions tests/Validator/Spec/Section/AttributeListsTest.php
Expand Up @@ -18,10 +18,17 @@ class AttributeListsTest extends TestCase
/** @var AttributeLists */
private $attributeLists;

public function __construct()
/**
* Sets up the fixture, for example, open a network connection.
*
* This method is called before each test.
*
* @return void
*/
protected function set_up()
{
parent::__construct();
$spec = new Spec();
parent::set_up();
$spec = new Spec();
$this->attributeLists = $spec->attributeLists();
}

Expand Down
11 changes: 9 additions & 2 deletions tests/Validator/Spec/Section/CssSpecRulesTest.php
Expand Up @@ -19,9 +19,16 @@ class CssSpecRulesTest extends TestCase
/** @var CssRulesets */
private $cssRulesets;

public function __construct()
/**
* Sets up the fixture, for example, open a network connection.
*
* This method is called before each test.
*
* @return void
*/
protected function set_up()
{
parent::__construct();
parent::set_up();
$spec = new Spec();
$this->cssRulesets = $spec->cssRulesets();
}
Expand Down
13 changes: 10 additions & 3 deletions tests/Validator/Spec/Section/DeclarationListsTest.php
Expand Up @@ -17,10 +17,17 @@ class DeclarationListsTest extends TestCase
/** @var DeclarationLists */
private $declarationLists;

public function __construct()
/**
* Sets up the fixture, for example, open a network connection.
*
* This method is called before each test.
*
* @return void
*/
protected function set_up()
{
parent::__construct();
$spec = new Spec();
parent::set_up();
$spec = new Spec();
$this->declarationLists = $spec->declarationLists();
}

Expand Down
11 changes: 9 additions & 2 deletions tests/Validator/Spec/Section/DescendantTagListsTest.php
Expand Up @@ -18,9 +18,16 @@ class DescendantTagListsTest extends TestCase
/** @var DescendantTagLists */
private $descendantTagLists;

public function __construct()
/**
* Sets up the fixture, for example, open a network connection.
*
* This method is called before each test.
*
* @return void
*/
protected function set_up()
{
parent::__construct();
parent::set_up();
$spec = new Spec();
$this->descendantTagLists = $spec->descendantTagLists();
}
Expand Down
11 changes: 9 additions & 2 deletions tests/Validator/Spec/Section/DocRulesetsTest.php
Expand Up @@ -19,9 +19,16 @@ class DocRulesetsTest extends TestCase
/** @var DocRulesets */
private $docRulesets;

public function __construct()
/**
* Sets up the fixture, for example, open a network connection.
*
* This method is called before each test.
*
* @return void
*/
protected function set_up()
{
parent::__construct();
parent::set_up();
$spec = new Spec();
$this->docRulesets = $spec->docRulesets();
}
Expand Down
11 changes: 9 additions & 2 deletions tests/Validator/Spec/Section/ErrorsTest.php
Expand Up @@ -18,9 +18,16 @@ class ErrorsTest extends TestCase
/** @var Errors */
private $errors;

public function __construct()
/**
* Sets up the fixture, for example, open a network connection.
*
* This method is called before each test.
*
* @return void
*/
protected function set_up()
{
parent::__construct();
parent::set_up();
$spec = new Spec();
$this->errors = $spec->errors();
}
Expand Down