Skip to content

Commit 85c3301

Browse files
authored
Merge pull request #611 from PHPCSStandards/feature/utilitymethodtestcase-improve-tests
TestUtils/UtilityMethodTestCase: improve setup/teardown tests
2 parents 3dcf418 + fb8c3a0 commit 85c3301

File tree

4 files changed

+88
-22
lines changed

4 files changed

+88
-22
lines changed

Tests/TestUtils/UtilityMethodTestCase/GetTargetTokenTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final class GetTargetTokenTest extends PolyfilledTestCase
3131
*/
3232
public static function setUpTestFile()
3333
{
34-
self::$caseFile = __DIR__ . '/UtilityMethodTestCaseTest.inc';
34+
self::$caseFile = __DIR__ . '/SetUpTestFileTest.inc';
3535
parent::setUpTestFile();
3636
}
3737

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
4+
*
5+
* @package PHPCSUtils
6+
* @copyright 2019-2020 PHPCSUtils Contributors
7+
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
8+
* @link https://github.com/PHPCSStandards/PHPCSUtils
9+
*/
10+
11+
namespace PHPCSUtils\Tests\TestUtils\UtilityMethodTestCase;
12+
13+
use PHPCSUtils\Tests\PolyfilledTestCase;
14+
15+
/**
16+
* Tests for the \PHPCSUtils\TestUtils\UtilityMethodTestCase class.
17+
*
18+
* @covers \PHPCSUtils\TestUtils\UtilityMethodTestCase::resetTestFile
19+
*
20+
* @since 1.0.0
21+
*/
22+
final class ResetTestFileTest extends PolyfilledTestCase
23+
{
24+
25+
/**
26+
* Overload the "normal" set up as it needs to be run from within the actual test(s) to ensure we have a valid test.
27+
*
28+
* @beforeClass
29+
*
30+
* @return void
31+
*/
32+
public static function setUpTestFile()
33+
{
34+
self::$caseFile = __DIR__ . '/SetUpTestFileTest.inc';
35+
// Deliberately not running the actual setUpTestFile() method.
36+
}
37+
38+
/**
39+
* Test that the static class properties in the class are correctly reset.
40+
*
41+
* @return void
42+
*/
43+
public function testTearDown()
44+
{
45+
// Initialize a test, which should change the values of most static properties.
46+
self::$tabWidth = 2;
47+
self::$selectedSniff = ['Test.Test.Test'];
48+
parent::setUpTestFile();
49+
50+
// Verify that (most) properties no longer have their original value.
51+
$this->assertNotSame('0', self::$phpcsVersion, 'phpcsVersion was not updated');
52+
$this->assertSame('inc', self::$fileExtension, 'fileExtension was (not) updated');
53+
$this->assertNotSame('', self::$caseFile, 'caseFile was not updated');
54+
$this->assertNotSame(4, self::$tabWidth, 'tabWidth was not updated');
55+
$this->assertNotNull(self::$phpcsFile, 'phpcsFile was not updated');
56+
$this->assertNotSame(['Dummy.Dummy.Dummy'], self::$selectedSniff, 'selectedSniff was not updated');
57+
58+
// Reset the file as per the "afterClass"/tear down method.
59+
parent::resetTestFile();
60+
61+
// Verify the properties in the class have been cleaned up.
62+
$this->assertSame('0', self::$phpcsVersion, 'phpcsVersion was not reset');
63+
$this->assertSame('inc', self::$fileExtension, 'fileExtension was not reset');
64+
$this->assertSame('', self::$caseFile, 'caseFile was not reset');
65+
$this->assertSame(4, self::$tabWidth, 'tabWidth was not reset');
66+
$this->assertNull(self::$phpcsFile, 'phpcsFile was not reset');
67+
$this->assertSame(['Dummy.Dummy.Dummy'], self::$selectedSniff, 'selectedSniff was not reset');
68+
}
69+
}
File renamed without changes.

Tests/TestUtils/UtilityMethodTestCase/UtilityMethodTestCaseTest.php renamed to Tests/TestUtils/UtilityMethodTestCase/SetUpTestFileTest.php

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
/**
1616
* Tests for the \PHPCSUtils\TestUtils\UtilityMethodTestCase class.
1717
*
18-
* @covers \PHPCSUtils\TestUtils\UtilityMethodTestCase
18+
* @covers \PHPCSUtils\TestUtils\UtilityMethodTestCase::setUpTestFile
19+
* @covers \PHPCSUtils\TestUtils\UtilityMethodTestCase::parseFile
20+
* @covers \PHPCSUtils\TestUtils\UtilityMethodTestCase::skipJSCSSTestsOnPHPCS4
1921
*
2022
* @since 1.0.0
2123
*/
22-
final class UtilityMethodTestCaseTest extends PolyfilledTestCase
24+
final class SetUpTestFileTest extends PolyfilledTestCase
2325
{
2426

2527
/**
@@ -32,7 +34,7 @@ final class UtilityMethodTestCaseTest extends PolyfilledTestCase
3234
*/
3335
public static function setUpTestFile()
3436
{
35-
// Deliberately left empty.
37+
// Deliberately not running the actual setUpTestFile() method.
3638
}
3739

3840
/**
@@ -42,29 +44,24 @@ public static function setUpTestFile()
4244
*/
4345
public function testSetUp()
4446
{
47+
// Verify that the class was virgin to begin with.
48+
$this->assertSame('0', self::$phpcsVersion, 'phpcsVersion was not correct to begin with');
49+
$this->assertSame('inc', self::$fileExtension, 'fileExtension was not correct to begin with');
50+
$this->assertSame('', self::$caseFile, 'caseFile was not correct to begin with');
51+
$this->assertSame(4, self::$tabWidth, 'tabWidth was not correct to begin with');
52+
$this->assertNull(self::$phpcsFile, 'phpcsFile was not correct to begin with');
53+
$this->assertSame(['Dummy.Dummy.Dummy'], self::$selectedSniff, 'selectedSniff was not correct to begin with');
54+
55+
// Run the set up.
4556
parent::setUpTestFile();
57+
58+
// Verify select properties have been set correctly.
59+
$this->assertNotSame('0', self::$phpcsVersion, 'phpcsVersion was not set');
60+
4661
$this->assertInstanceOf('PHP_CodeSniffer\Files\File', self::$phpcsFile);
4762
$this->assertSame(57, self::$phpcsFile->numTokens);
4863

4964
$tokens = self::$phpcsFile->getTokens();
5065
$this->assertIsArray($tokens);
5166
}
52-
53-
/**
54-
* Test that the class is correct reset.
55-
*
56-
* @depends testSetUp
57-
*
58-
* @return void
59-
*/
60-
public function testTearDown()
61-
{
62-
parent::resetTestFile();
63-
$this->assertSame('0', self::$phpcsVersion, 'phpcsVersion was not reset');
64-
$this->assertSame('inc', self::$fileExtension, 'fileExtension was not reset');
65-
$this->assertSame('', self::$caseFile, 'caseFile was not reset');
66-
$this->assertSame(4, self::$tabWidth, 'tabWidth was not reset');
67-
$this->assertNull(self::$phpcsFile, 'phpcsFile was not reset');
68-
$this->assertSame(['Dummy.Dummy.Dummy'], self::$selectedSniff, 'selectedSniff was not reset');
69-
}
7067
}

0 commit comments

Comments
 (0)