-
Notifications
You must be signed in to change notification settings - Fork 154
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
Conversation
tests/Unit/Css/CssDocumentTest.php
Outdated
$result = $subject->renderNonConditionalAtRules(); | ||
|
||
self::assertSame('', $result); | ||
} catch (UnexpectedEOFException $exception) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above.
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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
andexpectExceptionMessage
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?
There was a problem hiding this comment.
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 thetry
/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
.)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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
|
||
$result = $subject->renderNonConditionalAtRules(); | ||
|
||
self::assertSame('', $result); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hence this construct.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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?
There was a problem hiding this 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.
tests/Unit/Css/CssDocumentTest.php
Outdated
|
||
$result = $subject->renderNonConditionalAtRules(); | ||
|
||
self::assertSame('', $result); |
There was a problem hiding this comment.
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.
tests/Unit/Css/CssDocumentTest.php
Outdated
|
||
$result = $subject->renderNonConditionalAtRules(); | ||
|
||
self::assertSame('', $result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests/Unit/Css/CssDocumentTest.php
Outdated
|
||
$result = $subject->renderNonConditionalAtRules(); | ||
|
||
self::assertSame('', $result); |
There was a problem hiding this comment.
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.
tests/Unit/Css/CssDocumentTest.php
Outdated
|
||
$result = $subject->renderNonConditionalAtRules(); | ||
|
||
self::assertSame('', $result); |
There was a problem hiding this comment.
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.
tests/Unit/Css/CssDocumentTest.php
Outdated
|
||
$result = $subject->renderNonConditionalAtRules(); | ||
|
||
self::assertSame('', $result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hence this construct.
tests/Unit/Css/CssDocumentTest.php
Outdated
$result = $subject->renderNonConditionalAtRules(); | ||
|
||
self::assertSame('', $result); | ||
} catch (UnexpectedEOFException $exception) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above.
There was a problem hiding this 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.
tests/Unit/Css/CssDocumentTest.php
Outdated
$result = $subject->renderNonConditionalAtRules(); | ||
|
||
self::assertSame('', $result); | ||
} catch (UnexpectedEOFException $exception) { |
There was a problem hiding this comment.
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).
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.
1819d40
to
4e389d5
Compare
There was a problem hiding this 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.
tests/Unit/Css/CssDocumentTest.php
Outdated
$result = $subject->renderNonConditionalAtRules(); | ||
|
||
self::assertSame('', $result); | ||
} catch (UnexpectedEOFException $exception) { |
There was a problem hiding this comment.
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
|
||
$this->createDebugSubject($cssBefore . $atRuleCss); | ||
|
||
self::markTestSkipped( |
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat!
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.