Skip to content

Commit 1c8d4f4

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 2ea974b commit 1c8d4f4

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
@@ -959,37 +959,37 @@ private function backfillHelper($token, $skipScopeCloserCheck=false)
959959
{
960960
$tokens = $this->phpcsFile->getTokens();
961961

962-
$this->assertTrue(array_key_exists('scope_condition', $tokens[$token]), 'Scope condition is not set');
963-
$this->assertTrue(array_key_exists('scope_opener', $tokens[$token]), 'Scope opener is not set');
964-
$this->assertTrue(array_key_exists('scope_closer', $tokens[$token]), 'Scope closer is not set');
962+
$this->assertArrayHasKey('scope_condition', $tokens[$token], 'Scope condition is not set');
963+
$this->assertArrayHasKey('scope_opener', $tokens[$token], 'Scope opener is not set');
964+
$this->assertArrayHasKey('scope_closer', $tokens[$token], 'Scope closer is not set');
965965
$this->assertSame($tokens[$token]['scope_condition'], $token, 'Scope condition is not the T_FN token');
966-
$this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$token]), 'Parenthesis owner is not set');
967-
$this->assertTrue(array_key_exists('parenthesis_opener', $tokens[$token]), 'Parenthesis opener is not set');
968-
$this->assertTrue(array_key_exists('parenthesis_closer', $tokens[$token]), 'Parenthesis closer is not set');
966+
$this->assertArrayHasKey('parenthesis_owner', $tokens[$token], 'Parenthesis owner is not set');
967+
$this->assertArrayHasKey('parenthesis_opener', $tokens[$token], 'Parenthesis opener is not set');
968+
$this->assertArrayHasKey('parenthesis_closer', $tokens[$token], 'Parenthesis closer is not set');
969969
$this->assertSame($tokens[$token]['parenthesis_owner'], $token, 'Parenthesis owner is not the T_FN token');
970970

971971
$opener = $tokens[$token]['scope_opener'];
972-
$this->assertTrue(array_key_exists('scope_condition', $tokens[$opener]), 'Opener scope condition is not set');
973-
$this->assertTrue(array_key_exists('scope_opener', $tokens[$opener]), 'Opener scope opener is not set');
974-
$this->assertTrue(array_key_exists('scope_closer', $tokens[$opener]), 'Opener scope closer is not set');
972+
$this->assertArrayHasKey('scope_condition', $tokens[$opener], 'Opener scope condition is not set');
973+
$this->assertArrayHasKey('scope_opener', $tokens[$opener], 'Opener scope opener is not set');
974+
$this->assertArrayHasKey('scope_closer', $tokens[$opener], 'Opener scope closer is not set');
975975
$this->assertSame($tokens[$opener]['scope_condition'], $token, 'Opener scope condition is not the T_FN token');
976976
$this->assertSame(T_FN_ARROW, $tokens[$opener]['code'], 'Arrow scope opener not tokenized as T_FN_ARROW (code)');
977977
$this->assertSame('T_FN_ARROW', $tokens[$opener]['type'], 'Arrow scope opener not tokenized as T_FN_ARROW (type)');
978978

979979
$closer = $tokens[$token]['scope_closer'];
980-
$this->assertTrue(array_key_exists('scope_condition', $tokens[$closer]), 'Closer scope condition is not set');
981-
$this->assertTrue(array_key_exists('scope_opener', $tokens[$closer]), 'Closer scope opener is not set');
982-
$this->assertTrue(array_key_exists('scope_closer', $tokens[$closer]), 'Closer scope closer is not set');
980+
$this->assertArrayHasKey('scope_condition', $tokens[$closer], 'Closer scope condition is not set');
981+
$this->assertArrayHasKey('scope_opener', $tokens[$closer], 'Closer scope opener is not set');
982+
$this->assertArrayHasKey('scope_closer', $tokens[$closer], 'Closer scope closer is not set');
983983
if ($skipScopeCloserCheck === false) {
984984
$this->assertSame($tokens[$closer]['scope_condition'], $token, 'Closer scope condition is not the T_FN token');
985985
}
986986

987987
$opener = $tokens[$token]['parenthesis_opener'];
988-
$this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$opener]), 'Opening parenthesis owner is not set');
988+
$this->assertArrayHasKey('parenthesis_owner', $tokens[$opener], 'Opening parenthesis owner is not set');
989989
$this->assertSame($tokens[$opener]['parenthesis_owner'], $token, 'Opening parenthesis owner is not the T_FN token');
990990

991991
$closer = $tokens[$token]['parenthesis_closer'];
992-
$this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$closer]), 'Closing parenthesis owner is not set');
992+
$this->assertArrayHasKey('parenthesis_owner', $tokens[$closer], 'Closing parenthesis owner is not set');
993993
$this->assertSame($tokens[$closer]['parenthesis_owner'], $token, 'Closing parenthesis owner is not the T_FN token');
994994

995995
}//end backfillHelper()

0 commit comments

Comments
 (0)