Skip to content

Streamline the tests code style #296

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

Merged
merged 1 commit into from
Jul 2, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions config/php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,17 @@
'@PSR12' => true,
// Disable constant visibility from the PSR12 rule set as this would break compatibility with PHP < 7.1.
'visibility_required' => ['elements' => ['property', 'method']],

'php_unit_construct' => true,
'php_unit_dedicate_assert' => ['target' => '5.0'],
'php_unit_expectation' => ['target' => '5.6'],
'php_unit_fqcn_annotation' => true,
'php_unit_method_casing' => true,
'php_unit_mock' => ['target' => '5.5'],
'php_unit_mock_short_will_return' => true,
'php_unit_namespaced' => ['target' => '5.7'],
'php_unit_set_up_tear_down_visibility' => true,
'php_unit_test_annotation' => ['style' => 'annotation'],
'php_unit_test_case_static_method_calls' => ['call_type' => 'self'],
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not use

Suggested change
'php_unit_test_case_static_method_calls' => ['call_type' => 'self'],
'php_unit_test_case_static_method_calls' => ['call_type' => 'this'],

and leave the assertions as-is?

I think most people are used to calling assertions using $this and it’s also what the phpunit documentation does.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I talked to Sebastian Bergmann about this. He says calling the assert methods statically and non-statically both is valid (as PHP allows calling static methods non-statically), and it comes down to a matter of preference. He himself prefers the non-static calls because he likes doing calls statically only when this is technically necessary. I, on the other hand, prefer to keep things consistent: Always call static methods statically, and call non-static methods in a non-static way.

]
);
13 changes: 8 additions & 5 deletions tests/CSSList/AtRuleBlockListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@

class AtRuleBlockListTest extends TestCase
{
public function testMediaQueries()
/**
* @test
*/
public function mediaQueries()
{
$sCss = '@media(min-width: 768px){.class{color:red}}';
$oParser = new Parser($sCss);
$oDoc = $oParser->parse();
$aContents = $oDoc->getContents();
$oMediaQuery = $aContents[0];
$this->assertSame('media', $oMediaQuery->atRuleName(), 'Does not interpret the type as a function');
$this->assertSame('(min-width: 768px)', $oMediaQuery->atRuleArgs(), 'The media query is the value');
self::assertSame('media', $oMediaQuery->atRuleName(), 'Does not interpret the type as a function');
self::assertSame('(min-width: 768px)', $oMediaQuery->atRuleArgs(), 'The media query is the value');

$sCss = '@media (min-width: 768px) {.class{color:red}}';
$oParser = new Parser($sCss);
$oDoc = $oParser->parse();
$aContents = $oDoc->getContents();
$oMediaQuery = $aContents[0];
$this->assertSame('media', $oMediaQuery->atRuleName(), 'Does not interpret the type as a function');
$this->assertSame('(min-width: 768px)', $oMediaQuery->atRuleArgs(), 'The media query is the value');
self::assertSame('media', $oMediaQuery->atRuleName(), 'Does not interpret the type as a function');
self::assertSame('(min-width: 768px)', $oMediaQuery->atRuleArgs(), 'The media query is the value');
}
}
9 changes: 6 additions & 3 deletions tests/CSSList/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@

class DocumentTest extends TestCase
{
public function testOverrideContents()
/**
* @test
*/
public function overrideContents()
{
$sCss = '.thing { left: 10px; }';
$oParser = new Parser($sCss);
$oDoc = $oParser->parse();
$aContents = $oDoc->getContents();
$this->assertCount(1, $aContents);
self::assertCount(1, $aContents);

$sCss2 = '.otherthing { right: 10px; }';
$oParser2 = new Parser($sCss);
Expand All @@ -22,6 +25,6 @@ public function testOverrideContents()

$oDoc->setContents([$aContents[0], $aContents2[0]]);
$aFinalContents = $oDoc->getContents();
$this->assertCount(2, $aFinalContents);
self::assertCount(2, $aFinalContents);
}
}
125 changes: 89 additions & 36 deletions tests/OutputFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,42 +41,57 @@ protected function setUp()
$this->oDocument = $this->oParser->parse();
}

public function testPlain()
/**
* @test
*/
public function plain()
{
$this->assertSame(
self::assertSame(
'.main, .test {font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background: white;}
@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}',
$this->oDocument->render()
);
}

