Skip to content

Commit 2808de0

Browse files
committed
tests: enhance test generator;
reorganize comment tests (move similar methods into common class) (cherry picked from commit a59a33f)
1 parent 9e780d8 commit 2808de0

File tree

8 files changed

+153
-93
lines changed

8 files changed

+153
-93
lines changed

src/Driver/MySQL/Schema/MySQLColumn.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,6 @@ public function sqlStatement(DriverInterface $driver): string
296296
$statement = parent::sqlStatement($driver);
297297

298298
$this->defaultValue = $defaultValue;
299-
if ($this->autoIncrement) {
300-
return "{$statement} AUTO_INCREMENT";
301-
}
302299

303300
if ($this->comment !== '') {
304301
return "{$statement} COMMENT {$driver->quote($this->comment)}";

tests/Database/Functional/Driver/Common/BaseTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function setUp(): void
4141

4242
public function tearDown(): void
4343
{
44+
$this->disableProfiling();
4445
$this->dropDatabase($this->database);
4546
}
4647

tests/Database/Functional/Driver/Common/Connection/BaseConnectionTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
use Cycle\Database\Tests\Stub\Driver\MysqlWrapDriver;
1010
use Cycle\Database\Tests\Stub\Driver\PostgresWrapDriver;
1111
use Cycle\Database\Tests\Stub\Driver\SQLiteWrapDriver;
12+
use Cycle\Database\Tests\Utils\DontGenerateAttribute;
1213

14+
#[DontGenerateAttribute]
1315
abstract class BaseConnectionTest extends BaseTest
1416
{
1517
public function setUp(): void
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\Database\Tests\Functional\Driver\Common\Schema;
6+
7+
// phpcs:ignore
8+
use Cycle\Database\Tests\Functional\Driver\Common\BaseTest;
9+
use Cycle\Database\Tests\Utils\DontGenerateAttribute;
10+
11+
#[DontGenerateAttribute]
12+
abstract class CommentTest extends BaseTest
13+
{
14+
public function testAddComment(): void
15+
{
16+
$schema = $this->schema('table');
17+
$this->assertFalse($schema->exists());
18+
19+
$column = $schema->string('target');
20+
$column->comment('foo');
21+
22+
$schema->save();
23+
24+
$schema = $this->schema('table');
25+
$this->assertTrue($schema->exists());
26+
27+
$column2 = $schema->column('target');
28+
$this->assertTrue($column2->compare($column));
29+
self::assertSame('foo', $column2->getComment());
30+
}
31+
32+
public function testChangeComment(): void
33+
{
34+
$schema = $this->schema('table');
35+
$this->assertFalse($schema->exists());
36+
37+
$column = $schema->string('target');
38+
$column->comment('foo');
39+
40+
$schema->save();
41+
42+
$schema = $this->schema('table');
43+
$this->assertTrue($schema->exists());
44+
45+
$column2 = $schema->column('target');
46+
$column2->comment('bar');
47+
48+
$schema->save();
49+
50+
$this->assertTrue($schema->column('target')->compare($column2));
51+
self::assertSame('bar', $schema->column('target')->getComment());
52+
}
53+
54+
public function testChangeCommentToEmpty(): void
55+
{
56+
$schema = $this->schema('table');
57+
$column = $schema->string('target');
58+
$column->comment('foo');
59+
60+
$schema->save();
61+
62+
$schema = $this->schema('table');
63+
$this->assertTrue($schema->exists());
64+
65+
$column2 = $schema->column('target');
66+
self::assertSame('foo', $column2->getComment());
67+
68+
$column2->comment('');
69+
70+
$schema->save();
71+
72+
$schema = $this->schema('table');
73+
$column3 = $schema->column('target');
74+
self::assertSame('', $column3->getComment());
75+
}
76+
77+
public function testCommentWithAutoIncrement(): void
78+
{
79+
$schema = $this->schema('table');
80+
$column = $schema->primary('target');
81+
$column->comment('foo');
82+
83+
$schema->save();
84+
85+
$schema = $this->schema('table');
86+
$this->assertTrue($schema->exists());
87+
88+
$column2 = $schema->column('target');
89+
$this->assertTrue($column2->compare($column));
90+
self::assertSame('foo', $column2->getComment());
91+
}
92+
93+
public function testSQLInjection(): void
94+
{
95+
$schema = $this->schema('table');
96+
$column = $schema->string('target');
97+
$column->comment('f"o\'o`');
98+
99+
$schema->save();
100+
101+
$schema = $this->schema('table');
102+
$this->assertTrue($schema->exists());
103+
104+
$column2 = $schema->column('target');
105+
$this->assertTrue($column2->compare($column));
106+
self::assertSame('f"o\'o`', $column2->getComment());
107+
}
108+
109+
public function testSQLInjectionIntegers(): void
110+
{
111+
$schema = $this->schema('table');
112+
$column = $schema->primary('target');
113+
$column->comment('f"o\'o`');
114+
115+
$schema->save();
116+
117+
$schema = $this->schema('table');
118+
$this->assertTrue($schema->exists());
119+
120+
$column2 = $schema->column('target');
121+
$this->assertTrue($column2->compare($column));
122+
self::assertSame('f"o\'o`', $column2->getComment());
123+
}
124+
}

tests/Database/Functional/Driver/MySQL/Schema/CommentTest.php

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,13 @@
55
namespace Cycle\Database\Tests\Functional\Driver\MySQL\Schema;
66

77
// phpcs:ignore
8-
use Cycle\Database\Driver\MySQL\Schema\MySQLColumn;
9-
use Cycle\Database\Tests\Functional\Driver\Common\BaseTest;
8+
use Cycle\Database\Tests\Functional\Driver\Common\Schema\CommentTest as CommonClass;
109

1110
/**
1211
* @group driver
1312
* @group driver-mysql
1413
*/
15-
class CommentTest extends BaseTest
14+
class CommentTest extends CommonClass
1615
{
1716
public const DRIVER = 'mysql';
18-
19-
public function testAddComment(): void
20-
{
21-
$schema = $this->schema('table');
22-
$this->assertFalse($schema->exists());
23-
24-
/** @var MySqlColumn $column */
25-
$column = $schema->string('target');
26-
$column->comment('foo');
27-
28-
$schema->save();
29-
30-
$schema = $this->schema('table');
31-
$this->assertTrue($schema->exists());
32-
$this->assertTrue($schema->column('target')->compare($column));
33-
}
34-
35-
public function testChangeComment(): void
36-
{
37-
$schema = $this->schema('table');
38-
$this->assertFalse($schema->exists());
39-
40-
/** @var MySqlColumn $column */
41-
$column = $schema->string('target');
42-
$column->comment('foo');
43-
44-
$schema->save();
45-
46-
$schema = $this->schema('table');
47-
$this->assertTrue($schema->exists());
48-
49-
/** @var MySqlColumn $column */
50-
$column = $schema->string('target');
51-
$column->comment('bar');
52-
53-
$schema->save();
54-
55-
$this->assertTrue($schema->column('target')->compare($column));
56-
}
5717
}

tests/Database/Functional/Driver/Postgres/Schema/CommentTest.php

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,13 @@
55
namespace Cycle\Database\Tests\Functional\Driver\Postgres\Schema;
66

77
// phpcs:ignore
8-
use Cycle\Database\Driver\Postgres\Schema\PostgresColumn;
9-
use Cycle\Database\Tests\Functional\Driver\Common\BaseTest;
8+
use Cycle\Database\Tests\Functional\Driver\Common\Schema\CommentTest as CommonClass;
109

1110
/**
1211
* @group driver
1312
* @group driver-postgres
1413
*/
15-
class CommentTest extends BaseTest
14+
class CommentTest extends CommonClass
1615
{
1716
public const DRIVER = 'postgres';
18-
19-
public function testAddComment(): void
20-
{
21-
$schema = $this->schema('table');
22-
$this->assertFalse($schema->exists());
23-
24-
/** @var PostgresColumn $column */
25-
$column = $schema->string('target');
26-
$column->comment('foo');
27-
28-
$schema->save();
29-
30-
$schema = $this->schema('table');
31-
$this->assertTrue($schema->exists());
32-
$this->assertTrue($schema->column('target')->compare($column));
33-
}
34-
35-
public function testChangeComment(): void
36-
{
37-
$schema = $this->schema('table');
38-
$this->assertFalse($schema->exists());
39-
40-
/** @var PostgresColumn $column */
41-
$column = $schema->string('target');
42-
$column->comment('foo');
43-
44-
$schema->save();
45-
46-
$schema = $this->schema('table');
47-
$this->assertTrue($schema->exists());
48-
49-
/** @var PostgresColumn $column */
50-
$column = $schema->string('target');
51-
$column->comment('bar');
52-
53-
$schema->save();
54-
55-
$this->assertTrue($schema->column('target')->compare($column));
56-
}
5717
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\Database\Tests\Utils;
6+
7+
#[\Attribute(\Attribute::TARGET_CLASS)]
8+
final class DontGenerateAttribute {}

tests/generate.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
declare(strict_types=1);
44

5+
use Cycle\Database\Tests\Utils\DontGenerateAttribute;
56
use Spiral\Tokenizer;
67

7-
\error_reporting(E_ALL | E_STRICT);
8-
\ini_set('display_errors', '1');
8+
error_reporting(E_ALL | E_STRICT);
9+
ini_set('display_errors', '1');
910

1011
//Composer
11-
require \dirname(__DIR__) . '/vendor/autoload.php';
12+
require_once dirname(__DIR__) . '/vendor/autoload.php';
1213

1314
$tokenizer = new Tokenizer\Tokenizer(new Tokenizer\Config\TokenizerConfig([
1415
'directories' => [__DIR__ . '/Database/Functional/Driver/Common'],
@@ -56,18 +57,25 @@
5657
continue;
5758
}
5859

59-
echo "Found {$class->getName()}\n";
60+
if ($class->getAttributes(DontGenerateAttribute::class) !== []) {
61+
continue;
62+
}
6063

6164
$path = \str_replace(
6265
[\str_replace('\\', '/', __DIR__), 'Database/Functional/Driver/Common/'],
6366
'',
6467
\str_replace('\\', '/', $class->getFileName()),
6568
);
6669

67-
$path = \ltrim($path, '/');
70+
$path = ltrim($path, '/');
6871

6972
foreach ($databases as $driver => $details) {
70-
$filename = \sprintf('%s%s', $details['directory'], $path);
73+
$filename = $details['directory'] . $path;
74+
if (\file_exists($filename)) {
75+
continue;
76+
}
77+
echo "Processing $filename\n";
78+
7179
$dir = \pathinfo($filename, PATHINFO_DIRNAME);
7280

7381
$namespace = \str_replace(

0 commit comments

Comments
 (0)