|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * WordPress Coding Standard. |
| 4 | + * |
| 5 | + * @package WPCS\WordPressCodingStandards |
| 6 | + * @link https://github.com/WordPress/WordPress-Coding-Standards |
| 7 | + * @license https://opensource.org/licenses/MIT MIT |
| 8 | + */ |
| 9 | + |
| 10 | +namespace WordPressCS\WordPress\Util\Tests\Helpers\ContextHelper; |
| 11 | + |
| 12 | +use PHPCSUtils\Tokens\Collections; |
| 13 | +use WordPressCS\WordPress\Helpers\ContextHelper; |
| 14 | +use PHPCSUtils\TestUtils\UtilityMethodTestCase; |
| 15 | + |
| 16 | +/** |
| 17 | + * Tests for the `ContextHelper::is_token_namespaced()` utility method. |
| 18 | + * |
| 19 | + * @since 3.3.0 |
| 20 | + * |
| 21 | + * @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_token_namespaced() |
| 22 | + */ |
| 23 | +final class IsTokenNamespacedUnitTest extends UtilityMethodTestCase { |
| 24 | + |
| 25 | + /** |
| 26 | + * Test is_token_namespaced() returns false if the token does not exist. |
| 27 | + * |
| 28 | + * @return void |
| 29 | + */ |
| 30 | + public function testIsTokenNamespacedReturnFalseIfTokenDoesntExist() { |
| 31 | + $this->assertFalse( ContextHelper::is_token_namespaced( self::$phpcsFile, -1 ) ); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Test is_token_namespaced() returns false for non-namespaced contexts. |
| 36 | + * |
| 37 | + * @dataProvider dataIsTokenNamespacedShouldReturnFalse |
| 38 | + * |
| 39 | + * @param string $commentString The comment which prefaces the target token in the test file. |
| 40 | + * @param string $tokenContent The token content to use for the target token. |
| 41 | + * |
| 42 | + * @return void |
| 43 | + */ |
| 44 | + public function testIsTokenNamespacedShouldReturnFalse( $commentString, $tokenContent ) { |
| 45 | + $stackPtr = $this->getTargetToken( $commentString, Collections::nameTokens(), $tokenContent ); |
| 46 | + $this->assertFalse( ContextHelper::is_token_namespaced( self::$phpcsFile, $stackPtr ) ); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Data provider. |
| 51 | + * |
| 52 | + * @return array |
| 53 | + * @see testIsTokenNamespacedShouldReturnFalse() |
| 54 | + */ |
| 55 | + public static function dataIsTokenNamespacedShouldReturnFalse() { |
| 56 | + return array( |
| 57 | + array( '/* test return false 1 */', 'my_function' ), |
| 58 | + array( '/* test return false 2 */', 'my_function' ), |
| 59 | + array( '/* test return false 3 */', 'MyClass' ), |
| 60 | + array( '/* test return false 4 */', 'MyClass' ), |
| 61 | + array( '/* test return false 5 */', 'MY_CONSTANT' ), |
| 62 | + array( '/* test return false 6 */', 'MY_CONSTANT' ), |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Test is_token_namespaced() returns true for namespaced contexts. |
| 68 | + * |
| 69 | + * @dataProvider dataIsTokenNamespacedShouldReturnTrue |
| 70 | + * |
| 71 | + * @param string $commentString The comment which prefaces the target token in the test file. |
| 72 | + * @param string $tokenContent The token content to use for the target token. |
| 73 | + * |
| 74 | + * @return void |
| 75 | + */ |
| 76 | + public function testIsTokenNamespacedShouldReturnTrue( $commentString, $tokenContent ) { |
| 77 | + $stackPtr = $this->getTargetToken( $commentString, Collections::nameTokens(), $tokenContent ); |
| 78 | + $this->assertTrue( ContextHelper::is_token_namespaced( self::$phpcsFile, $stackPtr ) ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Data provider. |
| 83 | + * |
| 84 | + * @return array |
| 85 | + * @see testIsTokenNamespacedShouldReturnTrue() |
| 86 | + */ |
| 87 | + public static function dataIsTokenNamespacedShouldReturnTrue() { |
| 88 | + return array( |
| 89 | + array( '/* test return true 1 */', 'my_function' ), |
| 90 | + array( '/* test return true 2 */', 'my_function' ), |
| 91 | + array( '/* test return true 3 */', 'my_function' ), |
| 92 | + array( '/* test return true 4 */', 'MyClass' ), |
| 93 | + array( '/* test return true 5 */', 'MY_CONSTANT' ), |
| 94 | + ); |
| 95 | + } |
| 96 | +} |
0 commit comments