354 lines
24 KiB
PHP
354 lines
24 KiB
PHP
<?php
|
|
|
|
namespace JumPHP\Ide\Tests;
|
|
|
|
use JumPHP\Ide\Doctrine\Dql;
|
|
use JumPHP\Ide\Doctrine\EntityWriter;
|
|
use JumPHP\Ide\Doctrine\Inflector;
|
|
use JumPHP\Ide\Doctrine\MappingCompare;
|
|
use JumPHP\Ide\Doctrine\MappingReader;
|
|
use JumPHP\Ide\Doctrine\Migrations;
|
|
use JumPHP\Ide\Doctrine\Rename;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/**
|
|
* The Doctrine window's core (ide/src/Doctrine): the mapping read from attributes and XML, the code the entity editor
|
|
* writes (exact texts in tests/fixtures/doctrine-expected, the untouched lines kept byte for byte), renames, DQL
|
|
* completion, the mapping beside a table, and the output of Doctrine Migrations.
|
|
*/
|
|
final class DoctrineTest extends TestCase
|
|
{
|
|
private const ROOT = __DIR__ . '/fixtures/doctrine';
|
|
private const EXPECTED = __DIR__ . '/fixtures/doctrine-expected';
|
|
|
|
/** @return array<string, array<string, mixed>> */
|
|
private static function entities(): array
|
|
{
|
|
$out = [];
|
|
foreach ((new MappingReader(self::ROOT))->read()['entities'] as $e) {
|
|
$out[$e['name']] = $e;
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
/** @param list<array<string, mixed>> $list @return array<string, array<string, mixed>> */
|
|
private static function byName(array $list): array
|
|
{
|
|
return array_column($list, null, 'name');
|
|
}
|
|
|
|
private static function source(string $name): string
|
|
{
|
|
return (string)file_get_contents(self::ROOT . "/src/Entity/$name.php");
|
|
}
|
|
|
|
private function assertSameAsExpected(string $file, string $actual): void
|
|
{
|
|
if (getenv('DOCTRINE_EXPECTED_WRITE')) {
|
|
@mkdir(self::EXPECTED, 0777, true);
|
|
file_put_contents(self::EXPECTED . "/$file", $actual);
|
|
}
|
|
self::assertSame((string)file_get_contents(self::EXPECTED . "/$file"), $actual, $file);
|
|
}
|
|
|
|
/** the lines of the original are all in the new text, in the same order (only insertions) */
|
|
private function assertOnlyInsertions(string $before, string $after): void
|
|
{
|
|
$lines = explode("\n", $after);
|
|
$at = 0;
|
|
foreach (explode("\n", $before) as $line) {
|
|
while ($at < count($lines) && $lines[$at] !== $line) {
|
|
$at++;
|
|
}
|
|
self::assertLessThan(count($lines), $at, "lost line: $line");
|
|
$at++;
|
|
}
|
|
}
|
|
|
|
public function testEntitiesKindsTablesAndRepositoriesAreRead(): void
|
|
{
|
|
$e = self::entities();
|
|
self::assertSame(['Address', 'Article', 'BaseEntity', 'Comment', 'Content', 'Employee', 'Person', 'Post', 'Profile', 'Tag', 'User', 'Video'], array_keys($e));
|
|
self::assertSame('embeddable', $e['Address']['kind']);
|
|
self::assertSame('mappedSuperclass', $e['BaseEntity']['kind']);
|
|
self::assertSame('user', $e['User']['table']);
|
|
self::assertTrue($e['User']['tableQuoted'] ?? false);
|
|
self::assertSame('App\Repository\PostRepository', $e['Post']['repository']);
|
|
self::assertSame('src/Repository/PostRepository.php', $e['Post']['repositoryFile']);
|
|
self::assertTrue($e['Post']['api']);
|
|
self::assertFalse($e['Tag']['api']);
|
|
self::assertSame('src/Entity/Post.php', $e['Post']['file']);
|
|
}
|
|
|
|
public function testFieldsHaveTheirTypesOptionsAndInferredColumns(): void
|
|
{
|
|
$post = self::byName(self::entities()['Post']['fields']);
|
|
self::assertTrue($post['id']['id']);
|
|
self::assertSame('AUTO', $post['id']['generated']);
|
|
self::assertSame('integer', $post['id']['type']);
|
|
self::assertSame(255, $post['title']['length']);
|
|
self::assertSame(['decimal', 10, 2, true], [$post['price']['type'], $post['price']['precision'], $post['price']['scale'], $post['price']['nullable']]);
|
|
self::assertSame('App\Enum\Status', $post['status']['enumType']);
|
|
self::assertSame(['draft', 'published'], array_column($post['status']['enumCases'], 'value'));
|
|
$user = self::byName(self::entities()['User']['fields']);
|
|
self::assertTrue($user['email']['unique']);
|
|
self::assertSame('json', $user['roles']['type']);
|
|
// a field of the mapped superclass, its column by the underscore naming strategy
|
|
self::assertSame(['datetime_immutable', 'created_at', 'App\Entity\BaseEntity'], [$user['createdAt']['type'], $user['createdAt']['column'], $user['createdAt']['inherited']]);
|
|
self::assertSame('full_name', self::byName(self::entities()['Person']['fields'])['fullName']['column']);
|
|
}
|
|
|
|
public function testEveryKindOfRelationKnowsItsSides(): void
|
|
{
|
|
$e = self::entities();
|
|
$post = self::byName($e['Post']['relations']);
|
|
self::assertSame(['ManyToOne', 'App\Entity\User', true, 'posts', false], [$post['author']['kind'], $post['author']['target'], $post['author']['owning'], $post['author']['inversedBy'], $post['author']['nullable']]);
|
|
self::assertSame(['author_id', 'CASCADE'], [$post['author']['joinColumns'][0]['name'], $post['author']['joinColumns'][0]['onDelete']]);
|
|
self::assertSame(['ManyToMany', true, 'post_tags'], [$post['tags']['kind'], $post['tags']['owning'], $post['tags']['joinTable']['name']]);
|
|
self::assertSame(['OneToMany', false, 'post', ['persist'], true, ['id' => 'DESC']],
|
|
[$post['comments']['kind'], $post['comments']['owning'], $post['comments']['mappedBy'], $post['comments']['cascade'], $post['comments']['orphanRemoval'], $post['comments']['orderBy']]);
|
|
$user = self::byName($e['User']['relations']);
|
|
self::assertSame(['OneToOne', true, 'profile_id', ['persist', 'remove']], [$user['profile']['kind'], $user['profile']['owning'], $user['profile']['joinColumns'][0]['name'], $user['profile']['cascade']]);
|
|
self::assertSame(['OneToMany', 'App\Entity\Post', false], [$user['posts']['kind'], $user['posts']['target'], $user['posts']['owning']]);
|
|
self::assertSame(['OneToOne', false, 'profile'], [$e['Profile']['relations'][0]['kind'], $e['Profile']['relations'][0]['owning'], $e['Profile']['relations'][0]['mappedBy']]);
|
|
self::assertSame(['ManyToMany', false, 'tags'], [$e['Tag']['relations'][0]['kind'], $e['Tag']['relations'][0]['owning'], $e['Tag']['relations'][0]['mappedBy']]);
|
|
self::assertSame(['address', 'App\Entity\Address', 'home_'], [$e['User']['embedded'][0]['name'], $e['User']['embedded'][0]['class'], $e['User']['embedded'][0]['columnPrefix']]);
|
|
}
|
|
|
|
public function testSingleTableAndJoinedInheritanceAreFollowed(): void
|
|
{
|
|
$e = self::entities();
|
|
self::assertSame('SINGLE_TABLE', $e['Content']['inheritance']['type']);
|
|
self::assertSame(['article' => 'App\Entity\Article', 'video' => 'App\Entity\Video'], $e['Content']['inheritance']['map']);
|
|
self::assertSame('kind', $e['Content']['inheritance']['column']['name']);
|
|
self::assertSame(['App\Entity\Content', 'App\Entity\Content', 'content'], [$e['Article']['parent'], $e['Article']['inheritance']['root'], $e['Article']['table']]);
|
|
self::assertContains('headline', array_column($e['Video']['fields'], 'name'));
|
|
self::assertSame(['JOINED', 'employee', 'App\Entity\Person'], [$e['Employee']['inheritance']['type'], $e['Employee']['table'], $e['Employee']['parent']]);
|
|
self::assertSame('App\Entity\BaseEntity', $e['User']['parent']);
|
|
}
|
|
|
|
public function testEnumsAreListedWithTheirCases(): void
|
|
{
|
|
$enums = (new MappingReader(self::ROOT))->read()['enums'];
|
|
self::assertSame(['App\Enum\Status', 'string', ['Draft', 'Published']], [$enums[0]['class'], $enums[0]['backing'], array_column($enums[0]['cases'], 'name')]);
|
|
}
|
|
|
|
public function testXmlMappingsAreRead(): void
|
|
{
|
|
$xml = '<doctrine-mapping><entity name="App\Entity\Book" table="books" repository-class="App\Repository\BookRepository">'
|
|
. '<id name="id" type="integer"><generator strategy="AUTO"/></id><field name="title" type="string" length="120" nullable="true"/>'
|
|
. '<many-to-one field="shelf" target-entity="App\Entity\Shelf" inversed-by="books"><join-column name="shelf_id" nullable="false"/></many-to-one>'
|
|
. '</entity></doctrine-mapping>';
|
|
$book = MappingReader::readXml($xml, 'config/doctrine/Book.orm.xml')[0];
|
|
self::assertSame(['Book', 'books', 'App\Repository\BookRepository'], [$book['name'], $book['table'], $book['repository']]);
|
|
self::assertSame(['id', 'title'], array_column($book['fields'], 'name'));
|
|
self::assertSame([120, true], [$book['fields'][1]['length'], $book['fields'][1]['nullable']]);
|
|
self::assertSame(['ManyToOne', 'App\Entity\Shelf', 'shelf_id', false], [$book['relations'][0]['kind'], $book['relations'][0]['target'], $book['relations'][0]['joinColumns'][0]['name'], $book['relations'][0]['nullable']]);
|
|
}
|
|
|
|
public function testANewEntityAndItsRepositoryAreWrittenLikeMakerDoes(): void
|
|
{
|
|
$files = EntityWriter::newEntity(self::ROOT, ['name' => 'Category', 'table' => 'categories', 'fields' => [['name' => 'name', 'type' => 'string', 'length' => 100]]]);
|
|
self::assertSame(['src/Entity/Category.php', 'src/Repository/CategoryRepository.php'], array_keys($files));
|
|
$this->assertSameAsExpected('Category.php', $files['src/Entity/Category.php']);
|
|
$this->assertSameAsExpected('CategoryRepository.php', $files['src/Repository/CategoryRepository.php']);
|
|
}
|
|
|
|
public function testADefaultValueIsWrittenAsAColumnOption(): void
|
|
{
|
|
$code = EntityWriter::newEntity(self::ROOT, ['name' => 'Item', 'fields' => [
|
|
['name' => 'qty', 'type' => 'integer', 'default' => '3'], ['name' => 'label', 'type' => 'string', 'default' => "it's"], ['name' => 'on', 'type' => 'boolean', 'default' => 'true'],
|
|
]])['src/Entity/Item.php'];
|
|
self::assertStringContainsString("options: ['default' => 3]", $code);
|
|
self::assertStringContainsString("options: ['default' => 'it\\'s']", $code);
|
|
self::assertStringContainsString("options: ['default' => true]", $code);
|
|
}
|
|
|
|
public function testAFieldIsAddedWithItsAccessorsAndImports(): void
|
|
{
|
|
$before = self::source('Tag');
|
|
$after = EntityWriter::addField($before, ['name' => 'publishedAt', 'type' => 'date_immutable', 'nullable' => true]);
|
|
$this->assertSameAsExpected('Tag-field.php', $after);
|
|
$this->assertOnlyInsertions($before, $after);
|
|
$enum = EntityWriter::addField($before, ['name' => 'state', 'type' => 'string', 'enumType' => 'App\Enum\Status']);
|
|
self::assertStringContainsString("use App\\Enum\\Status;\n", $enum);
|
|
self::assertStringContainsString(" #[ORM\\Column(enumType: Status::class)]\n private ?Status \$state = null;\n", $enum);
|
|
self::assertStringContainsString('public function isActive(): ?bool', EntityWriter::addField($before, ['name' => 'active', 'type' => 'boolean']));
|
|
self::assertStringContainsString(" #[ORM\\Column]\n private array \$scores = [];", EntityWriter::addField($before, ['name' => 'scores', 'type' => 'json']));
|
|
}
|
|
|
|
public function testAManyToOneWritesBothSidesKeptInStep(): void
|
|
{
|
|
$category = EntityWriter::newEntity(self::ROOT, ['name' => 'Category'])['src/Entity/Category.php'];
|
|
$before = self::source('Post');
|
|
$r = EntityWriter::addRelation($before, $category, ['kind' => 'ManyToOne', 'property' => 'category', 'target' => 'App\Entity\Category', 'inverse' => 'posts', 'nullable' => false]);
|
|
$this->assertSameAsExpected('Post-category.php', $r['owner']);
|
|
$this->assertSameAsExpected('Category-posts.php', (string)$r['target']);
|
|
$this->assertOnlyInsertions($before, $r['owner']);
|
|
// the same relation written from the OneToMany side
|
|
$r2 = EntityWriter::addRelation($category, $before, ['kind' => 'OneToMany', 'property' => 'posts', 'target' => 'App\Entity\Post', 'inverse' => 'category', 'nullable' => false]);
|
|
self::assertSame($r['owner'], $r2['target']);
|
|
}
|
|
|
|
public function testAManyToManyAndAOneToOneWriteTheirInverseSides(): void
|
|
{
|
|
$r = EntityWriter::addRelation(self::source('Profile'), self::source('Tag'), ['kind' => 'ManyToMany', 'property' => 'tags', 'target' => 'App\Entity\Tag', 'inverse' => 'profiles']);
|
|
$this->assertSameAsExpected('Profile-tags.php', $r['owner']);
|
|
$this->assertSameAsExpected('Tag-profiles.php', (string)$r['target']);
|
|
$o = EntityWriter::addRelation(self::source('Tag'), self::source('Profile'), ['kind' => 'OneToOne', 'property' => 'owner', 'target' => 'App\Entity\Profile', 'inverse' => 'tag']);
|
|
self::assertStringContainsString("#[ORM\\OneToOne(inversedBy: 'tag')]\n private ?Profile \$owner = null;", $o['owner']);
|
|
$this->assertSameAsExpected('Profile-tag.php', (string)$o['target']);
|
|
// one-sided: nothing written on the other side
|
|
self::assertNull(EntityWriter::addRelation(self::source('Comment'), self::source('Tag'), ['kind' => 'ManyToOne', 'property' => 'tag', 'target' => 'App\Entity\Tag'])['target']);
|
|
}
|
|
|
|
public function testAFieldIsChangedInPlaceAndAPropertyRemovedWithItsMethods(): void
|
|
{
|
|
$before = self::source('Post');
|
|
$updated = EntityWriter::updateField($before, 'title', ['type' => 'text', 'nullable' => true]);
|
|
self::assertStringContainsString(" #[ORM\\Column(type: Types::TEXT, nullable: true)]\n private ?string \$title = null;\n", $updated);
|
|
self::assertStringContainsString('public function setTitle(?string $title): static', $updated);
|
|
self::assertSame(count(explode("\n", $before)), count(explode("\n", $updated)));
|
|
$removed = EntityWriter::removeProperty($before, 'tags');
|
|
self::assertStringNotContainsString('$tags', $removed);
|
|
self::assertStringNotContainsString('post_tags', $removed);
|
|
$removed = EntityWriter::removeProperty($updated, 'title');
|
|
self::assertStringNotContainsString('itle', $removed);
|
|
self::assertStringContainsString(" private ?int \$id = null;\n\n #[ORM\\Column(type: Types::DECIMAL", $removed);
|
|
}
|
|
|
|
public function testDtosAreMadeFromChosenFields(): void
|
|
{
|
|
$post = self::entities()['Post'];
|
|
$this->assertSameAsExpected('PostOutput.php', EntityWriter::dto(self::ROOT, $post, ['id', 'title', 'price', 'status', 'author'], 'output')['src/Dto/PostOutput.php']);
|
|
$this->assertSameAsExpected('PostInput.php', EntityWriter::dto(self::ROOT, $post, ['id', 'title', 'price', 'status'], 'input')['src/Dto/PostInput.php']);
|
|
}
|
|
|
|
public function testARenameChangesTheEntityTheOtherSideDqlAndTwig(): void
|
|
{
|
|
$reader = new MappingReader(self::ROOT);
|
|
$plan = Rename::plan(self::ROOT, $reader->read(), 'App\Entity\Post', 'title', 'headline', $reader->sourceFiles());
|
|
$changes = array_column($plan['changes'], 'after', 'path');
|
|
self::assertSame(['src/Entity/Post.php', 'src/Repository/PostRepository.php', 'templates/post/show.html.twig'], array_keys($changes));
|
|
self::assertStringContainsString("private ?string \$headline = null;", $changes['src/Entity/Post.php']);
|
|
self::assertStringContainsString("public function setHeadline(string \$headline): static\n {\n \$this->headline = \$headline;", $changes['src/Entity/Post.php']);
|
|
self::assertStringContainsString("->orderBy('p.headline', 'ASC')", $changes['src/Repository/PostRepository.php']);
|
|
self::assertStringContainsString('p.headline LIKE :t', $changes['src/Repository/PostRepository.php']);
|
|
self::assertSame("<h1>{{ post.headline }}</h1>\n<p>{{ post.author.email }}</p>\n", $changes['templates/post/show.html.twig']);
|
|
|
|
$plan = Rename::plan(self::ROOT, $reader->read(), 'App\Entity\Post', 'author', 'writer', $reader->sourceFiles());
|
|
$changes = array_column($plan['changes'], 'after', 'path');
|
|
self::assertStringContainsString("#[ORM\\OneToMany(targetEntity: Post::class, mappedBy: 'writer')]", $changes['src/Entity/User.php']);
|
|
self::assertStringContainsString('JOIN p.writer a WHERE a.email', $changes['src/Repository/PostRepository.php']);
|
|
self::assertSame("<h1>{{ post.title }}</h1>\n<p>{{ post.writer.email }}</p>\n", $changes['templates/post/show.html.twig']);
|
|
// a DQL alias of another entity keeps its field
|
|
self::assertStringContainsString('a.email = :email', $changes['src/Repository/PostRepository.php']);
|
|
}
|
|
|
|
public function testDqlCompletionKnowsWhereTheCursorIs(): void
|
|
{
|
|
$dql = new Dql(array_values(self::entities()));
|
|
$code = (string)file_get_contents(self::ROOT . '/src/Repository/PostRepository.php');
|
|
$at = fn (string $needle, int $back = 0) => $dql->complete($code, strpos($code, $needle) + strlen($needle) - $back);
|
|
// createQueryBuilder('p') of PostRepository: p is a Post
|
|
$r = $at("andWhere('p.st", 0);
|
|
self::assertSame(['field', 'st'], [$r['context'], $r['prefix']]);
|
|
self::assertContains('status', array_column($r['items'], 'label'));
|
|
self::assertContains('author', array_column($r['items'], 'label'));
|
|
// JOIN p.author a: a is a User
|
|
$r = $at('JOIN p.author a WHERE a.');
|
|
self::assertContains('email', array_column($r['items'], 'label'));
|
|
self::assertContains('address', array_column($r['items'], 'label'));
|
|
// after JOIN alias.: relations only
|
|
$r = $at('JOIN p.');
|
|
self::assertSame(['join', ['author', 'tags', 'comments']], [$r['context'], array_column($r['items'], 'label')]);
|
|
// FROM: the entities
|
|
$r = $at('FROM App');
|
|
self::assertSame('entity', $r['context']);
|
|
self::assertContains('App\Entity\Post', array_column($r['items'], 'insert'));
|
|
// elsewhere: aliases, functions, keywords
|
|
$r = $at('AND ');
|
|
self::assertSame(['p', 'a'], array_slice(array_column($r['items'], 'label'), 0, 2));
|
|
self::assertContains('CONCAT()', array_column($r['items'], 'label'));
|
|
// not in a DQL string
|
|
self::assertNull($at('function findPublished'));
|
|
// embedded fields: alias.embedded.
|
|
$q = "<?php \$em->createQuery('SELECT u FROM App\\Entity\\User u WHERE u.address.')";
|
|
self::assertSame(['street', 'city'], array_column($dql->complete($q, strlen($q) - 2)['items'], 'label'));
|
|
// QueryBuilder: from(X::class, 'x') and join('x.rel', 'y') declare aliases
|
|
$q = "<?php\nuse App\\Entity\\Comment;\n\$qb->select('c')->from(Comment::class, 'c')->join('c.post', 'po')->where('po.')";
|
|
self::assertContains('title', array_column($dql->complete($q, strlen($q) - 2)['items'], 'label'));
|
|
}
|
|
|
|
public function testTheMappingIsComparedWithTheRealTable(): void
|
|
{
|
|
$e = self::entities();
|
|
$table = ['name' => 'post', 'columns' => [
|
|
['name' => 'id', 'type' => 'int(11)', 'nullable' => false, 'key' => 'PRI'],
|
|
['name' => 'title', 'type' => 'varchar(200)', 'nullable' => false, 'key' => ''],
|
|
['name' => 'price', 'type' => 'decimal(10,2)', 'nullable' => true, 'key' => ''],
|
|
['name' => 'status', 'type' => 'int(11)', 'nullable' => false, 'key' => ''],
|
|
['name' => 'legacy', 'type' => 'text', 'nullable' => true, 'key' => ''],
|
|
]];
|
|
$r = MappingCompare::compare($e['Post'], array_values($e), $table);
|
|
$rows = array_column($r['rows'], null, 'column');
|
|
self::assertSame(['ok', ['length'], 'ok', ['type'], 'missing', 'extra'],
|
|
[$rows['id']['status'], $rows['title']['detail'], $rows['price']['status'], $rows['status']['detail'], $rows['author_id']['status'], $rows['legacy']['status']]);
|
|
self::assertSame(['ok' => 2, 'missing' => 1, 'extra' => 1, 'different' => 2], $r['summary']);
|
|
// embedded columns with their prefix, inherited ones, join columns; the discriminator and the subclasses' columns of a single table
|
|
$user = array_keys(MappingCompare::expected($e['User'], array_values($e)));
|
|
self::assertSame(['id', 'email', 'roles', 'created_at', 'home_street', 'home_city', 'profile_id'], $user);
|
|
self::assertSame(['id', 'headline', 'kind', 'text', 'seconds'], array_keys(MappingCompare::expected($e['Content'], array_values($e))));
|
|
self::assertSame(['salary', 'id'], array_keys(MappingCompare::expected($e['Employee'], array_values($e))));
|
|
self::assertFalse(MappingCompare::compare($e['Tag'], array_values($e), null)['exists']);
|
|
}
|
|
|
|
public function testMigrationsOutputIsReadAsAHistory(): void
|
|
{
|
|
$output = <<<'TXT'
|
|
+------------------------------------------+--------------+---------------------+----------------+-------------+
|
|
| Migration Versions | |
|
|
+------------------------------------------+--------------+---------------------+----------------+-------------+
|
|
| Migration | Status | Migrated At | Execution Time | Description |
|
|
+------------------------------------------+--------------+---------------------+----------------+-------------+
|
|
| DoctrineMigrations\Version20261101120000 | migrated | 2026-11-01 12:00:05 | 0.051s | Users |
|
|
| DoctrineMigrations\Version20261105090000 | migrated, not available | 2026-11-05 09:00:00 | 0.01s | |
|
|
| DoctrineMigrations\Version20261110100000 | not migrated | | | Posts |
|
|
+------------------------------------------+--------------+---------------------+----------------+-------------+
|
|
TXT;
|
|
$r = Migrations::parseList($output);
|
|
self::assertSame(['executed', 'unavailable', 'pending'], array_column($r['migrations'], 'status'));
|
|
self::assertSame(['2026-11-01 12:00:05', '0.051s', 'Users'], [$r['migrations'][0]['migratedAt'], $r['migrations'][0]['time'], $r['migrations'][0]['description']]);
|
|
self::assertSame('Version20261110100000', $r['migrations'][2]['short']);
|
|
self::assertSame(['executed' => 1, 'pending' => 1, 'unavailable' => 1, 'current' => 'DoctrineMigrations\Version20261105090000', 'latest' => 'DoctrineMigrations\Version20261110100000'], $r['summary']);
|
|
self::assertSame('migrations/Version20261116101010.php', Migrations::generated(" Success!\n Next: Review the new migration \"/app/migrations/Version20261116101010.php\"", '/home/me/site'));
|
|
self::assertSame('migrations/Version20261116101010.php', Migrations::generated('Generated new migration class to "/app/migrations/Version20261116101010.php"', '/x'));
|
|
self::assertSame(['CREATE TABLE tag (id INT AUTO_INCREMENT NOT NULL, PRIMARY KEY(id))', 'ALTER TABLE post ADD tag_id INT DEFAULT NULL'],
|
|
Migrations::statements("\n CREATE TABLE tag (id INT AUTO_INCREMENT NOT NULL, PRIMARY KEY(id));\n\n ALTER TABLE post ADD tag_id INT DEFAULT NULL;\n\n [OK] 2 queries"));
|
|
self::assertSame(['doctrine:migrations:migrate', 'prev', '--allow-no-migration'], Migrations::argv(['action' => 'migrate', 'version' => 'prev']));
|
|
self::assertSame(['doctrine:migrations:execute', 'DoctrineMigrations\Version1', '--down'], Migrations::argv(['action' => 'execute', 'version' => 'DoctrineMigrations\Version1', 'direction' => 'down']));
|
|
self::assertSame(['make:migration'], Migrations::argv(['action' => 'diff'], true));
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
Migrations::argv(['action' => 'migrate', 'version' => 'x; rm -rf /']);
|
|
}
|
|
|
|
public function testRealConsoleOutputsOfASymfonyProjectAreRead(): void
|
|
{
|
|
$dir = __DIR__ . '/fixtures/doctrine-console';
|
|
$r = Migrations::parseList((string)file_get_contents("$dir/list-migrated.txt"));
|
|
self::assertSame([['DoctrineMigrations\Version20261115230317', 'executed', '2026-11-15 23:03:18', '0.002s']],
|
|
array_map(fn ($m) => [$m['version'], $m['status'], $m['migratedAt'], $m['time']], $r['migrations']));
|
|
self::assertSame('pending', Migrations::parseList((string)file_get_contents("$dir/list-pending.txt"))['migrations'][0]['status']);
|
|
// make:migration writes its path as a terminal hyperlink
|
|
self::assertSame('migrations/Version20261115230317.php', Migrations::generated((string)file_get_contents("$dir/make-migration.txt"), '/x'));
|
|
$sql = Migrations::statements((string)file_get_contents("$dir/dump-sql.txt"));
|
|
self::assertContains('CREATE UNIQUE INDEX UNIQ_8D93D649E7927C74 ON "user" (email)', $sql);
|
|
self::assertGreaterThan(8, count($sql));
|
|
}
|
|
|
|
public function testInflection(): void
|
|
{
|
|
self::assertSame(['post', 'category', 'address', 'box', 'person', 'status'], array_map([Inflector::class, 'singular'], ['posts', 'categories', 'addresses', 'boxes', 'people', 'status']));
|
|
self::assertSame(['posts', 'categories', 'boxes'], array_map([Inflector::class, 'plural'], ['post', 'category', 'box']));
|
|
}
|
|
}
|