Skip to content

Commit ff8be33

Browse files
committed
Adding cursor test with enums
1 parent 68a51df commit ff8be33

File tree

9 files changed

+69
-12
lines changed

9 files changed

+69
-12
lines changed

Tests/Fixtures/Definition/Integer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ abstract class Integer extends \PHPFUI\ORM\Record
88

99
public static array $fields = [
1010
'integer' => ['sqltype', 'int', 19, false, '', false, ],
11-
'not_integer' => ['!sqltype', 'int', 19, false, '', false, ],
11+
'not_integer' => ['!sqltype', 'null', 19, false, '', false, ],
1212
];
1313

1414
public static string $primaryKey = '';

Tests/Fixtures/Enum/Name.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Tests\Fixtures\Enum;
4+
5+
trait Name
6+
{
7+
public function name() : string
8+
{
9+
return \ucwords(\strtolower(\str_replace('_', ' ', $this->name)));
10+
}
11+
}

Tests/Fixtures/Enum/ProductStatus.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Tests\Fixtures\Enum;
4+
5+
enum ProductStatus : int
6+
{
7+
use \Tests\Fixtures\Enum\Name;
8+
9+
case ACTIVE = 0;
10+
case BACKORDERED = 1;
11+
case DISCONTINUED = 2;
12+
}
13+

Tests/Fixtures/Record/Product.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
class Product extends \Tests\Fixtures\Record\Definition\Product
66
{
77
protected static array $virtualFields = [
8-
'suppliers' => [\PHPFUI\ORM\ManyToMany::class, \Tests\App\Table\ProductSupplier::class, \Tests\App\Table\Supplier::class],
8+
'suppliers' => [\PHPFUI\ORM\ManyToMany::class, \Tests\App\Table\ProductSupplier::class, \Tests\App\Table\Supplier::class, ],
99
'photos' => [\PHPFUI\ORM\MorphMany::class, \Tests\App\Table\Image::class, 'imageable', ],
10+
'discontinued' => [\PHPFUI\ORM\Enum::class, \Tests\Fixtures\Enum\ProductStatus::class, ],
1011
];
1112
}

Tests/Fixtures/Record/Validation/Product.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class Product extends \PHPFUI\ORM\Validator
1111
public static array $validators = [
1212
'category' => ['maxlength'],
1313
'description' => ['maxlength'],
14-
'discontinued' => ['required', 'integer'],
1514
'list_price' => ['required', 'number'],
1615
'minimum_reorder_quantity' => ['integer'],
1716
'product_code' => ['maxlength', 'unique'],
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Tests\Fixtures\Virtual;
4+
5+
class ProductStatus extends \PHPFUI\ORM\VirtualField
6+
{
7+
public function getValue(array $parameters) : mixed
8+
{
9+
return \number_format($this->currentRecord->unit_price * $this->currentRecord->quantity - $this->currentRecord->discount, 2);
10+
}
11+
}

Tests/Unit/CursorTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,14 @@ public function testCursor() : void
3737
$this->assertEquals($index, $record['customer_id'], 'DataObjectCursor array access is not correct');
3838
}
3939
}
40+
41+
public function testCursorOnEnum() : void
42+
{
43+
$table = new \Tests\Fixtures\Table\Product();
44+
45+
foreach ($table->getRecordCursor() as $key => $record)
46+
{
47+
$this->assertTrue($record->discontinued instanceof \Tests\Fixtures\Enum\ProductStatus, 'discontinued (type ' . get_debug_type($record->discontinued) . ') is not a ProductStatus enum');
48+
}
49+
}
4050
}

Tests/Unit/InsertTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function testDateNullInsert() : void
1212
$timeStamp = \date('Y-m-d H:i:s');
1313
$id = $test->insert();
1414
$insertedTest = new \Tests\App\Record\DateRecord($id);
15-
$this->assertNull($insertedTest->dateDefaultNull);
15+
$this->assertNull($insertedTest->dateDefaultNull, 'dateDefaultNull is not null');
1616
$this->assertEquals($date, $insertedTest->dateRequired);
1717
$this->assertEquals('2000-01-02', $insertedTest->dateDefaultNullable);
1818
$this->assertEquals('2000-01-02', $insertedTest->dateDefaultNotNull);
@@ -27,11 +27,12 @@ public function testDateRequiredInsert() : void
2727
$test = new \Tests\App\Record\DateRecord();
2828
$id = $test->insert();
2929
$this->assertNotEmpty(\PHPFUI\ORM::getLastError());
30-
$this->assertEquals(0, $id);
30+
$this->assertEquals(0, $id, '$id is not zero');
3131
$insertedTest = new \Tests\App\Record\DateRecord($id);
32-
$this->assertNull($insertedTest->dateDefaultNull);
33-
$this->assertEquals('2000-01-02', $insertedTest->dateDefaultNullable);
34-
$this->assertEquals('2000-01-02', $insertedTest->dateDefaultNotNull);
32+
$this->assertNull($insertedTest->dateDefaultNull, 'dateDefaultNull is not after insert');
33+
$this->assertEquals('2000-01-02', $insertedTest->dateDefaultNullable, 'dateDefaultNullable has bad default value');
34+
$this->assertEquals('2000-01-02', $insertedTest->dateDefaultNotNull, 'dateDefaultNotNull has bad default value');
35+
3536
}
3637

3738
public function testMultipleInserts() : void

Tests/Unit/ValidationTest.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,20 +500,31 @@ public function testInteger() : void
500500

501501
// empty test
502502
$validator->validate();
503-
$this->assertEmpty($validator->getErrors());
503+
$this->assertEmpty($validator->getErrors(), 'Errors found on empty integer validate');
504+
505+
// float test
506+
$crud->not_integer = 1.23;
507+
$validator->validate();
508+
$this->assertEmpty($validator->getErrors(), 'Errors found on float not_integer validate');
509+
510+
// int test
511+
$crud->not_integer = 1;
512+
$validator->validate();
513+
$this->assertCount(1, $validator->getErrors(), 'Errors found on int in not_integer validate');
504514

505515
// valid tests
516+
$crud->not_integer = 1.23;
506517
$crud->integer = 0;
507518
$validator->validate();
508-
$this->assertEmpty($validator->getErrors());
519+
$this->assertEmpty($validator->getErrors(), 'Errors found on zero integer validate');
509520

510521
$crud->integer = -1;
511522
$validator->validate();
512-
$this->assertEmpty($validator->getErrors());
523+
$this->assertEmpty($validator->getErrors(), 'Errors found on negative integer validate');
513524

514525
$crud->integer = 1;
515526
$validator->validate();
516-
$this->assertEmpty($validator->getErrors());
527+
$this->assertEmpty($validator->getErrors(), 'Errors found on positive integer validate');
517528
}
518529

519530
public function testMaxlength() : void

0 commit comments

Comments
 (0)