Merge pull request #19354 from liviuconcioiu/file
[phpmyadmin.git] / tests / unit / TypesTest.php
blob0104fc84e9d7729f590baa465063f47561287531
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests;
7 use PhpMyAdmin\DatabaseInterface;
8 use PhpMyAdmin\Types;
9 use PHPUnit\Framework\Attributes\CoversClass;
10 use PHPUnit\Framework\Attributes\DataProvider;
12 #[CoversClass(Types::class)]
13 class TypesTest extends AbstractTestCase
15 protected Types $object;
17 /**
18 * Sets up the fixture, for example, opens a network connection.
19 * This method is called before a test is executed.
21 protected function setUp(): void
23 parent::setUp();
25 DatabaseInterface::$instance = $this->createDatabaseInterface();
26 $this->object = new Types(DatabaseInterface::getInstance());
29 /**
30 * Test for isUnaryOperator
32 public function testUnary(): void
34 self::assertTrue($this->object->isUnaryOperator('IS NULL'));
35 self::assertFalse($this->object->isUnaryOperator('='));
38 /**
39 * Test for getUnaryOperators
41 public function testGetUnaryOperators(): void
43 self::assertSame(
44 ['IS NULL', 'IS NOT NULL', "= ''", "!= ''"],
45 $this->object->getUnaryOperators(),
49 /**
50 * Test for getNullOperators
52 public function testGetNullOperators(): void
54 self::assertSame(
55 ['IS NULL', 'IS NOT NULL'],
56 $this->object->getNullOperators(),
60 /**
61 * Test for getEnumOperators
63 public function testGetEnumOperators(): void
65 self::assertSame(
66 ['=', '!='],
67 $this->object->getEnumOperators(),
71 /**
72 * Test for getTextOperators
74 public function testgetTextOperators(): void
76 self::assertSame(
78 'LIKE',
79 'LIKE %...%',
80 'NOT LIKE',
81 'NOT LIKE %...%',
82 '=',
83 '!=',
84 'REGEXP',
85 'REGEXP ^...$',
86 'NOT REGEXP',
87 "= ''",
88 "!= ''",
89 'IN (...)',
90 'NOT IN (...)',
91 'BETWEEN',
92 'NOT BETWEEN',
94 $this->object->getTextOperators(),
98 /**
99 * Test for getNumberOperators
101 public function testGetNumberOperators(): void
103 self::assertSame(
105 '=',
106 '>',
107 '>=',
108 '<',
109 '<=',
110 '!=',
111 'LIKE',
112 'LIKE %...%',
113 'NOT LIKE',
114 'NOT LIKE %...%',
115 'IN (...)',
116 'NOT IN (...)',
117 'BETWEEN',
118 'NOT BETWEEN',
120 $this->object->getNumberOperators(),
125 * Test for getUUIDOperators
127 public function testGetUUIDOperators(): void
129 self::assertSame(
130 ['=', '!=', 'LIKE', 'LIKE %...%', 'NOT LIKE', 'NOT LIKE %...%', 'IN (...)', 'NOT IN (...)'],
131 $this->object->getUUIDOperators(),
136 * Test for getting type operators
138 * @param string $type Type of field
139 * @param bool $null Whether field can be NULL
140 * @param string|string[] $output Expected output
142 #[DataProvider('providerForGetTypeOperators')]
143 public function testGetTypeOperators(string $type, bool $null, string|array $output): void
145 self::assertSame(
146 $output,
147 $this->object->getTypeOperators($type, $null),
152 * data provider for testGetTypeOperators
154 * @return array<array{string, bool, string|string[]}>
156 public static function providerForGetTypeOperators(): array
158 return [
159 ['enum', false, ['=', '!=']],
161 'CHAR',
162 true,
164 'LIKE',
165 'LIKE %...%',
166 'NOT LIKE',
167 'NOT LIKE %...%',
168 '=',
169 '!=',
170 'REGEXP',
171 'REGEXP ^...$',
172 'NOT REGEXP',
173 '= \'\'',
174 '!= \'\'',
175 'IN (...)',
176 'NOT IN (...)',
177 'BETWEEN',
178 'NOT BETWEEN',
179 'IS NULL',
180 'IS NOT NULL',
184 'int',
185 false,
187 '=',
188 '>',
189 '>=',
190 '<',
191 '<=',
192 '!=',
193 'LIKE',
194 'LIKE %...%',
195 'NOT LIKE',
196 'NOT LIKE %...%',
197 'IN (...)',
198 'NOT IN (...)',
199 'BETWEEN',
200 'NOT BETWEEN',
204 'UUID',
205 false,
206 ['=', '!=', 'LIKE', 'LIKE %...%', 'NOT LIKE', 'NOT LIKE %...%', 'IN (...)', 'NOT IN (...)'],
209 'UUID',
210 true,
212 '=',
213 '!=',
214 'LIKE',
215 'LIKE %...%',
216 'NOT LIKE',
217 'NOT LIKE %...%',
218 'IN (...)',
219 'NOT IN (...)',
220 'IS NULL',
221 'IS NOT NULL',
228 * Test for getTypeOperatorsHtml
230 * @param string $type Type of field
231 * @param bool $null Whether field can be NULL
232 * @param string $selectedOperator Option to be selected
233 * @param string $output Expected output
235 #[DataProvider('providerForTestGetTypeOperatorsHtml')]
236 public function testGetTypeOperatorsHtml(
237 string $type,
238 bool $null,
239 string $selectedOperator,
240 string $output,
241 ): void {
242 self::assertSame(
243 $output,
244 $this->object->getTypeOperatorsHtml($type, $null, $selectedOperator),
249 * Provider for testGetTypeOperatorsHtml
251 * @return array<array{string, bool, string, string}>
253 public static function providerForTestGetTypeOperatorsHtml(): array
255 return [
256 ['enum', false, '=', '<option value="=" selected="selected">=</option><option value="!=">!=</option>'],
261 * Test for getTypeDescription
263 * @param string $type The data type to get a description.
265 #[DataProvider('providerForTestGetTypeDescription')]
266 public function testGetTypeDescription(string $type): void
268 self::assertNotEquals(
270 $this->object->getTypeDescription($type),
275 * Test for getTypeDescription with unknown value
277 public function testGetUnknownTypeDescription(): void
279 self::assertSame(
281 $this->object->getTypeDescription('UNKNOWN'),
286 * Provider for testGetTypeDescription
288 * @return array<array{string}>
290 public static function providerForTestGetTypeDescription(): array
292 return [
293 ['TINYINT'],
294 ['SMALLINT'],
295 ['MEDIUMINT'],
296 ['INT'],
297 ['BIGINT'],
298 ['DECIMAL'],
299 ['FLOAT'],
300 ['DOUBLE'],
301 ['REAL'],
302 ['BIT'],
303 ['BOOLEAN'],
304 ['SERIAL'],
305 ['DATE'],
306 ['DATETIME'],
307 ['TIMESTAMP'],
308 ['TIME'],
309 ['YEAR'],
310 ['CHAR'],
311 ['VARCHAR'],
312 ['TINYTEXT'],
313 ['TEXT'],
314 ['MEDIUMTEXT'],
315 ['LONGTEXT'],
316 ['BINARY'],
317 ['VARBINARY'],
318 ['TINYBLOB'],
319 ['MEDIUMBLOB'],
320 ['BLOB'],
321 ['LONGBLOB'],
322 ['ENUM'],
323 ['SET'],
324 ['GEOMETRY'],
325 ['POINT'],
326 ['LINESTRING'],
327 ['POLYGON'],
328 ['MULTIPOINT'],
329 ['MULTILINESTRING'],
330 ['MULTIPOLYGON'],
331 ['GEOMETRYCOLLECTION'],
332 ['JSON'],
333 ['INET6'],
334 ['UUID'],
339 * @param string $class The class to get function list.
340 * @param string[] $output Expected function list
342 #[DataProvider('providerFortTestGetFunctionsClass')]
343 public function testGetFunctionsClass(string $class, array $output): void
345 self::assertSame(
346 $output,
347 $this->object->getFunctionsClass($class),
351 /** @return array<array{string, string[]}> */
352 public static function providerFortTestGetFunctionsClass(): array
354 return [
356 'CHAR',
358 'AES_DECRYPT',
359 'AES_ENCRYPT',
360 'BIN',
361 'CHAR',
362 'COMPRESS',
363 'CURRENT_USER',
364 'DATABASE',
365 'DAYNAME',
366 'DES_DECRYPT',
367 'DES_ENCRYPT',
368 'ENCRYPT',
369 'HEX',
370 'INET6_NTOA',
371 'INET_NTOA',
372 'LOAD_FILE',
373 'LOWER',
374 'LTRIM',
375 'MD5',
376 'MONTHNAME',
377 'OLD_PASSWORD',
378 'PASSWORD',
379 'QUOTE',
380 'REVERSE',
381 'RTRIM',
382 'SHA1',
383 'SHA2',
384 'SOUNDEX',
385 'SPACE',
386 'TRIM',
387 'UNCOMPRESS',
388 'UNHEX',
389 'UPPER',
390 'USER',
391 'UUID',
392 'VERSION',
396 'DATE',
398 'CURRENT_DATE',
399 'CURRENT_TIME',
400 'DATE',
401 'FROM_DAYS',
402 'FROM_UNIXTIME',
403 'LAST_DAY',
404 'NOW',
405 'SEC_TO_TIME',
406 'SYSDATE',
407 'TIME',
408 'TIMESTAMP',
409 'UTC_DATE',
410 'UTC_TIME',
411 'UTC_TIMESTAMP',
412 'YEAR',
416 'SPATIAL',
418 'ST_GeomFromText',
419 'ST_GeomFromWKB',
421 'ST_GeomCollFromText',
422 'ST_LineFromText',
423 'ST_MLineFromText',
424 'ST_PointFromText',
425 'ST_MPointFromText',
426 'ST_PolyFromText',
427 'ST_MPolyFromText',
429 'ST_GeomCollFromWKB',
430 'ST_LineFromWKB',
431 'ST_MLineFromWKB',
432 'ST_PointFromWKB',
433 'ST_MPointFromWKB',
434 'ST_PolyFromWKB',
435 'ST_MPolyFromWKB',
439 'NUMBER',
441 'ABS',
442 'ACOS',
443 'ASCII',
444 'ASIN',
445 'ATAN',
446 'BIT_LENGTH',
447 'BIT_COUNT',
448 'CEILING',
449 'CHAR_LENGTH',
450 'CONNECTION_ID',
451 'COS',
452 'COT',
453 'CRC32',
454 'DAYOFMONTH',
455 'DAYOFWEEK',
456 'DAYOFYEAR',
457 'DEGREES',
458 'EXP',
459 'FLOOR',
460 'HOUR',
461 'INET6_ATON',
462 'INET_ATON',
463 'LENGTH',
464 'LN',
465 'LOG',
466 'LOG2',
467 'LOG10',
468 'MICROSECOND',
469 'MINUTE',
470 'MONTH',
471 'OCT',
472 'ORD',
473 'PI',
474 'QUARTER',
475 'RADIANS',
476 'RAND',
477 'ROUND',
478 'SECOND',
479 'SIGN',
480 'SIN',
481 'SQRT',
482 'TAN',
483 'TO_DAYS',
484 'TO_SECONDS',
485 'TIME_TO_SEC',
486 'UNCOMPRESSED_LENGTH',
487 'UNIX_TIMESTAMP',
488 'UUID_SHORT',
489 'WEEK',
490 'WEEKDAY',
491 'WEEKOFYEAR',
492 'YEARWEEK',
495 ['UNKNOWN', []],
500 * Test for getFunctions
502 public function testGetFunctions(): void
504 self::assertSame(
506 'AES_DECRYPT',
507 'AES_ENCRYPT',
508 'BIN',
509 'CHAR',
510 'COMPRESS',
511 'CURRENT_USER',
512 'DATABASE',
513 'DAYNAME',
514 'DES_DECRYPT',
515 'DES_ENCRYPT',
516 'ENCRYPT',
517 'HEX',
518 'INET6_NTOA',
519 'INET_NTOA',
520 'LOAD_FILE',
521 'LOWER',
522 'LTRIM',
523 'MD5',
524 'MONTHNAME',
525 'OLD_PASSWORD',
526 'PASSWORD',
527 'QUOTE',
528 'REVERSE',
529 'RTRIM',
530 'SHA1',
531 'SHA2',
532 'SOUNDEX',
533 'SPACE',
534 'TRIM',
535 'UNCOMPRESS',
536 'UNHEX',
537 'UPPER',
538 'USER',
539 'UUID',
540 'VERSION',
542 $this->object->getFunctions('enum'),
547 * Test for getAllFunctions
549 public function testGetAllFunctions(): void
551 self::assertSame(
553 'ABS',
554 'ACOS',
555 'AES_DECRYPT',
556 'AES_ENCRYPT',
557 'ASCII',
558 'ASIN',
559 'ATAN',
560 'BIN',
561 'BIT_COUNT',
562 'BIT_LENGTH',
563 'CEILING',
564 'CHAR',
565 'CHAR_LENGTH',
566 'COMPRESS',
567 'CONNECTION_ID',
568 'COS',
569 'COT',
570 'CRC32',
571 'CURRENT_DATE',
572 'CURRENT_TIME',
573 'CURRENT_USER',
574 'DATABASE',
575 'DATE',
576 'DAYNAME',
577 'DAYOFMONTH',
578 'DAYOFWEEK',
579 'DAYOFYEAR',
580 'DEGREES',
581 'DES_DECRYPT',
582 'DES_ENCRYPT',
583 'ENCRYPT',
584 'EXP',
585 'FLOOR',
586 'FROM_DAYS',
587 'FROM_UNIXTIME',
588 'HEX',
589 'HOUR',
590 'INET6_ATON',
591 'INET6_NTOA',
592 'INET_ATON',
593 'INET_NTOA',
594 'LAST_DAY',
595 'LENGTH',
596 'LN',
597 'LOAD_FILE',
598 'LOG',
599 'LOG10',
600 'LOG2',
601 'LOWER',
602 'LTRIM',
603 'MD5',
604 'MICROSECOND',
605 'MINUTE',
606 'MONTH',
607 'MONTHNAME',
608 'NOW',
609 'OCT',
610 'OLD_PASSWORD',
611 'ORD',
612 'PASSWORD',
613 'PI',
614 'QUARTER',
615 'QUOTE',
616 'RADIANS',
617 'RAND',
618 'REVERSE',
619 'ROUND',
620 'RTRIM',
621 'SECOND',
622 'SEC_TO_TIME',
623 'SHA1',
624 'SHA2',
625 'SIGN',
626 'SIN',
627 'SOUNDEX',
628 'SPACE',
629 'SQRT',
630 'ST_GeomCollFromText',
631 'ST_GeomCollFromWKB',
632 'ST_GeomFromText',
633 'ST_GeomFromWKB',
634 'ST_LineFromText',
635 'ST_LineFromWKB',
636 'ST_MLineFromText',
637 'ST_MLineFromWKB',
638 'ST_MPointFromText',
639 'ST_MPointFromWKB',
640 'ST_MPolyFromText',
641 'ST_MPolyFromWKB',
642 'ST_PointFromText',
643 'ST_PointFromWKB',
644 'ST_PolyFromText',
645 'ST_PolyFromWKB',
646 'SYSDATE',
647 'TAN',
648 'TIME',
649 'TIMESTAMP',
650 'TIME_TO_SEC',
651 'TO_DAYS',
652 'TO_SECONDS',
653 'TRIM',
654 'UNCOMPRESS',
655 'UNCOMPRESSED_LENGTH',
656 'UNHEX',
657 'UNIX_TIMESTAMP',
658 'UPPER',
659 'USER',
660 'UTC_DATE',
661 'UTC_TIME',
662 'UTC_TIMESTAMP',
663 'UUID',
664 'UUID_SHORT',
665 'VERSION',
666 'WEEK',
667 'WEEKDAY',
668 'WEEKOFYEAR',
669 'YEAR',
670 'YEARWEEK',
672 $this->object->getAllFunctions(),
677 * Test for getAttributes
679 public function testGetAttributes(): void
681 self::assertSame(
682 ['', 'BINARY', 'UNSIGNED', 'UNSIGNED ZEROFILL', 'on update CURRENT_TIMESTAMP'],
683 $this->object->getAttributes(),
688 * Test for getColumns
690 public function testGetColumns(): void
692 self::assertSame(
694 0 => 'INT',
695 1 => 'VARCHAR',
696 2 => 'TEXT',
697 3 => 'DATE',
698 'Numeric' => [
699 'TINYINT',
700 'SMALLINT',
701 'MEDIUMINT',
702 'INT',
703 'BIGINT',
704 '-',
705 'DECIMAL',
706 'FLOAT',
707 'DOUBLE',
708 'REAL',
709 '-',
710 'BIT',
711 'BOOLEAN',
712 'SERIAL',
714 'Date and time' => ['DATE', 'DATETIME', 'TIMESTAMP', 'TIME', 'YEAR'],
715 'String' => [
716 'CHAR',
717 'VARCHAR',
718 '-',
719 'TINYTEXT',
720 'TEXT',
721 'MEDIUMTEXT',
722 'LONGTEXT',
723 '-',
724 'BINARY',
725 'VARBINARY',
726 '-',
727 'TINYBLOB',
728 'BLOB',
729 'MEDIUMBLOB',
730 'LONGBLOB',
731 '-',
732 'ENUM',
733 'SET',
735 'Spatial' => [
736 'GEOMETRY',
737 'POINT',
738 'LINESTRING',
739 'POLYGON',
740 'MULTIPOINT',
741 'MULTILINESTRING',
742 'MULTIPOLYGON',
743 'GEOMETRYCOLLECTION',
745 'JSON' => ['JSON'],
747 $this->object->getColumns(),
752 * @param string $type Type to check
753 * @param string $output Expected result
755 #[DataProvider('providerFortTestGetTypeClass')]
756 public function testGetTypeClass(string $type, string $output): void
758 self::assertSame(
759 $output,
760 $this->object->getTypeClass($type),
765 * Data provider for type testing
767 * @return array<array{string, string}>
769 public static function providerFortTestGetTypeClass(): array
771 return [
772 ['SERIAL', 'NUMBER'],
773 ['YEAR', 'DATE'],
774 ['GEOMETRYCOLLECTION', 'SPATIAL'],
775 ['SET', 'CHAR'],
776 ['JSON', 'JSON'],
777 ['UUID', 'UUID'],
778 ['UNKNOWN', ''],