Skip to content

Commit 100771f

Browse files
committed
Tests: use more specific assertions
The `assertArrayHasKey()` and `assertArrayNotHasKey()` method can be used instead of "manually" doing an `array_key_exists()` with `assert[True|False]()`. These methods were already available in PHPUnit 4.x and are already in use in other places in the test suite, so there is no reason not to use them in these tests too.
1 parent 95e3a35 commit 100771f

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

tests/Core/Tokenizers/PHP/AnonClassParenthesisOwnerTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public function testAnonClassNoParentheses($testMarker)
3030
$tokens = $this->phpcsFile->getTokens();
3131

3232
$anonClass = $this->getTargetToken($testMarker, T_ANON_CLASS);
33-
$this->assertFalse(array_key_exists('parenthesis_owner', $tokens[$anonClass]));
34-
$this->assertFalse(array_key_exists('parenthesis_opener', $tokens[$anonClass]));
35-
$this->assertFalse(array_key_exists('parenthesis_closer', $tokens[$anonClass]));
33+
$this->assertArrayNotHasKey('parenthesis_owner', $tokens[$anonClass]);
34+
$this->assertArrayNotHasKey('parenthesis_opener', $tokens[$anonClass]);
35+
$this->assertArrayNotHasKey('parenthesis_closer', $tokens[$anonClass]);
3636

3737
}//end testAnonClassNoParentheses()
3838

@@ -54,11 +54,11 @@ public function testAnonClassNoParenthesesNextOpenClose($testMarker)
5454
$function = $this->getTargetToken($testMarker, T_FUNCTION);
5555

5656
$opener = $this->getTargetToken($testMarker, T_OPEN_PARENTHESIS);
57-
$this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$opener]));
57+
$this->assertArrayHasKey('parenthesis_owner', $tokens[$opener]);
5858
$this->assertSame($function, $tokens[$opener]['parenthesis_owner']);
5959

6060
$closer = $this->getTargetToken($testMarker, T_CLOSE_PARENTHESIS);
61-
$this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$closer]));
61+
$this->assertArrayHasKey('parenthesis_owner', $tokens[$closer]);
6262
$this->assertSame($function, $tokens[$closer]['parenthesis_owner']);
6363

6464
}//end testAnonClassNoParenthesesNextOpenClose()
@@ -107,23 +107,23 @@ public function testAnonClassWithParentheses($testMarker)
107107
$opener = $this->getTargetToken($testMarker, T_OPEN_PARENTHESIS);
108108
$closer = $this->getTargetToken($testMarker, T_CLOSE_PARENTHESIS);
109109

110-
$this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$anonClass]));
111-
$this->assertTrue(array_key_exists('parenthesis_opener', $tokens[$anonClass]));
112-
$this->assertTrue(array_key_exists('parenthesis_closer', $tokens[$anonClass]));
110+
$this->assertArrayHasKey('parenthesis_owner', $tokens[$anonClass]);
111+
$this->assertArrayHasKey('parenthesis_opener', $tokens[$anonClass]);
112+
$this->assertArrayHasKey('parenthesis_closer', $tokens[$anonClass]);
113113
$this->assertSame($anonClass, $tokens[$anonClass]['parenthesis_owner']);
114114
$this->assertSame($opener, $tokens[$anonClass]['parenthesis_opener']);
115115
$this->assertSame($closer, $tokens[$anonClass]['parenthesis_closer']);
116116

117-
$this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$opener]));
118-
$this->assertTrue(array_key_exists('parenthesis_opener', $tokens[$opener]));
119-
$this->assertTrue(array_key_exists('parenthesis_closer', $tokens[$opener]));
117+
$this->assertArrayHasKey('parenthesis_owner', $tokens[$opener]);
118+
$this->assertArrayHasKey('parenthesis_opener', $tokens[$opener]);
119+
$this->assertArrayHasKey('parenthesis_closer', $tokens[$opener]);
120120
$this->assertSame($anonClass, $tokens[$opener]['parenthesis_owner']);
121121
$this->assertSame($opener, $tokens[$opener]['parenthesis_opener']);
122122
$this->assertSame($closer, $tokens[$opener]['parenthesis_closer']);
123123

124-
$this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$closer]));
125-
$this->assertTrue(array_key_exists('parenthesis_opener', $tokens[$closer]));
126-
$this->assertTrue(array_key_exists('parenthesis_closer', $tokens[$closer]));
124+
$this->assertArrayHasKey('parenthesis_owner', $tokens[$closer]);
125+
$this->assertArrayHasKey('parenthesis_opener', $tokens[$closer]);
126+
$this->assertArrayHasKey('parenthesis_closer', $tokens[$closer]);
127127
$this->assertSame($anonClass, $tokens[$closer]['parenthesis_owner']);
128128
$this->assertSame($opener, $tokens[$closer]['parenthesis_opener']);
129129
$this->assertSame($closer, $tokens[$closer]['parenthesis_closer']);

