As discovered in #3001398-18: format_size() fixes, SizeTest needs
- some love, rejuvenating it to proper PHPUnit concepts - first and foremost introducing @dataproviders for making tests more atomic.
-
+++ b/core/tests/Drupal/KernelTests/Core/Common/SizeTest.php @@ -48,11 +53,8 @@ protected function setUp() { + $this->assertSame($expected, $result, $expected . ' == ' . $result . ' (' . $input . ' bytes)');What is the reason for putting $expected == $result in here? PHPUnit provides this information out of the box.
-
+++ b/core/tests/Drupal/KernelTests/Core/Common/SizeTest.php @@ -62,11 +64,8 @@ public function testCommonFormatSize() { - $this->assertEqual( - $size, - ($parsed_size = Bytes::toInt($string = format_size($size, NULL))), - $size . ' == ' . $parsed_size . ' (' . $string . ')' - ); + $parsed_size = Bytes::toInt($string = format_size($size, NULL)); + $this->assertEquals($size, $parsed_size, $size . ' == ' . $parsed_size . ' (' . $string . ')');To be honest I think we should improve the readability of this test and assign $string outside.
| Comment | File | Size | Author |
|---|---|---|---|
| #19 | interdiff-3001846-16-19.txt | 1.7 KB | krzysztof domański |
| #19 | interdiff-3001846-13-19.txt | 1.66 KB | krzysztof domański |
| #19 | 3001846-19.patch | 3.13 KB | krzysztof domański |
| #16 | 3001846-16.patch | 3.28 KB | krzysztof domański |
| #13 | interdiff-3001846-10-13.txt | 852 bytes | wengerk |
Comments
Comment #2
longwaveUnified the two tests into a single test with a data provider and (hopefully) modernised it to current standards.
Comment #3
mondrakeLooks good. Either this or #3001398: format_size() fixes will require a reroll, but let's see what lands first.
Comment #4
mondrakeooops no, this is in the rounded part, not the exact
EDIT: or not? it's in the rounded part in the current test, but since it's below 1KB in fact it shouldn't?
Comment #5
mondrakeComment #6
wengerkThanks for your tests ! I would go even beyond on modernisation & best practices to avoid any business logic or any
ifconditions.I would probably end up writing separate tests for the following if case
It would be by fare more elegant & easier to read if you provide 2 dataProviders & split the tests on 2 separated logics.
Here is my suggestion
testCommonFormatSizewith a providerproviderTestCommonFormatSizetestCommonFormatSizeCrossTestToIntwith a providerproviderTestCommonFormatSizeToIntWhat do you think ?
Comment #7
wengerkComment #8
wengerkHere are my suggestions, what do you think about it ?
By decoupling, we can add a proper
@coversand we simplify the test readability.PS: We could maybe rename the
testCommonFormatSizeCrossTestToIntto something better.Comment #10
wengerkOppsy I made a typo on my copy/past provider of tests ... sorry
Comment #11
longwaveThis doesn't actually cross-test; it should call format_size($expected) and then check that Bytes::toInt() returns the original value.
Comment #12
wengerkThis is the idea @longwave to rename this. UnitTest should not do some cross-test it's not a best practice. Otherwise if the first format_size fails it will obviously fail the "cross-test" which is not very useful. We can assert it works by himself just by using the provider.
If we want to assert
Bytes::toIntwe can do it in this test but we have to rename it, indeed as I suggest it in #8.What do you think ? Let's discuss about it !
Comment #13
wengerkHere are the changes according #8, #11 & #12.
Comment #14
longwaveThere is already a standalone test for
Bytes::toInt()in \Drupal\Tests\Component\Utility\BytesTest, so I don't think we need another unit test of that method here?The point of the cross-test seems to be ensuring that
Bytes::toInt()can correctly reverse whateverformat_size()produces (for non-rounded values at least), not sure whether that is actually worthwhile though.Comment #15
wengerkYour are perfectly right, I didn't see the existing tests before !
Then we should remove the tests I added, my bad.
I don't think we have to cross-test this, I mean all the necessary assertions are already done by the existing
\Drupal\Tests\Component\Utility\BytesTest::testToInt.Let's remove my unnecessary
testBytesToInt&providerTestBytesToInt.Comment #16
krzysztof domańskiHere are the changes according #2 and #13.
I remove
testBytesToInt&providerTestBytesToInt. I added$exactas the default parameter.Now it looks like this:
What do you think?
Comment #17
krzysztof domańskiI think the following code is not readable.
Better earlier version?
Comment #18
wengerkThanks for reroll & work on it.
For #16 the idea was to not do cross-test. So only remove the testBytesToInt & providerTestBytesToInt of #13 we don't want to mix 2 kind of assertion on the same test (plus BytesToInt is already covered).
About #17 I agree let's change it (again).
Don't forget to upload an interdiff on changes, would be easier to review :)
Comment #19
krzysztof domański@Kevin Thanks for the previous tips!
I removed
testBytesToInt&providerTestBytesToInt#15.I corrected #17.
The cross-test (with
Bytes::toInt()) could look like following code, but it is not necessary. Let's not add this. Do you agree with me?Comment #20
wengerkI agree with you as that's exactly what I try to explain (maybe badly ^^).
We don't need the test
testCommonFormatSizeConversionTestfor the following reasons:The
Bytes::toIntis already covered by\Drupal\Tests\Component\Utility\BytesTest::testToInt. And the result offormat_sizeare covered by our new testtestCommonFormatSize.So both
Bytes::toInt&format_sizeoutput/input are covered. if our tests onformat_sizefail then the output will be different as expected (aka not compliant withBytes::toInt). We don't need to add a cross-test anywhere.I can no longer RTBC this. Anyone else want to review / test / RTBC?
Comment #21
longwave#19 looks great to me.
Comment #22
alexpottMuch more readable and explicit - nice work - and once format_size becomes a class this can be easily converted to a unit test.
Committed and pushed f321b4aa69 to 8.7.x and 228c146440 to 8.6.x. Thanks!