diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 8def121..2f5264a 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,5 @@ - + @@ -12,6 +12,12 @@ + + + + + + @@ -65,6 +71,12 @@ + + + + + + schemas]]> @@ -88,10 +100,54 @@ config['table'] ?? 'migrations']]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -116,6 +172,12 @@ capsule->getDatabase()]]> + + + + + + @@ -199,15 +261,26 @@ + + + + + getTable()]]> + + + getTable()]]> + + + @@ -230,6 +303,9 @@ getTable()]]> name]]> + + + @@ -237,6 +313,9 @@ name]]> newName]]> + + + @@ -255,6 +334,9 @@ $this->getOption('update', ForeignKeyInterface::NO_ACTION), )]]> + + + getOption('delete', ForeignKeyInterface::NO_ACTION)]]> @@ -295,6 +377,9 @@ $this->getOption('update', ForeignKeyInterface::NO_ACTION), )]]> + + + getOption('delete', ForeignKeyInterface::NO_ACTION)]]> @@ -320,6 +405,9 @@ getTable()]]> + + + @@ -330,6 +418,9 @@ getTable()]]> + + + getOption('name')]]> getOption('unique', false)]]> @@ -342,6 +433,9 @@ getTable()]]> + + + getOption('unique', false)]]> @@ -353,6 +447,9 @@ getTable()]]> + + + columns]]> @@ -361,25 +458,43 @@ getTable()]]> + + + + + database]]> + getTable()]]> + + + + + database]]> + getTable()]]> + + + + + database]]> + @@ -387,17 +502,29 @@ newName]]> newName]]> + + + + + database]]> + getTable()]]> + + + + + database]]> + diff --git a/src/Atomizer/Renderer.php b/src/Atomizer/Renderer.php index 2d4cd32..14acff0 100644 --- a/src/Atomizer/Renderer.php +++ b/src/Atomizer/Renderer.php @@ -256,6 +256,12 @@ private function columnOptions(AbstractColumn $column): array if ($attribute === 'size' && $value === 0) { continue; } + if ($attribute === 'after' && $value === '') { + continue; + } + if ($attribute === 'first' && $value === false) { + continue; + } $options[$attribute] = $value; } diff --git a/tests/Migrations/MySQL/RendererTest.php b/tests/Migrations/MySQL/RendererTest.php new file mode 100644 index 0000000..68a40da --- /dev/null +++ b/tests/Migrations/MySQL/RendererTest.php @@ -0,0 +1,21 @@ +createMock(AbstractColumn::class); + $column->method('isNullable')->willReturn(true); + $column->method('getDefaultValue')->willReturn('one'); + $column->method('getAttributes')->willReturn([]); + $column->method('getAbstractType')->willReturn('enum'); + $column->method('getEnumValues')->willReturn(['one', 'two', 'three']); + + $renderer = new Renderer(); + + $method = new \ReflectionMethod($renderer, 'columnOptions'); + $method->setAccessible(true); + + $options = $method->invoke($renderer, $column); + + $this->assertArrayHasKey('nullable', $options); + $this->assertArrayHasKey('defaultValue', $options); + $this->assertArrayHasKey('values', $options); + $this->assertTrue($options['nullable']); + $this->assertSame('one', $options['defaultValue']); + $this->assertSame(['one', 'two', 'three'], $options['values']); + } + + public function testColumnOptionsWithDateTimeNow(): void + { + $column = $this->createMock(AbstractColumn::class); + $column->method('isNullable')->willReturn(false); + $column->method('getDefaultValue')->willReturn(AbstractColumn::DATETIME_NOW); + $column->method('getAttributes')->willReturn([]); + $column->method('getAbstractType')->willReturn('datetime'); + + $renderer = new Renderer(); + + $method = new \ReflectionMethod($renderer, 'columnOptions'); + $method->setAccessible(true); + + $options = $method->invoke($renderer, $column); + + $this->assertArrayHasKey('nullable', $options); + $this->assertArrayHasKey('defaultValue', $options); + $this->assertFalse($options['nullable']); + $this->assertSame(AbstractColumn::DATETIME_NOW, $options['defaultValue']); + } + + public function testColumnOptionsSizeNotZeroIsAccepted(): void + { + $column = $this->createMock(AbstractColumn::class); + $column->method('isNullable')->willReturn(true); + $column->method('getDefaultValue')->willReturn(null); + $column->method('getAttributes')->willReturn(['size' => 11]); + $column->method('getAbstractType')->willReturn('integer'); + + $renderer = new Renderer(); + + $method = new \ReflectionMethod($renderer, 'columnOptions'); + $method->setAccessible(true); + + $options = $method->invoke($renderer, $column); + + $this->assertArrayHasKey('nullable', $options); + $this->assertArrayHasKey('defaultValue', $options); + $this->assertArrayHasKey('size', $options); + $this->assertTrue($options['nullable']); + $this->assertNull($options['defaultValue']); + $this->assertSame(11, $options['size']); + } + + public function testColumnOptionsSizeZeroIsIgnored(): void + { + $column = $this->createMock(AbstractColumn::class); + $column->method('isNullable')->willReturn(true); + $column->method('getDefaultValue')->willReturn(null); + $column->method('getAttributes')->willReturn(['size' => 0]); + $column->method('getAbstractType')->willReturn('integer'); + + $renderer = new Renderer(); + + $method = new \ReflectionMethod($renderer, 'columnOptions'); + $method->setAccessible(true); + + $options = $method->invoke($renderer, $column); + + $this->assertArrayHasKey('nullable', $options); + $this->assertArrayHasKey('defaultValue', $options); + $this->assertArrayNotHasKey('size', $options); + $this->assertTrue($options['nullable']); + $this->assertNull($options['defaultValue']); + } + + public function testColumnOptionsAfterNotEmptyStringIsAccepted(): void + { + $column = $this->createMock(AbstractColumn::class); + $column->method('isNullable')->willReturn(true); + $column->method('getDefaultValue')->willReturn(null); + $column->method('getAttributes')->willReturn(['after' => 'email']); + $column->method('getAbstractType')->willReturn('integer'); + + $renderer = new Renderer(); + + $method = new \ReflectionMethod($renderer, 'columnOptions'); + $method->setAccessible(true); + + $options = $method->invoke($renderer, $column); + + $this->assertArrayHasKey('nullable', $options); + $this->assertArrayHasKey('defaultValue', $options); + $this->assertArrayHasKey('after', $options); + $this->assertTrue($options['nullable']); + $this->assertNull($options['defaultValue']); + $this->assertSame('email', $options['after']); + } + + public function testColumnOptionsAfterEmptyStringIsIgnored(): void + { + $column = $this->createMock(AbstractColumn::class); + $column->method('isNullable')->willReturn(true); + $column->method('getDefaultValue')->willReturn(null); + $column->method('getAttributes')->willReturn(['after' => '']); + $column->method('getAbstractType')->willReturn('integer'); + + $renderer = new Renderer(); + + $method = new \ReflectionMethod($renderer, 'columnOptions'); + $method->setAccessible(true); + + $options = $method->invoke($renderer, $column); + + $this->assertArrayHasKey('nullable', $options); + $this->assertArrayHasKey('defaultValue', $options); + $this->assertArrayNotHasKey('after', $options); + $this->assertTrue($options['nullable']); + $this->assertNull($options['defaultValue']); + } + + public function testColumnOptionsFirstNotFalseIsAccepted(): void + { + $column = $this->createMock(AbstractColumn::class); + $column->method('isNullable')->willReturn(true); + $column->method('getDefaultValue')->willReturn(null); + $column->method('getAttributes')->willReturn(['first' => true]); + $column->method('getAbstractType')->willReturn('integer'); + + $renderer = new Renderer(); + + $method = new \ReflectionMethod($renderer, 'columnOptions'); + $method->setAccessible(true); + + $options = $method->invoke($renderer, $column); + + $this->assertArrayHasKey('nullable', $options); + $this->assertArrayHasKey('defaultValue', $options); + $this->assertArrayHasKey('first', $options); + $this->assertTrue($options['nullable']); + $this->assertNull($options['defaultValue']); + $this->assertTrue($options['first']); + } + + public function testColumnOptionsFirstFalseIsIgnored(): void + { + $column = $this->createMock(AbstractColumn::class); + $column->method('isNullable')->willReturn(true); + $column->method('getDefaultValue')->willReturn(null); + $column->method('getAttributes')->willReturn(['first' => false]); + $column->method('getAbstractType')->willReturn('integer'); + + $renderer = new Renderer(); + + $method = new \ReflectionMethod($renderer, 'columnOptions'); + $method->setAccessible(true); + + $options = $method->invoke($renderer, $column); + + $this->assertArrayHasKey('nullable', $options); + $this->assertArrayHasKey('defaultValue', $options); + $this->assertArrayNotHasKey('first', $options); + $this->assertTrue($options['nullable']); + $this->assertNull($options['defaultValue']); + } +} diff --git a/tests/Migrations/SQLServer/RendererTest.php b/tests/Migrations/SQLServer/RendererTest.php new file mode 100644 index 0000000..6a61cb5 --- /dev/null +++ b/tests/Migrations/SQLServer/RendererTest.php @@ -0,0 +1,21 @@ +