public function testCompact()
/**
* @test
*/
public function compact()
{
$this->assertSame(
self::assertSame(
'.main,.test{font:italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background:white;}'
. '@media screen{.main{background-size:100% 100%;font-size:1.3em;background-color:#fff;}}',
$this->oDocument->render(OutputFormat::createCompact())
);
}

public function testPretty()
/**
* @test
*/
public function pretty()
{
$this->assertSame(self::TEST_CSS, $this->oDocument->render(OutputFormat::createPretty()));
self::assertSame(self::TEST_CSS, $this->oDocument->render(OutputFormat::createPretty()));
}

public function testSpaceAfterListArgumentSeparator()
/**
* @test
*/
public function spaceAfterListArgumentSeparator()
{
$this->assertSame(
self::assertSame(
'.main, .test {font: italic normal bold 16px/ 1.2 '
. '"Helvetica", Verdana, sans-serif;background: white;}'
. "\n@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}",
$this->oDocument->render(OutputFormat::create()->setSpaceAfterListArgumentSeparator(" "))
);
}

public function testSpaceAfterListArgumentSeparatorComplex()
/**
* @test
*/
public function spaceAfterListArgumentSeparatorComplex()
{
$this->assertSame(
self::assertSame(
'.main, .test {font: italic normal bold 16px/1.2 "Helvetica", Verdana, sans-serif;background: white;}'
. "\n@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}",
$this->oDocument->render(OutputFormat::create()->setSpaceAfterListArgumentSeparator([
Expand All @@ -88,55 +103,73 @@ public function testSpaceAfterListArgumentSeparatorComplex()
);
}

public function testSpaceAfterSelectorSeparator()
/**
* @test
*/
public function spaceAfterSelectorSeparator()
{
$this->assertSame(
self::assertSame(
'.main,
.test {font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background: white;}
@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}',
$this->oDocument->render(OutputFormat::create()->setSpaceAfterSelectorSeparator("\n"))
);
}

public function testStringQuotingType()
/**
* @test
*/
public function stringQuotingType()
{
$this->assertSame(
self::assertSame(
'.main, .test {font: italic normal bold 16px/1.2 \'Helvetica\',Verdana,sans-serif;background: white;}
@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}',
$this->oDocument->render(OutputFormat::create()->setStringQuotingType("'"))
);
}

public function testRGBHashNotation()
/**
* @test
*/
public function rGBHashNotation()
{
$this->assertSame(
self::assertSame(
'.main, .test {font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background: white;}
@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: rgb(255,255,255);}}',
$this->oDocument->render(OutputFormat::create()->setRGBHashNotation(false))
);
}

public function testSemicolonAfterLastRule()
/**
* @test
*/
public function semicolonAfterLastRule()
{
$this->assertSame(
self::assertSame(
'.main, .test {font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background: white}
@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff}}',
$this->oDocument->render(OutputFormat::create()->setSemicolonAfterLastRule(false))
);
}

public function testSpaceAfterRuleName()
/**
* @test
*/
public function spaceAfterRuleName()
{
$this->assertSame(
self::assertSame(
'.main, .test {font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background: white;}
@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}',
$this->oDocument->render(OutputFormat::create()->setSpaceAfterRuleName("\t"))
);
}

public function testSpaceRules()
/**
* @test
*/
public function spaceRules()
{
$this->assertSame('.main, .test {
self::assertSame('.main, .test {
font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;
background: white;
}
Expand All @@ -147,19 +180,25 @@ public function testSpaceRules()
}}', $this->oDocument->render(OutputFormat::create()->set('Space*Rules', "\n")));
}

public function testSpaceBlocks()
/**
* @test
*/
public function spaceBlocks()
{
$this->assertSame('
self::assertSame('
.main, .test {font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background: white;}
@media screen {
.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}
}
', $this->oDocument->render(OutputFormat::create()->set('Space*Blocks', "\n")));
}

public function testSpaceBoth()
/**
* @test
*/
public function spaceBoth()
{
$this->assertSame('
self::assertSame('
.main, .test {
font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;
background: white;
Expand All @@ -174,18 +213,24 @@ public function testSpaceBoth()
', $this->oDocument->render(OutputFormat::create()->set('Space*Rules', "\n")->set('Space*Blocks', "\n")));
}

public function testSpaceBetweenBlocks()
/**
* @test
*/
public function spaceBetweenBlocks()
{
$this->assertSame(
self::assertSame(
'.main, .test {font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background: white;}'
. '@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}',
$this->oDocument->render(OutputFormat::create()->setSpaceBetweenBlocks(''))
);
}

public function testIndentation()
/**
* @test
*/
public function indentation()
{
$this->assertSame('
self::assertSame('
.main, .test {
font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;
background: white;
Expand All @@ -203,9 +248,12 @@ public function testIndentation()
->setIndentation('')));
}

public function testSpaceBeforeBraces()
/**
* @test
*/
public function spaceBeforeBraces()
{
$this->assertSame(
self::assertSame(
'.main, .test{font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background: white;}
@media screen{.main{background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}',
$this->oDocument->render(OutputFormat::create()->setSpaceBeforeOpeningBrace(''))
Expand All @@ -214,13 +262,15 @@ public function testSpaceBeforeBraces()

/**
* @expectedException \Sabberworm\CSS\Parsing\OutputException
*
* @test
*/
public function testIgnoreExceptionsOff()
public function ignoreExceptionsOff()
{
$aBlocks = $this->oDocument->getAllDeclarationBlocks();
$oFirstBlock = $aBlocks[0];
$oFirstBlock->removeSelector('.main');
$this->assertSame(
self::assertSame(
'.test {font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background: white;}
@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}',
$this->oDocument->render(OutputFormat::create()->setIgnoreExceptions(false))
Expand All @@ -229,13 +279,16 @@ public function testIgnoreExceptionsOff()
$this->oDocument->render(OutputFormat::create()->setIgnoreExceptions(false));
}

public function testIgnoreExceptionsOn()
/**
* @test
*/
public function ignoreExceptionsOn()
{
$aBlocks = $this->oDocument->getAllDeclarationBlocks();
$oFirstBlock = $aBlocks[0];
$oFirstBlock->removeSelector('.main');
$oFirstBlock->removeSelector('.test');
$this->assertSame(
self::assertSame(
'@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}',
$this->oDocument->render(OutputFormat::create()->setIgnoreExceptions(true))
);
Expand Down
Loading