|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\Core\Tests\Unit\Domain\Subscription\MessageHandler; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Exception\TableExistsException; |
| 8 | +use Doctrine\DBAL\Schema\AbstractSchemaManager; |
| 9 | +use Doctrine\DBAL\Schema\Table; |
| 10 | +use InvalidArgumentException; |
| 11 | +use PhpList\Core\Domain\Subscription\Message\DynamicTableMessage; |
| 12 | +use PhpList\Core\Domain\Subscription\MessageHandler\DynamicTableMessageHandler; |
| 13 | +use PHPUnit\Framework\MockObject\MockObject; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use PhpList\Core\Tests\Support\DBAL\FakeDriverException; |
| 16 | + |
| 17 | +class DynamicTableMessageHandlerTest extends TestCase |
| 18 | +{ |
| 19 | + private AbstractSchemaManager&MockObject $schemaManager; |
| 20 | + |
| 21 | + protected function setUp(): void |
| 22 | + { |
| 23 | + $this->schemaManager = $this->createMock(AbstractSchemaManager::class); |
| 24 | + } |
| 25 | + |
| 26 | + public function testInvokeCreatesTableWhenNotExists(): void |
| 27 | + { |
| 28 | + $tableName = 'phplist_listattr_sizes'; |
| 29 | + $message = new DynamicTableMessage($tableName); |
| 30 | + |
| 31 | + $capturedTable = null; |
| 32 | + |
| 33 | + $this->schemaManager |
| 34 | + ->expects($this->once()) |
| 35 | + ->method('tablesExist') |
| 36 | + ->with([$tableName]) |
| 37 | + ->willReturn(false); |
| 38 | + |
| 39 | + $this->schemaManager |
| 40 | + ->expects($this->once()) |
| 41 | + ->method('createTable') |
| 42 | + ->with($this->callback(function (Table $table) use (&$capturedTable, $tableName) { |
| 43 | + $capturedTable = $table; |
| 44 | + // Basic checks |
| 45 | + $this->assertSame($tableName, $table->getName()); |
| 46 | + $this->assertTrue($table->hasColumn('id')); |
| 47 | + $this->assertTrue($table->hasColumn('name')); |
| 48 | + $this->assertTrue($table->hasColumn('listorder')); |
| 49 | + |
| 50 | + // id column |
| 51 | + $idCol = $table->getColumn('id'); |
| 52 | + $this->assertSame('integer', $idCol->getType()->getName()); |
| 53 | + $this->assertTrue($idCol->getAutoincrement()); |
| 54 | + $this->assertTrue($idCol->getNotnull()); |
| 55 | + |
| 56 | + // name column |
| 57 | + $nameCol = $table->getColumn('name'); |
| 58 | + $this->assertSame('string', $nameCol->getType()->getName()); |
| 59 | + $this->assertSame(255, $nameCol->getLength()); |
| 60 | + $this->assertFalse($nameCol->getNotnull()); |
| 61 | + |
| 62 | + // listorder column |
| 63 | + $orderCol = $table->getColumn('listorder'); |
| 64 | + $this->assertSame('integer', $orderCol->getType()->getName()); |
| 65 | + $this->assertFalse($orderCol->getNotnull()); |
| 66 | + $this->assertSame(0, $orderCol->getDefault()); |
| 67 | + |
| 68 | + // Primary key |
| 69 | + $this->assertSame(['id'], $table->getPrimaryKey()?->getColumns()); |
| 70 | + |
| 71 | + // Unique index on name |
| 72 | + $indexName = 'uniq_' . $tableName . '_name'; |
| 73 | + $this->assertTrue($table->hasIndex($indexName)); |
| 74 | + $idx = $table->getIndex($indexName); |
| 75 | + $this->assertTrue($idx->isUnique()); |
| 76 | + $this->assertSame(['name'], $idx->getColumns()); |
| 77 | + |
| 78 | + return true; |
| 79 | + })) |
| 80 | + ->willReturnCallback(function (Table $table) { |
| 81 | + // no-op; we just want the assertions in the callback |
| 82 | + }); |
| 83 | + |
| 84 | + $handler = new DynamicTableMessageHandler($this->schemaManager); |
| 85 | + $handler($message); |
| 86 | + |
| 87 | + $this->assertInstanceOf(Table::class, $capturedTable); |
| 88 | + } |
| 89 | + |
| 90 | + public function testInvokeDoesNothingWhenTableAlreadyExists(): void |
| 91 | + { |
| 92 | + $tableName = 'phplist_listattr_sizes'; |
| 93 | + $message = new DynamicTableMessage($tableName); |
| 94 | + |
| 95 | + $this->schemaManager |
| 96 | + ->expects($this->once()) |
| 97 | + ->method('tablesExist') |
| 98 | + ->with([$tableName]) |
| 99 | + ->willReturn(true); |
| 100 | + |
| 101 | + $this->schemaManager |
| 102 | + ->expects($this->never()) |
| 103 | + ->method('createTable'); |
| 104 | + |
| 105 | + $handler = new DynamicTableMessageHandler($this->schemaManager); |
| 106 | + $handler($message); |
| 107 | + // reached without creating a table |
| 108 | + $this->assertTrue(true); |
| 109 | + } |
| 110 | + |
| 111 | + public function testInvokeThrowsForInvalidTableName(): void |
| 112 | + { |
| 113 | + $invalidName = 'invalid-name!'; |
| 114 | + $message = new DynamicTableMessage($invalidName); |
| 115 | + |
| 116 | + // tablesExist is consulted before validating the name |
| 117 | + $this->schemaManager |
| 118 | + ->expects($this->once()) |
| 119 | + ->method('tablesExist') |
| 120 | + ->with([$invalidName]) |
| 121 | + ->willReturn(false); |
| 122 | + |
| 123 | + $handler = new DynamicTableMessageHandler($this->schemaManager); |
| 124 | + |
| 125 | + $this->expectException(InvalidArgumentException::class); |
| 126 | + $this->expectExceptionMessage('Invalid list table name'); |
| 127 | + $handler($message); |
| 128 | + } |
| 129 | + |
| 130 | + public function testInvokeSwallowsTableExistsRace(): void |
| 131 | + { |
| 132 | + $tableName = 'phplist_listattr_colors'; |
| 133 | + $message = new DynamicTableMessage($tableName); |
| 134 | + |
| 135 | + $this->schemaManager |
| 136 | + ->expects($this->once()) |
| 137 | + ->method('tablesExist') |
| 138 | + ->with([$tableName]) |
| 139 | + ->willReturn(false); |
| 140 | + |
| 141 | + $this->schemaManager |
| 142 | + ->expects($this->once()) |
| 143 | + ->method('createTable') |
| 144 | + ->willThrowException(new TableExistsException( |
| 145 | + new FakeDriverException('already exists', '42P07'), |
| 146 | + null |
| 147 | + )); |
| 148 | + |
| 149 | + $handler = new DynamicTableMessageHandler($this->schemaManager); |
| 150 | + |
| 151 | + // Should not throw despite the TableExistsException |
| 152 | + $handler($message); |
| 153 | + $this->assertTrue(true); |
| 154 | + } |
| 155 | +} |
0 commit comments