File indexing completed on 2024-12-22 03:47:07

0001 <?php
0002 /*
0003     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0004 
0005     SPDX-License-Identifier: MIT
0006 */
0007 
0008 require_once('../src/server/shared/utils.php');
0009 
0010 class UtilsTest extends PHPUnit\Framework\TestCase
0011 {
0012     public static function isValidIdentifier_data()
0013     {
0014         return [
0015             'empty' => [ '', false ],
0016             'number' => [ 42, false ],
0017             'numstring' => [ '42', false ],
0018             'leading number' => [ '1foo', false ],
0019             'alpha only' => [ 'foo', true ],
0020             'leading space' => [ ' foo', false ],
0021             'middle space' => [ 'f o o', false ],
0022             'trailing space' => [ 'foo ', false ],
0023             'valid' => [ 'foo42', true ],
0024             'leading underscore' => [ '_foo', true ],
0025             'underscore' => [ 'f_o_o', true ],
0026             'dots and dashes' => [ 'org.kde.unit-test', true ],
0027             'control' => [ "fo\no", false ]
0028         ];
0029     }
0030 
0031     /** @dataProvider isValidIdentifier_data */
0032     public function testIsValidIdentifier($str, $result)
0033     {
0034         $this->assertEquals($result, Utils::isValidIdentifier($str));
0035     }
0036 
0037     public static function normalize_data()
0038     {
0039         return [
0040             'empty' => [ '', '' ],
0041             'normal' => [ 'foo', 'foo' ],
0042             'dot' => [ 'org.kde.foo', 'org_kde_foo' ]
0043         ];
0044     }
0045 
0046     /** @dataProvider normalize_data */
0047     public function testNormalize($input, $output)
0048     {
0049         $this->assertEquals($output, Utils::normalizeString($input));
0050     }
0051 
0052     public static function primaryKeyColumn_data()
0053     {
0054         return [
0055             'sqlite' => [ 'sqlite', 'id', 'id INTEGER PRIMARY KEY AUTOINCREMENT' ],
0056             'pgsql' => [ 'pgsql', 'id', 'id SERIAL PRIMARY KEY' ],
0057             'mysql' => [ 'mysql', 'id', 'id INTEGER PRIMARY KEY AUTO_INCREMENT' ]
0058         ];
0059     }
0060 
0061     /** @dataProvider primaryKeyColumn_data */
0062     public function primaryKeyColumn($driver, $name, $output)
0063     {
0064         $this->assertEquals($output, Utils::primaryKeyColumnDeclaration($driver, $name));
0065     }
0066 
0067     public static function sqlStringType_data()
0068     {
0069         return [
0070             'sqlite' => [ 'sqlite', 'VARCHAR' ],
0071             'mysql' => [ 'mysql', 'VARCHAR(255)' ],
0072             'pgsql' => [ 'pgsql', 'VARCHAR' ],
0073         ];
0074     }
0075 
0076     /** @dataProvider sqlStringType_data */
0077     public function testSqlStringType($driver, $output)
0078     {
0079         $this->assertEquals($output, Utils::sqlStringType($driver));
0080     }
0081 }