Skip to content

Commit 8f8ce0d

Browse files
committed
Detect invalid null assignments
1 parent 0d1905a commit 8f8ce0d

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

Tests/Unit/MiscellaneousTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ enum TestEnum : int
1010

1111
class MiscellaneousTest extends \PHPUnit\Framework\TestCase
1212
{
13+
public function testNullAssignment() : void
14+
{
15+
$po = new \Tests\App\Record\PurchaseOrder();
16+
$po->payment_method = 'Cash';
17+
$this->assertNotNull($po->payment_method);
18+
$po->payment_method = null;
19+
$this->assertNull($po->payment_method);
20+
}
21+
22+
public function testNullAssignmentError() : void
23+
{
24+
$po = new \Tests\App\Record\PurchaseOrder();
25+
$this->assertIsFloat($po->shipping_fee);
26+
$this->assertEquals(0.0, $po->shipping_fee);
27+
$this->expectException(\PHPFUI\ORM\Exception::class);
28+
$po->shipping_fee = null;
29+
}
30+
1331
public function testEnumCondition() : void
1432
{
1533
$condition = new \PHPFUI\ORM\Condition('field', TestEnum::YES);

Tests/Unit/ValidationTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,6 @@ public function testRequired() : void
890890
$this->assertArrayHasKey('required', $errors);
891891
$this->assertContains('This field is required', $errors['required']);
892892

893-
$crud->required = null;
894893
$validator->validate();
895894
$this->assertNotEmpty($validator->getErrors());
896895
}

src/PHPFUI/ORM/Record.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,17 @@ public function __set(string $field, mixed $value) : void
186186
$expectedType = static::$fields[$field][self::PHP_TYPE_INDEX];
187187
$haveType = \get_debug_type($value);
188188

189-
if (null !== $value && $haveType != $expectedType)
189+
if (null === $value)
190+
{
191+
if (! static::$fields[$field][self::ALLOWS_NULL_INDEX])
192+
{
193+
$message = static::class . "::{$field} does not allow nulls";
194+
\PHPFUI\ORM::log(\Psr\Log\LogLevel::WARNING, $message);
195+
196+
throw new \PHPFUI\ORM\Exception($message);
197+
}
198+
}
199+
elseif ($haveType != $expectedType)
190200
{
191201
$message = static::class . "::{$field} is of type {$expectedType} but being assigned a type of {$haveType}";
192202
\PHPFUI\ORM::log(\Psr\Log\LogLevel::WARNING, $message);
@@ -741,11 +751,6 @@ protected function validateFieldExists(string $field) : void
741751
*/
742752
private function buildWhere(array|int|string $key, array &$input) : string
743753
{
744-
if ('*' === $key)
745-
{
746-
return '';
747-
}
748-
749754
if (! \is_array($key))
750755
{
751756
$key = [static::$primaryKeys[0] => $key];

src/PHPFUI/ORM/Tool/Generate/CRUD.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ protected function getLine(\PHPFUI\ORM\Schema\Field $field) : string
179179
case 'float':
180180
if (null !== $field->defaultValue)
181181
{
182-
$defaultValue = 'NULL' !== $field->defaultValue ? (float)$field->defaultValue : 'NULL';
182+
$temp = str_replace("'", '', $field->defaultValue);
183+
$defaultValue = 'NULL' !== $field->defaultValue ? $temp : 'NULL';
183184
}
184185

185186
break;

0 commit comments

Comments
 (0)