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

[TASK] Pass debug setting on to CSS parser #1142

Merged
merged 4 commits into from
Jan 12, 2022
Merged

Conversation

JakeQZ
Copy link
Contributor

@JakeQZ JakeQZ commented Dec 21, 2021

This partially addresses both #1139 and #1140.

However, if the CSS contains nested at-rules, the debug setting won't currently
be passed on, due to MyIntervals/PHP-CSS-Parser#127.

Some tests have been adapted to cater for an exception in debug mode with only
some of the data.

@JakeQZ JakeQZ added this to the 7.0.0 milestone Dec 21, 2021
@JakeQZ JakeQZ self-assigned this Dec 21, 2021
src/Css/CssDocument.php Outdated Show resolved Hide resolved
tests/Unit/Css/CssDocumentTest.php Outdated Show resolved Hide resolved
tests/Unit/Css/CssDocumentTest.php Outdated Show resolved Hide resolved
$result = $subject->renderNonConditionalAtRules();

self::assertSame('', $result);
} catch (UnexpectedEOFException $exception) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let's use $this->expectException, expectExceptionCode and expectExceptionMessage instead.

Ditto for other tests that check for exceptions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

Copy link
Contributor Author

@JakeQZ JakeQZ Dec 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean below. (Now it's above again. I mean see other comment on this.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let's use $this->expectException, expectExceptionCode and expectExceptionMessage instead.

Though if there is a doNotExpectException method the test could be written without the try/catch - i.e. we expect an exception but if we reach some later point we'll make an assertion instead, and cancel the expectation of the exception. Is that possible?

Copy link
Contributor Author

@JakeQZ JakeQZ Dec 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though if there is a doNotExpectException method the test could be written without the try/catch ... Is that possible?

expectException(null) would do the trick, except that the parameter is not documented as nullable and a Psalm error is generated. (Actually it's not nullable at all and a TypeError is generated if null is passed. There doesn't seem to be a way to 'undo' expectException.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To test that an exception is not thrown, we can do one of the following things:

a) state that not throwing the exception is the main statement of the test, and use a @doesNotPerformAssertions annotation
b) state that the return value (or something similar) is the main statement of the test, and assert for that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we can't, it seems, have a 'throws exception' OR 'satisfies assertion' test. Or, at least, not easily.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then please let's test for one of the two. (We can also add the other case as a test and mark it as self::markAsSkipped() (if I remember the method name correctly).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I've change this (and the other similar test) to expect an exception, but if one isn't thrown mark the test as skipped - since in strict (debug) mode the CSS parser only throws an exception with some of the datasets, and we are already testing that the invalid rules from these datasets are dropped in lenient (non-debug) mode.


$result = $subject->renderNonConditionalAtRules();

self::assertSame('', $result);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this line ever be reached?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replied at length but it's been lost because GitHub's UI/UX is rubbish and I accidentally clicked a link.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@JakeQZ JakeQZ Dec 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the above-linked change it was possible to split test data up on logical grounds - one set of data syntactically invalid, the other merely with property names/values that don't conform to the spec.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case there doesn't appear to be any logical split between which test data causes an exception and for which the invalid rules are simply discarded.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually in PHPUnit we can use logicalOr to test for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if one of the conditions is an exception being thrown, AFAIAA, this is not possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hence this construct.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We (as the Emogrifier maintainers) have the secret knowledge of what actually happens ;-) - and I think it's fair that we decide on one possible behavior to be the desired one and test for that. Otherwise, this would make the tests really confusing to read (because the desired behavior then is not clear to the reader).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What actually happens here is down to the behaviour of sabberworm/php-css-parser. We only know what happens because we have looked into it. We can influence it and I note you have had a number of PRs accepted. But at the end of the day it is a 3rd party library.

Ultimately we may be able to dispense with this intermediary class and use the Sabberworm classes directly, but we are a little way off that yet.

Some invalid CSS throws an excpetion (in debug mode). Some doesn't. If it throws an excpetion in debug mode, that's a bonus. If it doesn't, it's no big deal. But we don't control that. This relates back to #1135 - to what extent are we testing 3rd party libraries duplicitously?

Copy link
Contributor Author

@JakeQZ JakeQZ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have made some changes as suggested, and otherwise made the case for things being as originally proposed.

src/Css/CssDocument.php Outdated Show resolved Hide resolved
tests/Unit/Css/CssDocumentTest.php Outdated Show resolved Hide resolved

$result = $subject->renderNonConditionalAtRules();

self::assertSame('', $result);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replied at length but it's been lost because GitHub's UI/UX is rubbish and I accidentally clicked a link.


$result = $subject->renderNonConditionalAtRules();

