| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace Test\PhpDevCommunity\PaperORM;
- use PhpDevCommunity\PaperORM\EntityManager;
- use PhpDevCommunity\PaperORM\Mapping\Column\BoolColumn;
- use PhpDevCommunity\PaperORM\Mapping\Column\IntColumn;
- use PhpDevCommunity\PaperORM\Mapping\Column\PrimaryKeyColumn;
- use PhpDevCommunity\PaperORM\Mapping\Column\StringColumn;
- use PhpDevCommunity\PaperORM\Metadata\ColumnMetadata;
- use PhpDevCommunity\UniTester\TestCase;
- class PlatformTest extends TestCase
- {
- private EntityManager $em;
- protected function setUp(): void
- {
- $this->em = new EntityManager([
- 'driver' => 'sqlite',
- 'user' => null,
- 'password' => null,
- 'memory' => true,
- ]);
- }
- protected function tearDown(): void
- {
- $this->em->getConnection()->close();
- }
- protected function execute(): void
- {
- $this->testCreateTables();
- $this->testDropTable();
- $this->testDropColumn();
- $this->testAddColumn();
- $this->testRenameColumn();
- }
- public function testCreateTables()
- {
- $this->em->getConnection()->close();
- $this->em->getConnection()->connect();
- $platform = $this->em->createDatabasePlatform();
- $platform->createTable('user', [
- new PrimaryKeyColumn('id'),
- new StringColumn('firstname'),
- new StringColumn('lastname'),
- new StringColumn('email'),
- new StringColumn('password'),
- new BoolColumn('is_active'),
- ]);
- $platform->createTable('post', [
- new PrimaryKeyColumn('id'),
- new IntColumn('user_id'),
- new StringColumn('title'),
- new StringColumn('content'),
- ], [
- 'FOREIGN KEY (user_id) REFERENCES user (id)'
- ]);
- $this->assertStrictEquals(2, count($platform->listTables()));
- $this->assertEquals(['user', 'post'], $platform->listTables());
- }
- public function testDropTable()
- {
- $this->em->getConnection()->close();
- $this->em->getConnection()->connect();
- $platform = $this->em->createDatabasePlatform();
- $platform->createTable('user', [
- new PrimaryKeyColumn('id'),
- new StringColumn('firstname'),
- new StringColumn('lastname'),
- new StringColumn('email'),
- new StringColumn('password'),
- new BoolColumn('is_active'),
- ]);
- $this->assertStrictEquals(1, count($platform->listTables()));
- $platform->dropTable('user');
- $this->assertStrictEquals(0, count($platform->listTables()));
- }
- public function testDropColumn()
- {
- if (\SQLite3::version()['versionString'] < '3.35.0') {
- return;
- }
- $this->em->getConnection()->close();
- $this->em->getConnection()->connect();
- $platform = $this->em->createDatabasePlatform();
- $platform->createTable('user', [
- new PrimaryKeyColumn('id'),
- new StringColumn('firstname'),
- new StringColumn('lastname'),
- new StringColumn('email'),
- new StringColumn('password'),
- new BoolColumn('is_active'),
- ]);
- $this->assertStrictEquals(6, count($platform->listTableColumns('user')));
- $platform->dropColumn('user', 'lastname');
- $this->assertStrictEquals(5, count($platform->listTableColumns('user')));
- }
- public function testAddColumn()
- {
- $this->em->getConnection()->close();
- $this->em->getConnection()->connect();
- $platform = $this->em->createDatabasePlatform();
- $platform->createTable('user', [
- new PrimaryKeyColumn('id'),
- new StringColumn('firstname'),
- new StringColumn('lastname'),
- new StringColumn('email'),
- new StringColumn('password'),
- new BoolColumn('is_active'),
- ]);
- $this->assertStrictEquals(6, count($platform->listTableColumns('user')));
- $platform->addColumn('user', new StringColumn('username'));
- $this->assertStrictEquals(7, count($platform->listTableColumns('user')));
- }
- public function testRenameColumn()
- {
- $this->em->getConnection()->close();
- $this->em->getConnection()->connect();
- $platform = $this->em->createDatabasePlatform();
- $platform->createTable('user', [
- new PrimaryKeyColumn('id'),
- new StringColumn('firstname'),
- new StringColumn('lastname'),
- new StringColumn('email'),
- new StringColumn('password'),
- new BoolColumn('is_active'),
- ]);
- $columnsAsArray = array_map(function (ColumnMetadata $column) {
- return $column->toArray();
- }, $platform->listTableColumns('user'));
- $columns = array_column($columnsAsArray, 'name');
- $this->assertTrue(in_array('firstname', $columns));
- $platform->renameColumn('user', 'firstname', 'prenom');
- $columnsAsArray = array_map(function (ColumnMetadata $column) {
- return $column->toArray();
- }, $platform->listTableColumns('user'));
- $columns = array_column($columnsAsArray, 'name');
- $this->assertTrue(!in_array('firstname', $columns));
- $this->assertTrue(in_array('prenom', $columns));
- }
- }
|