|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the \PHP_CodeSniffer\Util\Sniffs\Comments::suggestType() method. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <[email protected]> |
| 6 | + * @copyright 2019 Juliette Reinders Folmer. All rights reserved. |
| 7 | + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHP_CodeSniffer\Tests\Core\Util; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Util\Common; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +/** |
| 16 | + * Tests for the \PHP_CodeSniffer\Util\Sniffs\Comments::suggestType() method. |
| 17 | + * |
| 18 | + * @covers \PHP_CodeSniffer\Util\Common::suggestType |
| 19 | + */ |
| 20 | +class SuggestTypeTest extends TestCase |
| 21 | +{ |
| 22 | + |
| 23 | + |
| 24 | + /** |
| 25 | + * Test passing an empty type to the suggestType() method. |
| 26 | + * |
| 27 | + * @return void |
| 28 | + */ |
| 29 | + public function testSuggestTypeEmpty() |
| 30 | + { |
| 31 | + $this->assertSame('', Common::suggestType('')); |
| 32 | + |
| 33 | + }//end testSuggestTypeEmpty() |
| 34 | + |
| 35 | + |
| 36 | + /** |
| 37 | + * Test passing one of the allowed types to the suggestType() method. |
| 38 | + * |
| 39 | + * @param string $varType The type. |
| 40 | + * |
| 41 | + * @dataProvider dataSuggestTypeAllowedType |
| 42 | + * |
| 43 | + * @return void |
| 44 | + */ |
| 45 | + public function testSuggestTypeAllowedType($varType) |
| 46 | + { |
| 47 | + $result = Common::suggestType($varType); |
| 48 | + $this->assertSame($varType, $result); |
| 49 | + |
| 50 | + }//end testSuggestTypeAllowedType() |
| 51 | + |
| 52 | + |
| 53 | + /** |
| 54 | + * Data provider. |
| 55 | + * |
| 56 | + * @see testSuggestTypeAllowedType() |
| 57 | + * |
| 58 | + * @return array<string, array<string>> |
| 59 | + */ |
| 60 | + public function dataSuggestTypeAllowedType() |
| 61 | + { |
| 62 | + $data = []; |
| 63 | + foreach (Common::$allowedTypes as $type) { |
| 64 | + $data['Type: '.$type] = [$type]; |
| 65 | + } |
| 66 | + |
| 67 | + return $data; |
| 68 | + |
| 69 | + }//end dataSuggestTypeAllowedType() |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * Test passing one of the allowed types in the wrong case to the suggestType() method. |
| 74 | + * |
| 75 | + * @param string $varType The type found. |
| 76 | + * @param string $expected Expected suggested type. |
| 77 | + * |
| 78 | + * @dataProvider dataSuggestTypeAllowedTypeWrongCase |
| 79 | + * |
| 80 | + * @return void |
| 81 | + */ |
| 82 | + public function testSuggestTypeAllowedTypeWrongCase($varType, $expected) |
| 83 | + { |
| 84 | + $result = Common::suggestType($varType); |
| 85 | + $this->assertSame($expected, $result); |
| 86 | + |
| 87 | + }//end testSuggestTypeAllowedTypeWrongCase() |
| 88 | + |
| 89 | + |
| 90 | + /** |
| 91 | + * Data provider. |
| 92 | + * |
| 93 | + * @see testSuggestTypeAllowedTypeWrongCase() |
| 94 | + * |
| 95 | + * @return array<string, array<string, string>> |
| 96 | + */ |
| 97 | + public function dataSuggestTypeAllowedTypeWrongCase() |
| 98 | + { |
| 99 | + $data = []; |
| 100 | + foreach (Common::$allowedTypes as $type) { |
| 101 | + $data['Mixed case: '.$type] = [ |
| 102 | + 'varType' => ucfirst($type), |
| 103 | + 'expected' => $type, |
| 104 | + ]; |
| 105 | + $data['Uppercase: '.$type] = [ |
| 106 | + 'varType' => strtoupper($type), |
| 107 | + 'expected' => $type, |
| 108 | + ]; |
| 109 | + } |
| 110 | + |
| 111 | + return $data; |
| 112 | + |
| 113 | + }//end dataSuggestTypeAllowedTypeWrongCase() |
| 114 | + |
| 115 | + |
| 116 | + /** |
| 117 | + * Test the suggestType() method for all other cases. |
| 118 | + * |
| 119 | + * @param string $varType The type found. |
| 120 | + * @param string $expected Expected suggested type. |
| 121 | + * |
| 122 | + * @dataProvider dataSuggestTypeOther |
| 123 | + * |
| 124 | + * @return void |
| 125 | + */ |
| 126 | + public function testSuggestTypeOther($varType, $expected) |
| 127 | + { |
| 128 | + $result = Common::suggestType($varType); |
| 129 | + $this->assertSame($expected, $result); |
| 130 | + |
| 131 | + }//end testSuggestTypeOther() |
| 132 | + |
| 133 | + |
| 134 | + /** |
| 135 | + * Data provider. |
| 136 | + * |
| 137 | + * @see testSuggestTypeOther() |
| 138 | + * |
| 139 | + * @return array<string, array<string, string>> |
| 140 | + */ |
| 141 | + public function dataSuggestTypeOther() |
| 142 | + { |
| 143 | + return [ |
| 144 | + // Short forms. |
| 145 | + 'Short form type: bool, lowercase' => [ |
| 146 | + 'varType' => 'bool', |
| 147 | + 'expected' => 'boolean', |
| 148 | + ], |
| 149 | + 'Short form type: bool, uppercase' => [ |
| 150 | + 'varType' => 'BOOL', |
| 151 | + 'expected' => 'boolean', |
| 152 | + ], |
| 153 | + 'Short form type: double, lowercase' => [ |
| 154 | + 'varType' => 'double', |
| 155 | + 'expected' => 'float', |
| 156 | + ], |
| 157 | + 'Short form type: real, mixed case' => [ |
| 158 | + 'varType' => 'Real', |
| 159 | + 'expected' => 'float', |
| 160 | + ], |
| 161 | + 'Short form type: double, mixed case' => [ |
| 162 | + 'varType' => 'DoUbLe', |
| 163 | + 'expected' => 'float', |
| 164 | + ], |
| 165 | + 'Short form type: int, lowercase' => [ |
| 166 | + 'varType' => 'int', |
| 167 | + 'expected' => 'integer', |
| 168 | + ], |
| 169 | + 'Short form type: int, uppercase' => [ |
| 170 | + 'varType' => 'INT', |
| 171 | + 'expected' => 'integer', |
| 172 | + ], |
| 173 | + |
| 174 | + // Array types. |
| 175 | + 'Array type: mixed case keyword, empty parentheses' => [ |
| 176 | + 'varType' => 'Array()', |
| 177 | + 'expected' => 'array', |
| 178 | + ], |
| 179 | + 'Array type: short form type as value within the parentheses' => [ |
| 180 | + 'varType' => 'array(real)', |
| 181 | + 'expected' => 'array(float)', |
| 182 | + ], |
| 183 | + 'Array type: short form type as key within the parentheses' => [ |
| 184 | + 'varType' => 'array(int => object)', |
| 185 | + 'expected' => 'array(integer => object)', |
| 186 | + ], |
| 187 | + 'Array type: valid specification' => [ |
| 188 | + 'varType' => 'array(integer => array(string => resource))', |
| 189 | + 'expected' => 'array(integer => array(string => resource))', |
| 190 | + ], |
| 191 | + 'Array type: short form + uppercase types within the parentheses' => [ |
| 192 | + 'varType' => 'ARRAY(BOOL => DOUBLE)', |
| 193 | + 'expected' => 'array(boolean => float)', |
| 194 | + ], |
| 195 | + 'Array type: no space around the arrow within the parentheses' => [ |
| 196 | + 'varType' => 'array(string=>resource)', |
| 197 | + 'expected' => 'array(string => resource)', |
| 198 | + ], |
| 199 | + |
| 200 | + // Incomplete array type. |
| 201 | + 'Array type: incomplete specification' => [ |
| 202 | + 'varType' => 'array(int =>', |
| 203 | + 'expected' => 'array', |
| 204 | + ], |
| 205 | + |
| 206 | + // Custom types are returned unchanged. |
| 207 | + 'Unknown type: "<string> => <int>"' => [ |
| 208 | + 'varType' => '<string> => <int>', |
| 209 | + 'expected' => '<string> => <int>', |
| 210 | + ], |
| 211 | + 'Unknown type: "string[]"' => [ |
| 212 | + 'varType' => 'string[]', |
| 213 | + 'expected' => 'string[]', |
| 214 | + ], |
| 215 | + 'Unknown type: "\DateTime"' => [ |
| 216 | + 'varType' => '\DateTime', |
| 217 | + 'expected' => '\DateTime', |
| 218 | + ], |
| 219 | + ]; |
| 220 | + |
| 221 | + }//end dataSuggestTypeOther() |
| 222 | + |
| 223 | + |
| 224 | +}//end class |
0 commit comments