Skip to content

Commit ac1364d

Browse files
greg0irerela589n
authored andcommitted
Convert test into 2 unit tests
That test was testing too many thing and not really making it clear what the expected output was, given some output. Instead, let us create 2 tests, each pertaining to the class under test.
1 parent d42c295 commit ac1364d

File tree

3 files changed

+96
-46
lines changed

3 files changed

+96
-46
lines changed

tests/Tests/ORM/Functional/Ticket/DDC2825Test.php

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
use PHPUnit\Framework\Attributes\DataProvider;
1212
use PHPUnit\Framework\Attributes\Group;
1313

14-
use function sprintf;
15-
1614
/**
1715
* This class makes tests on the correct use of a database schema when entities are stored
1816
*/
@@ -30,41 +28,7 @@ protected function setUp(): void
3028
}
3129
}
3230

33-
#[DataProvider('getTestedClasses')]
34-
public function testClassSchemaMappingsValidity(
35-
string $className,
36-
string $expectedSchemaName,
37-
string $expectedTableName,
38-
bool $isQuoted,
39-
): void {
40-
$classMetadata = $this->_em->getClassMetadata($className);
41-
$platform = $this->_em->getConnection()->getDatabasePlatform();
42-
$quotedTableName = $this->_em->getConfiguration()->getQuoteStrategy()->getTableName($classMetadata, $platform);
43-
44-
// Check if table name and schema properties are defined in the class metadata
45-
self::assertEquals($expectedTableName, $classMetadata->table['name']);
46-
self::assertEquals($expectedSchemaName, $classMetadata->table['schema']);
47-
48-
$fullTableName = sprintf(
49-
$isQuoted ? '"%s"."%s"' : '%s.%s',
50-
$expectedSchemaName,
51-
$expectedTableName,
52-
);
53-
54-
self::assertEquals($fullTableName, $quotedTableName);
55-
56-
// Checks sequence name validity
57-
self::assertEquals(
58-
sprintf(
59-
'%s.%s_%s_seq',
60-
$expectedSchemaName,
61-
$expectedTableName,
62-
$classMetadata->getSingleIdentifierColumnName(),
63-
),
64-
$classMetadata->getSequenceName($platform),
65-
);
66-
}
67-
31+
/** @param class-string $className */
6832
#[DataProvider('getTestedClasses')]
6933
public function testPersistenceOfEntityWithSchemaMapping(string $className): void
7034
{
@@ -77,18 +41,14 @@ public function testPersistenceOfEntityWithSchemaMapping(string $className): voi
7741
self::assertCount(1, $this->_em->getRepository($className)->findAll());
7842
}
7943

80-
/**
81-
* Data provider
82-
*
83-
* @return string[][]
84-
*/
44+
/** @return list<array{class-string}> */
8545
public static function getTestedClasses(): array
8646
{
8747
return [
88-
[ExplicitSchemaAndTable::class, 'explicit_schema', 'explicit_table', false],
89-
[SchemaAndTableInTableName::class, 'implicit_schema', 'implicit_table', false],
90-
[DDC2825ClassWithImplicitlyDefinedSchemaAndQuotedTableName::class, 'myschema', 'order', false],
91-
[File::class, 'yourschema', 'file', true],
48+
[ExplicitSchemaAndTable::class],
49+
[SchemaAndTableInTableName::class],
50+
[DDC2825ClassWithImplicitlyDefinedSchemaAndQuotedTableName::class],
51+
[File::class],
9252
];
9353
}
9454
}

tests/Tests/ORM/Mapping/ClassMetadataTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Doctrine\Tests\ORM\Mapping;
66

77
use ArrayObject;
8+
use Doctrine\DBAL\Platforms\AbstractPlatform;
89
use Doctrine\DBAL\Types\Types;
910
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
1011
use Doctrine\ORM\Events;
@@ -51,6 +52,7 @@
5152
use Doctrine\Tests\OrmTestCase;
5253
use DoctrineGlobalArticle;
5354
use LogicException;
55+
use PHPUnit\Framework\Attributes\DataProvider;
5456
use PHPUnit\Framework\Attributes\Group as TestGroup;
5557
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
5658
use ReflectionClass;
@@ -970,6 +972,47 @@ public function testQuotedSequenceName(): void
970972
);
971973
}
972974