tests/Core/Tokenizers/PHP/BackfillFnTokenTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -896,37 +896,37 @@ private function backfillHelper($token, $skipScopeCloserCheck=false)
896896
{
897897
$tokens = $this->phpcsFile->getTokens();
898898

899-
$this->assertTrue(array_key_exists('scope_condition', $tokens[$token]), 'Scope condition is not set');
900-
$this->assertTrue(array_key_exists('scope_opener', $tokens[$token]), 'Scope opener is not set');
901-
$this->assertTrue(array_key_exists('scope_closer', $tokens[$token]), 'Scope closer is not set');
899+
$this->assertArrayHasKey('scope_condition', $tokens[$token], 'Scope condition is not set');
900+
$this->assertArrayHasKey('scope_opener', $tokens[$token], 'Scope opener is not set');
901+
$this->assertArrayHasKey('scope_closer', $tokens[$token], 'Scope closer is not set');
902902
$this->assertSame($tokens[$token]['scope_condition'], $token, 'Scope condition is not the T_FN token');
903-
$this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$token]), 'Parenthesis owner is not set');
904-
$this->assertTrue(array_key_exists('parenthesis_opener', $tokens[$token]), 'Parenthesis opener is not set');
905-
$this->assertTrue(array_key_exists('parenthesis_closer', $tokens[$token]), 'Parenthesis closer is not set');
903+
$this->assertArrayHasKey('parenthesis_owner', $tokens[$token], 'Parenthesis owner is not set');
904+
$this->assertArrayHasKey('parenthesis_opener', $tokens[$token], 'Parenthesis opener is not set');
905+
$this->assertArrayHasKey('parenthesis_closer', $tokens[$token], 'Parenthesis closer is not set');
906906
$this->assertSame($tokens[$token]['parenthesis_owner'], $token, 'Parenthesis owner is not the T_FN token');
907907

908908
$opener = $tokens[$token]['scope_opener'];
909-
$this->assertTrue(array_key_exists('scope_condition', $tokens[$opener]), 'Opener scope condition is not set');
910-
$this->assertTrue(array_key_exists('scope_opener', $tokens[$opener]), 'Opener scope opener is not set');
911-
$this->assertTrue(array_key_exists('scope_closer', $tokens[$opener]), 'Opener scope closer is not set');
909+
$this->assertArrayHasKey('scope_condition', $tokens[$opener], 'Opener scope condition is not set');
910+
$this->assertArrayHasKey('scope_opener', $tokens[$opener], 'Opener scope opener is not set');
911+
$this->assertArrayHasKey('scope_closer', $tokens[$opener], 'Opener scope closer is not set');
912912
$this->assertSame($tokens[$opener]['scope_condition'], $token, 'Opener scope condition is not the T_FN token');
913913
$this->assertSame(T_FN_ARROW, $tokens[$opener]['code'], 'Arrow scope opener not tokenized as T_FN_ARROW (code)');
914914
$this->assertSame('T_FN_ARROW', $tokens[$opener]['type'], 'Arrow scope opener not tokenized as T_FN_ARROW (type)');
915915

916916
$closer = $tokens[$token]['scope_closer'];
917-
$this->assertTrue(array_key_exists('scope_condition', $tokens[$closer]), 'Closer scope condition is not set');
918-
$this->assertTrue(array_key_exists('scope_opener', $tokens[$closer]), 'Closer scope opener is not set');
919-
$this->assertTrue(array_key_exists('scope_closer', $tokens[$closer]), 'Closer scope closer is not set');
917+
$this->assertArrayHasKey('scope_condition', $tokens[$closer], 'Closer scope condition is not set');
918+
$this->assertArrayHasKey('scope_opener', $tokens[$closer], 'Closer scope opener is not set');
919+
$this->assertArrayHasKey('scope_closer', $tokens[$closer], 'Closer scope closer is not set');
920920
if ($skipScopeCloserCheck === false) {
921921
$this->assertSame($tokens[$closer]['scope_condition'], $token, 'Closer scope condition is not the T_FN token');
922922
}
923923

924924
$opener = $tokens[$token]['parenthesis_opener'];
925-
$this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$opener]), 'Opening parenthesis owner is not set');
925+
$this->assertArrayHasKey('parenthesis_owner', $tokens[$opener], 'Opening parenthesis owner is not set');
926926
$this->assertSame($tokens[$opener]['parenthesis_owner'], $token, 'Opening parenthesis owner is not the T_FN token');
927927

928928
$closer = $tokens[$token]['parenthesis_closer'];
929-
$this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$closer]), 'Closing parenthesis owner is not set');
929+
$this->assertArrayHasKey('parenthesis_owner', $tokens[$closer], 'Closing parenthesis owner is not set');
930930
$this->assertSame($tokens[$closer]['parenthesis_owner'], $token, 'Closing parenthesis owner is not the T_FN token');
931931

932932
}//end backfillHelper()

0 commit comments

Comments
 (0)