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

Util/Common: add tests for the suggestType() method #246

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
224 changes: 224 additions & 0 deletions tests/Core/Util/SuggestTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
<?php
/**
* Tests for the \PHP_CodeSniffer\Util\Sniffs\Comments::suggestType() method.
*
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
* @copyright 2019 Juliette Reinders Folmer. All rights reserved.
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\Core\Util;

use PHP_CodeSniffer\Util\Common;
use PHPUnit\Framework\TestCase;

/**
* Tests for the \PHP_CodeSniffer\Util\Sniffs\Comments::suggestType() method.
*
* @covers \PHP_CodeSniffer\Util\Common::suggestType
*/
class SuggestTypeTest extends TestCase
{


/**
* Test passing an empty type to the suggestType() method.
*
* @return void
*/
public function testSuggestTypeEmpty()
{
$this->assertSame('', Common::suggestType(''));

}//end testSuggestTypeEmpty()


/**
* Test passing one of the allowed types to the suggestType() method.
*
* @param string $varType The type.
*
* @dataProvider dataSuggestTypeAllowedType
*
* @return void
*/
public function testSuggestTypeAllowedType($varType)
{
$result = Common::suggestType($varType);
$this->assertSame($varType, $result);

}//end testSuggestTypeAllowedType()


/**
* Data provider.
*
* @see testSuggestTypeAllowedType()
*
* @return array<string, array<string>>
*/
public function dataSuggestTypeAllowedType()
{
$data = [];
foreach (Common::$allowedTypes as $type) {
$data['Type: '.$type] = [$type];
}

return $data;

}//end dataSuggestTypeAllowedType()


/**
* Test passing one of the allowed types in the wrong case to the suggestType() method.
*
* @param string $varType The type found.
* @param string $expected Expected suggested type.
*
* @dataProvider dataSuggestTypeAllowedTypeWrongCase
*
* @return void
*/
public function testSuggestTypeAllowedTypeWrongCase($varType, $expected)
{
$result = Common::suggestType($varType);
$this->assertSame($expected, $result);

}//end testSuggestTypeAllowedTypeWrongCase()


/**
* Data provider.
*
* @see testSuggestTypeAllowedTypeWrongCase()
*
* @return array<string, array<string, string>>
*/
public function dataSuggestTypeAllowedTypeWrongCase()
{
$data = [];
foreach (Common::$allowedTypes as $type) {
$data['Mixed case: '.$type] = [
'varType' => ucfirst($type),
'expected' => $type,
];
$data['Uppercase: '.$type] = [
'varType' => strtoupper($type),
'expected' => $type,
];
}

return $data;

}//end dataSuggestTypeAllowedTypeWrongCase()


/**
* Test the suggestType() method for all other cases.
*
* @param string $varType The type found.
* @param string $expected Expected suggested type.
*
* @dataProvider dataSuggestTypeOther
*
* @return void
*/
public function testSuggestTypeOther($varType, $expected)
{
$result = Common::suggestType($varType);
$this->assertSame($expected, $result);

}//end testSuggestTypeOther()


/**
* Data provider.
*
* @see testSuggestTypeOther()
*
* @return array<string, array<string, string>>
*/
public function dataSuggestTypeOther()
{
return [
// Short forms.
'Short form type: bool, lowercase' => [
'varType' => 'bool',
'expected' => 'boolean',
],
'Short form type: bool, uppercase' => [
'varType' => 'BOOL',
'expected' => 'boolean',
],
'Short form type: double, lowercase' => [
'varType' => 'double',
'expected' => 'float',
],
'Short form type: real, mixed case' => [
'varType' => 'Real',
'expected' => 'float',
],
'Short form type: double, mixed case' => [
'varType' => 'DoUbLe',
'expected' => 'float',
],
'Short form type: int, lowercase' => [
'varType' => 'int',
'expected' => 'integer',
],
'Short form type: int, uppercase' => [
'varType' => 'INT',
'expected' => 'integer',
],

// Array types.
'Array type: mixed case keyword, empty parentheses' => [
'varType' => 'Array()',
'expected' => 'array',
],
'Array type: short form type as value within the parentheses' => [
'varType' => 'array(real)',
'expected' => 'array(float)',
],
'Array type: short form type as key within the parentheses' => [
'varType' => 'array(int => object)',
'expected' => 'array(integer => object)',
],
'Array type: valid specification' => [
'varType' => 'array(integer => array(string => resource))',
'expected' => 'array(integer => array(string => resource))',
],
'Array type: short form + uppercase types within the parentheses' => [
'varType' => 'ARRAY(BOOL => DOUBLE)',
'expected' => 'array(boolean => float)',
],
'Array type: no space around the arrow within the parentheses' => [
'varType' => 'array(string=>resource)',
'expected' => 'array(string => resource)',
],

// Incomplete array type.
'Array type: incomplete specification' => [
'varType' => 'array(int =>',
'expected' => 'array',
],

// Custom types are returned unchanged.
'Unknown type: "<string> => <int>"' => [
'varType' => '<string> => <int>',
'expected' => '<string> => <int>',
],
'Unknown type: "string[]"' => [
'varType' => 'string[]',
'expected' => 'string[]',
],
'Unknown type: "\DateTime"' => [
'varType' => '\DateTime',
'expected' => '\DateTime',
],
];

}//end dataSuggestTypeOther()


}//end class