975+
#[DataProvider('fullTableNameProvider')]
976+
public function testGetSequenceName(
977+
string $expectedSequenceName,
978+
string $fullTableName,
979+
): void {
980+
$cm = new ClassMetadata(self::class);
981+
$cm->setIdentifier(['id']);
982+
$cm->setPrimaryTable(['name' => $fullTableName]);
983+
984+
$platform = $this->createStub(AbstractPlatform::class);
985+
986+
self::assertSame(
987+
$expectedSequenceName,
988+
$cm->getSequenceName($platform),
989+
);
990+
}
991+
992+
/** @return iterable<string, array{string, string}> */
993+
public static function fullTableNameProvider(): iterable
994+
{
995+
yield 'quoted table name with schema' => [
996+
'custom.reserved_id_seq',
997+
'custom.`reserved`',
998+
];
999+
1000+
yield 'unquoted table name with schema' => [
1001+
'custom.non_reserved_id_seq',
1002+
'custom.non_reserved',
1003+
];
1004+
1005+
yield 'quoted table name without schema' => [
1006+
'reserved_id_seq',
1007+
'`reserved`',
1008+
];
1009+
1010+
yield 'unquoted table name without schema' => [
1011+
'non_reserved_id_seq',
1012+
'non_reserved',
1013+
];
1014+
}
1015+
9731016
#[TestGroup('DDC-2700')]
9741017
public function testIsIdentifierMappedSuperClass(): void
9751018
{

tests/Tests/ORM/Mapping/DefaultQuoteStrategyTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66

77
use Doctrine\DBAL\Platforms\AbstractPlatform;
88
use Doctrine\DBAL\Schema\Name\UnquotedIdentifierFolding;
9+
use Doctrine\ORM\Mapping\ClassMetadata;
910
use Doctrine\ORM\Mapping\DefaultQuoteStrategy;
1011
use Doctrine\Tests\Models\NonPublicSchemaJoins\User as NonPublicSchemaUser;
1112
use Doctrine\Tests\OrmTestCase;
13+
use PHPUnit\Framework\Attributes\DataProvider;
1214
use PHPUnit\Framework\Attributes\Group;
1315

1416
use function assert;
1517
use function enum_exists;
18+
use function sprintf;
1619

1720
/**
1821
* Doctrine\Tests\ORM\Mapping\DefaultQuoteStrategyTest
@@ -34,4 +37,48 @@ public function testGetJoinTableName(): void
3437
$strategy->getJoinTableName($metadata->associationMappings['readers'], $metadata, $platform),
3538
);
3639
}
40+
41+
#[DataProvider('fullTableNameProvider')]
42+
public function testGetTableName(string $expectedFullTableName, string $tableName): void
43+
{
44+
$classMetadata = new ClassMetadata(self::class);
45+
$classMetadata->setPrimaryTable(['name' => $tableName]);
46+
47+
$platform = $this->createStub(AbstractPlatform::class);
48+
$platform->method('quoteSingleIdentifier')
49+
->willReturnCallback(
50+
static fn (string $identifier): string => sprintf('✌️%s✌️', $identifier),
51+
);
52+
53+
$quotedTableName = (new DefaultQuoteStrategy())->getTableName(
54+
$classMetadata,
55+
$platform,
56+
);
57+
58+
self::assertSame($expectedFullTableName, $quotedTableName);
59+
}
60+
61+
/** @return iterable<string, array{string, string}> */
62+
public static function fullTableNameProvider(): iterable
63+
{
64+
yield 'quoted table name with schema' => [
65+
'✌️custom✌️.✌️reserved✌️',
66+
'custom.`reserved`',
67+
];
68+
69+
yield 'unquoted table name with schema' => [
70+
'custom.non_reserved',
71+
'custom.non_reserved',
72+
];
73+
74+
yield 'quoted table name without schema' => [
75+
'✌️reserved✌️',
76+
'`reserved`',
77+
];
78+
79+
yield 'unquoted table name without schema' => [
80+
'non_reserved',
81+
'non_reserved',
82+
];
83+
}
3784
}

0 commit comments

Comments
 (0)