Skip to content
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

UtilityMethodTestCase::getTargetToken(): throw exception when method called without tokenized test case file #385

Merged
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
47 changes: 47 additions & 0 deletions PHPCSUtils/Exceptions/TestFileNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
*
* @package PHPCSUtils
* @copyright 2019-2020 PHPCSUtils Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSUtils
*/

namespace PHPCSUtils\Exceptions;

use BadMethodCallException;

/**
* Exception thrown when the UtilityMethodTestCase::getTargetToken() method is run without a
* tokenized test case file being available.
*
* @since 1.0.0-alpha4
*/
final class TestFileNotFound extends BadMethodCallException
{

/**
* Create a new "test file not found" exception with a standardized text.
*
* @since 1.0.0-alpha4
*
* @param string $message The Exception message to throw.
* @param int $code The Exception code.
* @param \Throwable|null $previous The previous exception used for the exception chaining.
*
* @return void
*/
public function __construct($message = '', $code = 0, $previous = null)
{
if ($message === '') {
$message = \sprintf(
'Failed to find a tokenized test case file.%sMake sure the UtilityMethodTestCase::setUpTestFile()'
. ' method has run before calling UtilityMethodTestCase::getTargetToken()',
\PHP_EOL
);
}

parent::__construct($message, $code, $previous);
}
}
6 changes: 6 additions & 0 deletions PHPCSUtils/TestUtils/UtilityMethodTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
namespace PHPCSUtils\TestUtils;

use PHP_CodeSniffer\Exceptions\TokenizerException;
use PHP_CodeSniffer\Files\File;
use PHPCSUtils\BackCompat\Helper;
use PHPCSUtils\Exceptions\TestFileNotFound;
use PHPCSUtils\Exceptions\TestMarkerNotFound;
use PHPCSUtils\Exceptions\TestTargetNotFound;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -343,6 +345,10 @@ public static function usesPhp8NameTokens()
*/
public static function getTargetToken($commentString, $tokenType, $tokenContent = null)
{
if ((self::$phpcsFile instanceof File) === false) {
throw new TestFileNotFound();
}

$start = (self::$phpcsFile->numTokens - 1);
$comment = self::$phpcsFile->findPrevious(
\T_COMMENT,
Expand Down
54 changes: 54 additions & 0 deletions Tests/Exceptions/TestFileNotFound/TestFileNotFoundTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
*
* @package PHPCSUtils
* @copyright 2019-2020 PHPCSUtils Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSUtils
*/

namespace PHPCSUtils\Tests\Exceptions\TestFileNotFound;

use PHPCSUtils\Exceptions\TestFileNotFound;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;

/**
* Test class.
*
* @covers \PHPCSUtils\Exceptions\TestFileNotFound
*
* @since 1.0.0
*/
final class TestFileNotFoundTest extends TestCase
{

/**
* Test that the text of the exception is as expected.
*
* @return void
*/
public function testNoCustomMessage()
{
$this->expectException('PHPCSUtils\Exceptions\TestFileNotFound');
$this->expectExceptionMessage(
'Failed to find a tokenized test case file.' . \PHP_EOL
. 'Make sure the UtilityMethodTestCase::setUpTestFile() method has run'
);

throw new TestFileNotFound();
}

/**
* Test that a passed message overruled the default message.
*
* @return void
*/
public function testWithCustomMessage()
{
$this->expectException('PHPCSUtils\Exceptions\TestFileNotFound');
$this->expectExceptionMessage('foobar');

throw new TestFileNotFound('foobar');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
*
* @package PHPCSUtils
* @copyright 2019-2020 PHPCSUtils Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCSStandards/PHPCSUtils
*/

namespace PHPCSUtils\Tests\TestUtils\UtilityMethodTestCase;

use PHPCSUtils\Tests\PolyfilledTestCase;

/**
* Tests for the \PHPCSUtils\TestUtils\UtilityMethodTestCase::getTargetToken() method.
*
* @covers \PHPCSUtils\TestUtils\UtilityMethodTestCase::getTargetToken
*
* @group testutils
*
* @since 1.0.0
*/
final class GetTargetTokenFileNotFoundTest extends PolyfilledTestCase
{

/**
* Overload the "normal" set up to prevent a test case file from being tokenized.
*
* @beforeClass
*
* @return void
*/
public static function setUpTestFile()
{
// Deliberately left empty.
}

/**
* Test the behaviour of the getTargetToken() method when the test case file has not been tokenized.
*
* @return void
*/
public function testGetTargetTokenFileNotFound()
{
$this->expectException('PHPCSUtils\Exceptions\TestFileNotFound');
$this->expectExceptionMessage(
'Failed to find a tokenized test case file.' . \PHP_EOL
. 'Make sure the UtilityMethodTestCase::setUpTestFile() method has run'
);

$this->getTargetToken('/* testSomething */', [\T_VARIABLE], '$a');
}
}