Skip to content

Commit

Permalink
Do not array_shift() foreign keys in tests
Browse files Browse the repository at this point in the history
PhpStorm loses type information about array_shift()'ed elements and
doesn't report the usage of deprecated methods.

Also, shifting requires non-null assertions even if the count is known.
  • Loading branch information
morozov committed Jan 18, 2025
1 parent 6978a8f commit d6e63e6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
33 changes: 20 additions & 13 deletions tests/Functional/Schema/SQLiteSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use function array_keys;
use function array_shift;
use function array_values;

class SQLiteSchemaManagerTest extends SchemaManagerFunctionalTestCase
{
Expand Down Expand Up @@ -47,6 +48,7 @@ public function createListTableColumns(): Table
return $table;
}

/** @throws Exception */
public function testListForeignKeysFromExistingDatabase(): void
{
$this->connection->executeStatement('DROP TABLE IF EXISTS user');
Expand Down Expand Up @@ -254,6 +256,7 @@ public function testAlterTableWithSchema(): void
self::assertSame(['b'], array_keys($this->schemaManager->listTableColumns('t')));
}

/** @throws Exception */
public function testIntrospectMultipleAnonymousForeignKeyConstraints(): void
{
$this->dropTableIfExists('album');
Expand Down Expand Up @@ -285,25 +288,26 @@ public function testIntrospectMultipleAnonymousForeignKeyConstraints(): void

$schemaManager = $this->connection->createSchemaManager();

$song = $schemaManager->introspectTable('song');
$foreignKeys = $song->getForeignKeys();
$song = $schemaManager->introspectTable('song');

/** @var list<ForeignKeyConstraint> $foreignKeys */
$foreignKeys = array_values($song->getForeignKeys());
self::assertCount(2, $foreignKeys);

$foreignKey1 = array_shift($foreignKeys);
self::assertNotNull($foreignKey1);
$foreignKey1 = $foreignKeys[0];
self::assertEmpty($foreignKey1->getName());

self::assertSame(['album_id'], $foreignKey1->getLocalColumns());
self::assertSame(['id'], $foreignKey1->getForeignColumns());

$foreignKey2 = array_shift($foreignKeys);
self::assertNotNull($foreignKey2);
$foreignKey2 = $foreignKeys[1];
self::assertEmpty($foreignKey2->getName());

self::assertSame(['artist_id'], $foreignKey2->getLocalColumns());
self::assertSame(['id'], $foreignKey2->getForeignColumns());
}

/** @throws Exception */
public function testNoWhitespaceInForeignKeyReference(): void
{
$this->dropTableIfExists('notes');
Expand All @@ -323,16 +327,18 @@ public function testNoWhitespaceInForeignKeyReference(): void
$this->connection->executeStatement($ddl);
$notes = $this->schemaManager->introspectTable('notes');

$foreignKeys = $notes->getForeignKeys();
/** @var list<ForeignKeyConstraint> $foreignKeys */
$foreignKeys = array_values($notes->getForeignKeys());
self::assertCount(1, $foreignKeys);

$foreignKey = array_shift($foreignKeys);
self::assertNotNull($foreignKey);
$foreignKey = $foreignKeys[0];

self::assertSame(['created_by'], $foreignKey->getLocalColumns());
self::assertSame('users', $foreignKey->getForeignTableName());
self::assertSame(['id'], $foreignKey->getForeignColumns());
}

/** @throws Exception */
public function testShorthandInForeignKeyReference(): void
{
$this->dropTableIfExists('artist');
Expand All @@ -355,12 +361,13 @@ public function testShorthandInForeignKeyReference(): void

$schemaManager = $this->connection->createSchemaManager();

$song = $schemaManager->introspectTable('track');
$foreignKeys = $song->getForeignKeys();
$song = $schemaManager->introspectTable('track');

/** @var list<ForeignKeyConstraint> $foreignKeys */
$foreignKeys = array_values($song->getForeignKeys());
self::assertCount(1, $foreignKeys);

$foreignKey1 = array_shift($foreignKeys);
self::assertNotNull($foreignKey1);
$foreignKey1 = $foreignKeys[0];
self::assertEmpty($foreignKey1->getName());

self::assertSame(['trackartist'], $foreignKey1->getLocalColumns());
Expand Down
16 changes: 9 additions & 7 deletions tests/Functional/Schema/SchemaManagerFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Schema\AbstractAsset;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Name\OptionallyQualifiedName;
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
use Doctrine\DBAL\Schema\Schema;
Expand Down Expand Up @@ -40,7 +41,6 @@
use function array_keys;
use function array_map;
use function array_search;
use function array_shift;
use function array_values;
use function count;
use function current;
Expand Down Expand Up @@ -499,6 +499,7 @@ public function testMigrateSchema(): void
self::assertTrue($schema->hasTable('table_to_create'));
}

/** @throws Exception */
public function testAlterTableScenario(): void
{
$this->createTestTable('alter_table');
Expand Down Expand Up @@ -587,10 +588,10 @@ public function testAlterTableScenario(): void
// don't check for index size here, some platforms automatically add indexes for foreign keys.
self::assertFalse($table->hasIndex('bar_idx'));

$fks = $table->getForeignKeys();
/** @var list<ForeignKeyConstraint> $fks */
$fks = array_values($table->getForeignKeys());
self::assertCount(1, $fks);
$foreignKey = array_shift($fks);
self::assertNotNull($foreignKey);
$foreignKey = $fks[0];

self::assertEquals('alter_table_foreign', strtolower($foreignKey->getForeignTableName()));
self::assertEquals(['foreign_key_test'], array_map('strtolower', $foreignKey->getLocalColumns()));
Expand Down Expand Up @@ -691,6 +692,7 @@ public function testAutoincrementDetectionMulticolumns(): void
self::assertFalse($inferredTable->getColumn('id')->getAutoincrement());
}

/** @throws Exception */
public function testUpdateSchemaWithForeignKeyRenaming(): void
{
$table = new Table('test_fk_base');
Expand Down Expand Up @@ -741,10 +743,10 @@ public function testUpdateSchemaWithForeignKeyRenaming(): void
$table = $this->schemaManager->introspectTable('test_fk_rename');
self::assertTrue($table->hasColumn('rename_fk_id'));

$foreignKeys = $table->getForeignKeys();
/** @var list<ForeignKeyConstraint> $foreignKeys */
$foreignKeys = array_values($table->getForeignKeys());
self::assertCount(1, $foreignKeys);
$foreignKey = array_shift($foreignKeys);
self::assertNotNull($foreignKey);
$foreignKey = $foreignKeys[0];

self::assertSame(['rename_fk_id'], array_map('strtolower', $foreignKey->getLocalColumns()));
}
Expand Down

0 comments on commit d6e63e6

Please sign in to comment.