Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Previously \Drupal\simpletest\AssertContentTrait::assertNoPattern() is used by many tests. It needs to be converted to BTB.
1/ Add it to AssertLegacyTrait
2/ Create change record
Comment | File | Size | Author |
---|---|---|---|
#15 | interdiff-2864257-8-15.txt | 1.18 KB | GoZ |
#15 | convert_web_tests-2864257-15.patch | 2.31 KB | GoZ |
#8 | 2864257-8.patch | 2.34 KB | jofitz |
#8 | interdiff-6-8.txt | 1.97 KB | jofitz |
#6 | convert_web_tests-2864257-6.patch | 2.73 KB | GoZ |
Comments
Comment #2
GoZ CreditAttribution: GoZ at Barbe-Rousse, Centarro commentedAdd change record http://www.drupal.org/node/2864262
Comment #3
GoZ CreditAttribution: GoZ at Barbe-Rousse, Centarro commentedComment #4
GoZ CreditAttribution: GoZ at Barbe-Rousse, Centarro commentedComment #5
dawehner+1 for sure!
Do you mind providing some test coverage for that in
\Drupal\Tests\Core\Assert\AssertLegacyTraitTest
?Comment #6
GoZ CreditAttribution: GoZ at Barbe-Rousse, Centarro commentedhere is test coverage.
i'm forced to replace
$this->assertSession()->responseNotMatches()
inassertNoPattern()
by duplicating code fromresponseNotMatches()
so AssertLegacyTraitTest find tests to pass.Comment #7
klausiThanks! I don't understand why we can't use $this->assertSession()->responseNotMatches(), can you explain that again? You just need to mock stuff on $this->webassert. See testAssertOptionSelected() for example.
That test would then just confirm that the call to assertNoPattern() is passed through.
Comment #8
jofitz CreditAttribution: jofitz at ComputerMinds commentedI have followed @klausi's suggestion and it works well for testAssertNoPatternFail(), but not testAssertNoPattern(). Any ideas?
Comment #9
GoZ CreditAttribution: GoZ at Barbe-Rousse, Centarro commented@klausi i don't see how to mock this.
responseNotMatches()
return nothing, andassertNoPattern()
only call toresponseNotMatches()
.responseNotMatches()
only usepage->getContent()
to make a regex search and assert result.If i mock responseNotMatches(), i define which will be the result, so i don't really test assertNoPattern(). I make some obvious tests in this case.
May be i'm wrong on how mock works and how to deal with it. Feel free to show me or explain where i am wrong if it's the case.
Comment #10
GoZ CreditAttribution: GoZ at Barbe-Rousse, Centarro commented@Jo Fitzgerald testAssertNoPatternFail() succeed, since you explicitly ask to
responseNotMatches()
to throw exception.We expect a PHPUnit_Framework_ExpectationFailedException::class.
assertNoPattern()
only call toresponseNotMatches()
We define
responseNotMatches()
will throw exception PHPUnit_Framework_ExpectationFailedExceptionWe test assertNoPattern(), so it throw PHPUnit_Framework_ExpectationFailedException as we ask it to do.
For me, this test is a nonsense (as i also expain in #9)
Analogy:
Ask to a guy to say "Hey"
Ask to this guy to answer "Yes" when you will ask a question to him.
Ask him if he said "Hey".
He responds "Yes"
Test succeed.
Repeat asking him if he said "Tô" and he will answer "Yes".
Test fail
Comment #11
jofitz CreditAttribution: jofitz at ComputerMinds commented@GoZ I couldn't agree more, but that is often the way with tests and mocks. The example @klausi recommended, testAssertOptionSelected(), is only one small step away from the same thing. I would be interested to hear @klausi's opinion...
Comment #12
klausi@GoZ: you would be testing that assertNoPattern() works as you intend it to work: just passing through stuff to responseNotMatches(). So the unit test confirms that
1) the method assertNoPattern() exists and is callable
2) The calls are forwarded to responseNotMatches()
That is the nature of a unit test: it only tests that little piece that you just wrote.
However, we can also add a test to BrowserTestBaseTest instead if you prefer that?
Comment #13
jofitz CreditAttribution: jofitz at ComputerMinds commented@klausi if the testbot replicates my local installation then testAssertNoPattern() will fail due to not containing an assert - are you aware how to avoid this?
Comment #14
klausi@Jo: you can do ->shouldBeCalled() or shouldNotBeCalled() on the mock. That will produce an assertion in PHPUnit.
You should add a ->shouldBeCalled() here.
I think we can remove this test. We only want to test that assertNoPattern() can be called and that it forwards calls to responseNotMatches(). That is covered by the first test already.
This line can be removed.
Comment #15
GoZ CreditAttribution: GoZ at Barbe-Rousse, Centarro commentedHere we go
Thanks for the detailed solution @klausi
Comment #16
dawehnerWe have a test, we have the required documentation ...
Comment #17
alexpottCommitted and pushed 47d0521 to 8.4.x and fd92fc7 to 8.3.x. Thanks!
Committed to 8.3.x because keeping the test API the same is important.
Comment #21
Mile23Updated and published CR.