self::assertSame('', $result);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


$result = $subject->renderNonConditionalAtRules();

self::assertSame('', $result);
Copy link
Contributor Author

@JakeQZ JakeQZ Dec 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the above-linked change it was possible to split test data up on logical grounds - one set of data syntactically invalid, the other merely with property names/values that don't conform to the spec.


$result = $subject->renderNonConditionalAtRules();

self::assertSame('', $result);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if one of the conditions is an exception being thrown, AFAIAA, this is not possible.


$result = $subject->renderNonConditionalAtRules();

self::assertSame('', $result);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hence this construct.

$result = $subject->renderNonConditionalAtRules();

self::assertSame('', $result);
} catch (UnexpectedEOFException $exception) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

tests/Unit/Css/CssDocumentTest.php Outdated Show resolved Hide resolved
src/Css/CssDocument.php Outdated Show resolved Hide resolved
Copy link
Contributor

@oliverklee oliverklee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay - I think reviewing this got lost for me over the holidays.

src/Css/CssDocument.php Outdated Show resolved Hide resolved
$result = $subject->renderNonConditionalAtRules();

self::assertSame('', $result);
} catch (UnexpectedEOFException $exception) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then please let's test for one of the two. (We can also add the other case as a test and mark it as self::markAsSkipped() (if I remember the method name correctly).

tests/Unit/Css/CssDocumentTest.php Outdated Show resolved Hide resolved
tests/Unit/Css/CssDocumentTest.php Outdated Show resolved Hide resolved
tests/Unit/Css/CssDocumentTest.php Outdated Show resolved Hide resolved
src/Css/CssDocument.php Outdated Show resolved Hide resolved
This partially addresses both #1139 and #1140.

However, if the CSS contains nested at-rules, the debug setting won't currently
be passed on, due to MyIntervals/PHP-CSS-Parser#127.

Some tests have been adapted to cater for an exception in debug mode with only
some of the data.
Copy link
Contributor Author

@JakeQZ JakeQZ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've hopefully resolved all the comments from the latest review - see individual replies.

src/Css/CssDocument.php Outdated Show resolved Hide resolved
src/Css/CssDocument.php Outdated Show resolved Hide resolved
tests/Unit/Css/CssDocumentTest.php Outdated Show resolved Hide resolved
tests/Unit/Css/CssDocumentTest.php Outdated Show resolved Hide resolved
$result = $subject->renderNonConditionalAtRules();

self::assertSame('', $result);
} catch (UnexpectedEOFException $exception) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I've change this (and the other similar test) to expect an exception, but if one isn't thrown mark the test as skipped - since in strict (debug) mode the CSS parser only throws an exception with some of the datasets, and we are already testing that the invalid rules from these datasets are dropped in lenient (non-debug) mode.

tests/Unit/Css/CssDocumentTest.php Outdated Show resolved Hide resolved
src/Css/CssDocument.php Show resolved Hide resolved

$this->createDebugSubject($cssBefore . $atRuleCss);

self::markTestSkipped(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let's have the markTestSkipped calls at the start of the test so no other code gets run. (This also helps the reader.)

Copy link
Contributor Author

@JakeQZ JakeQZ Jan 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would skip the test for all datasets, including the ones that currently pass.

The test data consists of CSS that contains some invalid declarations. We have a separate test to confirm in non-debug mode that the invalid CSS is dropped. In debug ("strict") mode, the CSS parser throws exceptions for some but not all invalid CSS constructs. So we cannot assert that an exception is thrown for all the CSS strings in the data.

But if we don't assert it for any, we may as well not bother with the test at all, or have a dummy test that does not use the data provider but calls markTestSkipped() so we just see one skipped test in the results (not one for each dataset).

An alternative would be to split the datasets (data providers) up so that one provides datasets for which the CSS parser currently throws an exception, and one provides those for which it doesn't. Though there doesn't appear to be any particular distinctive feature of those for which an exception is thrown - it's just cases where the strictness isn't quite as strict as the spec dictates.

WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative would be to split the datasets (data providers) up so that one provides datasets for which the CSS parser currently throws an exception, and one provides those for which it doesn't.

That's the approach I'd prefer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I've split up the data accordingly, only passing the CSS for which the CSS parser throws an exception to the tests for that, with a comment in the DocBlock indicating that the other CSS is not currently tested.

…ption in

the CSS parser in strict parsing mode and that which does not.
Only use the datasets which trigger an exception in the test for that.
Copy link
Contributor

@oliverklee oliverklee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat!

@oliverklee oliverklee merged commit 1290db3 into main Jan 12, 2022
@oliverklee oliverklee deleted the task/parser-debug-setting branch January 12, 2022 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants