Skip to content

Commit 5a950e5

Browse files
Merge pull request #230 from cycle/boolean-comparison
2 parents f8b3a87 + 9ac4fb2 commit 5a950e5

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/Schema/AbstractColumn.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,13 @@ public function compare(self $initial): bool
598598
? $initial->getDefaultValue()->__toString()
599599
: $initial->getDefaultValue();
600600

601+
$defaultValue = $this->userType === 'boolean'
602+
? (bool) $defaultValue
603+
: $defaultValue;
604+
$initialDefaultValue = $this->userType === 'boolean'
605+
? (bool) $initialDefaultValue
606+
: $initialDefaultValue;
607+
601608
//Default values has to compared using type-casted value
602609
if ($defaultValue != $initialDefaultValue) {
603610
$difference[] = $name;

tests/Database/Functional/Driver/MySQL/Schema/BooleanColumnTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,76 @@
1414
class BooleanColumnTest extends CommonClass
1515
{
1616
public const DRIVER = 'mysql';
17+
18+
public function testBooleanDefaultSize(): void
19+
{
20+
$schema = $this->schema('table');
21+
$schema->boolean('column');
22+
$schema->save();
23+
24+
$column = $this->fetchSchema($schema)->column('column');
25+
26+
$this->assertSame('boolean', $column->getAbstractType());
27+
$this->assertSame(1, $column->getSize());
28+
}
29+
30+
public function testBooleanComparisonWithSize(): void
31+
{
32+
$schema = $this->schema('table');
33+
$foo = $schema->boolean('foo')->defaultValue(false)->nullable(false)->unsigned(true)->size(1)->zerofill(true);
34+
$bar = $schema->boolean('bar', nullable: true, unsigned: true, size: 1, zerofill: true);
35+
$baz = $schema->boolean('baz', nullable: false, unsigned: true);
36+
$mux = $schema->boolean('mux', nullable: false);
37+
$schema->save();
38+
39+
$schema = $this->schema('table');
40+
$this->assertTrue($schema->exists());
41+
$this->assertSame(1, $foo->getSize());
42+
$this->assertSame(1, $bar->getSize());
43+
$this->assertSame(1, $baz->getSize());
44+
$this->assertSame(1, $mux->getSize());
45+
$this->assertSame(1, $schema->column('foo')->getSize());
46+
$this->assertSame(1, $schema->column('bar')->getSize());
47+
$this->assertTrue(\in_array($schema->column('baz')->getSize(), [1, 4], true));
48+
$this->assertSame(1, $schema->column('mux')->getSize());
49+
$this->assertTrue($foo->compare($schema->column('foo')));
50+
$this->assertTrue($bar->compare($schema->column('bar')));
51+
$this->assertTrue($baz->compare($schema->column('baz')));
52+
$this->assertTrue($mux->compare($schema->column('mux')));
53+
}
54+
55+
public function testBooleanWithProblematicValues(): void
56+
{
57+
$schema = $this->schema('table');
58+
59+
$column = $schema->boolean('target')
60+
->defaultValue(false)
61+
->nullable(false)
62+
->unsigned(true)
63+
->comment('Target comment');
64+
65+
$schema->save();
66+
67+
$this->assertTrue($schema->exists());
68+
69+
$schema = $this->schema('table');
70+
$target = $schema->column('target');
71+
72+
$this->assertSame(1, $column->getSize());
73+
$this->assertSame(4, $target->getSize());
74+
$this->assertFalse($column->isNullable());
75+
$this->assertFalse($target->isNullable());
76+
$this->assertTrue($column->isUnsigned());
77+
$this->assertTrue($target->isUnsigned());
78+
79+
$object = new \ReflectionObject($target);
80+
$property = $object->getProperty('defaultValue');
81+
$property->setAccessible(true);
82+
$defaultValue = $property->getValue($target);
83+
84+
$this->assertSame(false, $column->getDefaultValue());
85+
$this->assertSame(0, $target->getDefaultValue());
86+
$this->assertSame('0', $defaultValue);
87+
$this->assertTrue($column->compare($target));
88+
}
1789
}

0 commit comments

Comments
 (0)