Skip to content

Commit 49d3fd3

Browse files
committed
More validators
1 parent 8122e3d commit 49d3fd3

24 files changed

+1399
-1157
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Tests\Fixtures\Definition;
4+
5+
abstract class Comparison extends \PHPFUI\ORM\Record
6+
{
7+
public static bool $autoIncrement = false;
8+
9+
public static array $fields = [
10+
'equal' => ['sqltype', 'string', 50, false, '', false, ],
11+
'not_equal' => ['sqltype', 'string', 50, false, '', false, ],
12+
'gt_field' => ['sqltype', 'string', 50, false, '', false, ],
13+
'gte_field' => ['sqltype', 'string', 50, false, '', false, ],
14+
'lt_field' => ['sqltype', 'string', 50, false, '', false, ],
15+
'lte_field' => ['sqltype', 'string', 50, false, '', false, ],
16+
'eq_field' => ['sqltype', 'string', 50, false, '', false, ],
17+
'neq_field' => ['sqltype', 'string', 50, false, '', false, ],
18+
'date' => ['date', 'string', 50, false, '', false, ],
19+
];
20+
21+
public static string $primaryKey = '';
22+
23+
public static string $table = '';
24+
}

Tests/Fixtures/Record/Comparison.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Tests\Fixtures\Record;
4+
5+
class Comparison extends \Tests\Fixtures\Definition\Comparison
6+
{
7+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Tests\Fixtures\Validation;
4+
5+
class Comparison extends \PHPFUI\ORM\Validator
6+
{
7+
public static array $validators = [
8+
'equal' => ['equal:2023-01-01'],
9+
'not_equal' => ['not_equal:2023-01-01'],
10+
'gt_field' => ['gt_field:date'],
11+
'gte_field' => ['gte_field:date'],
12+
'lt_field' => ['lt_field:date'],
13+
'lte_field' => ['lte_field:date'],
14+
'eq_field' => ['eq_field:date'],
15+
'neq_field' => ['neq_field:date'],
16+
'date' => ['date'],
17+
];
18+
}

Tests/Unit/InsertTest.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,6 @@
44

55
class InsertTest extends \PHPUnit\Framework\TestCase
66
{
7-
public function testStringNullInsert() : void
8-
{
9-
$transaction = new \PHPFUI\ORM\Transaction();
10-
$test = new \Tests\App\Record\StringRecord();
11-
$test->stringRequired = $required = 'required';
12-
$id = $test->insert();
13-
$this->assertGreaterThan(0, $id);
14-
$insertedTest = new \Tests\App\Record\StringRecord($id);
15-
$this->assertNull($insertedTest->stringDefaultNull);
16-
$this->assertEquals($required, $insertedTest->stringRequired);
17-
$this->assertEquals('default', $insertedTest->stringDefaultNullable);
18-
$this->assertEquals('default', $insertedTest->stringDefaultNotNull);
19-
$this->assertTrue($transaction->rollBack());
20-
}
21-
22-
public function testRequiredStringNotSetInsert() : void
23-
{
24-
$this->expectException(\Exception::class);
25-
$transaction = new \PHPFUI\ORM\Transaction();
26-
$test = new \Tests\App\Record\StringRecord();
27-
$id = $test->insert();
28-
$this->assertTrue($transaction->rollBack());
29-
}
30-
317
public function testDateNullInsert() : void
328
{
339
$transaction = new \PHPFUI\ORM\Transaction();
@@ -149,4 +125,28 @@ public function testRelatedInsert() : void
149125
$orderTable->setWhere();
150126
$this->assertEquals(48, $orderTable->count());
151127
}
128+
129+
public function testRequiredStringNotSetInsert() : void
130+
{
131+
$this->expectException(\Exception::class);
132+
$transaction = new \PHPFUI\ORM\Transaction();
133+
$test = new \Tests\App\Record\StringRecord();
134+
$id = $test->insert();
135+
$this->assertTrue($transaction->rollBack());
136+
}
137+
138+
public function testStringNullInsert() : void
139+
{
140+
$transaction = new \PHPFUI\ORM\Transaction();
141+
$test = new \Tests\App\Record\StringRecord();
142+
$test->stringRequired = $required = 'required';
143+
$id = $test->insert();
144+
$this->assertGreaterThan(0, $id);
145+
$insertedTest = new \Tests\App\Record\StringRecord($id);
146+
$this->assertNull($insertedTest->stringDefaultNull);
147+
$this->assertEquals($required, $insertedTest->stringRequired);
148+
$this->assertEquals('default', $insertedTest->stringDefaultNullable);
149+
$this->assertEquals('default', $insertedTest->stringDefaultNotNull);
150+
$this->assertTrue($transaction->rollBack());
151+
}
152152
}

Tests/Unit/SelectTest.php

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,18 @@ public function testSelectCount() : void
2424
$this->assertEquals('Company A', $dataObjectCursor->current()->company);
2525
}
2626

27-
public function testSelectWhere() : void
27+
public function testSelectGroupBy() : void
2828
{
29-
$table = new \Tests\App\Table\Customer();
30-
$table->setWhere(new \PHPFUI\ORM\Condition('job_title', 'Purchasing Manager'));
31-
$this->assertEquals(13, $table->count());
32-
33-
$table->setWhere(new \PHPFUI\ORM\Condition('job_title', '%Purchasing%', new \PHPFUI\ORM\Operator\Like()));
34-
$this->assertEquals(20, $table->count());
35-
36-
$table->setWhere(new \PHPFUI\ORM\Condition('job_title', '%Purchasing%', new \PHPFUI\ORM\Operator\NotLike()));
37-
$this->assertEquals(9, $table->count());
38-
39-
$condition = new \PHPFUI\ORM\Condition('job_title', '%Purchasing%', new \PHPFUI\ORM\Operator\Like());
40-
$condition->and(new \PHPFUI\ORM\Condition('state_province', 'NY'));
41-
$table->setWhere($condition);
42-
$this->assertEquals(2, $table->count());
43-
44-
$condition = new \PHPFUI\ORM\Condition('job_title', '%Purchasing%', new \PHPFUI\ORM\Operator\NotLike());
45-
$condition->or(new \PHPFUI\ORM\Condition('state_province', 'NY'));
46-
$table->setWhere($condition);
47-
$this->assertEquals(11, \count($table));
48-
$this->assertEquals(11, $table->count());
29+
$table = new \Tests\App\Table\InventoryTransaction();
30+
$table->addSelect('category');
31+
$table->addSelect(new \PHPFUI\ORM\Literal('count(*)'), 'count');
32+
$table->addJoin('product');
33+
$table->addGroupBy('category');
34+
$table->addOrderBy('count', 'desc');
35+
$this->assertEquals(14, $table->count());
36+
$record = $table->getDataObjectCursor()->current();
37+
$this->assertEquals(26, $record->count);
38+
$this->assertEquals('Beverages', $record->category);
4939
}
5040

5141
public function testSelectIn() : void
@@ -58,14 +48,29 @@ public function testSelectIn() : void
5848
$this->assertEquals(6, $orderTable->count());
5949
}
6050

61-
public function testSelectNotIn() : void
51+
public function testSelectJoin() : void
6252
{
63-
$orderDetailTable = new \Tests\App\Table\OrderDetail();
64-
$orderDetailTable->setWhere(new \PHPFUI\ORM\Condition('quantity', 10));
65-
$orderDetailTable->addSelect('order_id');
66-
$orderTable = new \Tests\App\Table\Order();
67-
$orderTable->setWhere(new \PHPFUI\ORM\Condition('order_id', $orderDetailTable, new \PHPFUI\ORM\Operator\NotIn()));
68-
$this->assertEquals(42, $orderTable->count());
53+
$table = new \Tests\App\Table\InventoryTransaction();
54+
$table->addJoin('inventory_transaction_type');
55+
$table->addJoin('order');
56+
$table->addJoin('product');
57+
$table->addJoin('purchase_order');
58+
59+
$table->setLimit(10);
60+
$this->assertEquals(10, $table->count());
61+
$this->assertEquals(102, $table->total());
62+
$record = $table->getDataObjectCursor()->current();
63+
64+
$this->assertEquals(10, $table->count());
65+
$this->assertEquals(3.5, $record->list_price);
66+
$this->assertEquals(25, $record->minimum_reorder_quantity);
67+
$this->assertEquals('NWTDFN-80', $record->product_code);
68+
$this->assertEquals(80, $record->product_product_id);
69+
$this->assertEquals('Northwind Traders Dried Plums', $record->product_name);
70+
$this->assertEquals('1 lb bag', $record->quantity_per_unit);
71+
$this->assertEquals(50, $record->reorder_level);
72+
$this->assertEquals(3, $record->standard_cost);
73+
$this->assertEquals(75, $record->target_level);
6974
}
7075

7176
// this fails under sqlite for some reason. The count clause returns 0 for unknown reasons. Commented out for now.
@@ -103,43 +108,14 @@ public function testSelectLimitOrderBy() : void
103108
$this->assertEquals(96, $table->getRecordCursor()->current()->inventory_transaction_id);
104109
}
105110

106-
public function testSelectGroupBy() : void
107-
{
108-
$table = new \Tests\App\Table\InventoryTransaction();
109-
$table->addSelect('category');
110-
$table->addSelect(new \PHPFUI\ORM\Literal('count(*)'), 'count');
111-
$table->addJoin('product');
112-
$table->addGroupBy('category');
113-
$table->addOrderBy('count', 'desc');
114-
$this->assertEquals(14, $table->count());
115-
$record = $table->getDataObjectCursor()->current();
116-
$this->assertEquals(26, $record->count);
117-
$this->assertEquals('Beverages', $record->category);
118-
}
119-
120-
public function testSelectJoin() : void
111+
public function testSelectNotIn() : void
121112
{
122-
$table = new \Tests\App\Table\InventoryTransaction();
123-
$table->addJoin('inventory_transaction_type');
124-
$table->addJoin('order');
125-
$table->addJoin('product');
126-
$table->addJoin('purchase_order');
127-
128-
$table->setLimit(10);
129-
$this->assertEquals(10, $table->count());
130-
$this->assertEquals(102, $table->total());
131-
$record = $table->getDataObjectCursor()->current();
132-
133-
$this->assertEquals(10, $table->count());
134-
$this->assertEquals(3.5, $record->list_price);
135-
$this->assertEquals(25, $record->minimum_reorder_quantity);
136-
$this->assertEquals('NWTDFN-80', $record->product_code);
137-
$this->assertEquals(80, $record->product_product_id);
138-
$this->assertEquals('Northwind Traders Dried Plums', $record->product_name);
139-
$this->assertEquals('1 lb bag', $record->quantity_per_unit);
140-
$this->assertEquals(50, $record->reorder_level);
141-
$this->assertEquals(3, $record->standard_cost);
142-
$this->assertEquals(75, $record->target_level);
113+
$orderDetailTable = new \Tests\App\Table\OrderDetail();
114+
$orderDetailTable->setWhere(new \PHPFUI\ORM\Condition('quantity', 10));
115+
$orderDetailTable->addSelect('order_id');
116+
$orderTable = new \Tests\App\Table\Order();
117+
$orderTable->setWhere(new \PHPFUI\ORM\Condition('order_id', $orderDetailTable, new \PHPFUI\ORM\Operator\NotIn()));
118+
$this->assertEquals(42, $orderTable->count());
143119
}
144120

145121
public function testSelectUnion() : void
@@ -155,4 +131,28 @@ public function testSelectUnion() : void
155131
$this->assertEquals(18, $table->count());
156132
$this->assertEquals('Allocated', $table->getDataObjectCursor()->current()->name);
157133
}
134+
135+
public function testSelectWhere() : void
136+
{
137+
$table = new \Tests\App\Table\Customer();
138+
$table->setWhere(new \PHPFUI\ORM\Condition('job_title', 'Purchasing Manager'));
139+
$this->assertEquals(13, $table->count());
140+
141+
$table->setWhere(new \PHPFUI\ORM\Condition('job_title', '%Purchasing%', new \PHPFUI\ORM\Operator\Like()));
142+
$this->assertEquals(20, $table->count());
143+
144+
$table->setWhere(new \PHPFUI\ORM\Condition('job_title', '%Purchasing%', new \PHPFUI\ORM\Operator\NotLike()));
145+
$this->assertEquals(9, $table->count());
146+
147+
$condition = new \PHPFUI\ORM\Condition('job_title', '%Purchasing%', new \PHPFUI\ORM\Operator\Like());
148+
$condition->and(new \PHPFUI\ORM\Condition('state_province', 'NY'));
149+
$table->setWhere($condition);
150+
$this->assertEquals(2, $table->count());
151+
152+
$condition = new \PHPFUI\ORM\Condition('job_title', '%Purchasing%', new \PHPFUI\ORM\Operator\NotLike());
153+
$condition->or(new \PHPFUI\ORM\Condition('state_province', 'NY'));
154+
$table->setWhere($condition);
155+
$this->assertEquals(11, \count($table));
156+
$this->assertEquals(11, $table->count());
157+
}
158158
}

Tests/Unit/ValidationTest.php

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,49 @@ public function testColor() : void
123123
$this->assertNotEmpty($validator->getErrors());
124124
}
125125

126+
public function testComparison() : void
127+
{
128+
$crud = new \Tests\Fixtures\Record\Comparison();
129+
$validator = new \Tests\Fixtures\Validation\Comparison($crud);
130+
131+
$date = '2023-01-01';
132+
$gtDate = '2023-01-02';
133+
$ltDate = '2022-12-31';
134+
135+
$crud->equal = $date;
136+
$crud->not_equal = $gtDate;
137+
$crud->gt_field = $gtDate;
138+
$crud->gte_field = $date;
139+
$crud->lt_field = $ltDate;
140+
$crud->lte_field = $date;
141+
$crud->eq_field = $date;
142+
$crud->neq_field = $ltDate;
143+
$crud->date = $date;
144+
$validator->validate();
145+
$errors = $validator->getErrors();
146+
$this->assertEmpty($errors);
147+
148+
$crud->equal = $ltDate;
149+
$crud->not_equal = $date;
150+
$crud->gt_field = $date;
151+
$crud->gte_field = $ltDate;
152+
$crud->lt_field = $date;
153+
$crud->lte_field = $gtDate;
154+
$crud->eq_field = $ltDate;
155+
$crud->neq_field = $date;
156+
$validator->validate();
157+
$errors = $validator->getErrors();
158+
$this->assertCount(8, $errors);
159+
$this->assertContains('2022-12-31 is not equal to 2023-01-01', $errors['equal']);
160+
$this->assertContains('2023-01-01 can not be equal to 2023-01-01', $errors['not_equal']);
161+
$this->assertContains('2023-01-01 is not greater than 2023-01-01 (date)', $errors['gt_field']);
162+
$this->assertContains('2022-12-31 is not greater than or equal to 2023-01-01 (date)', $errors['gte_field']);
163+
$this->assertContains('2023-01-01 is not less than 2023-01-01 (date)', $errors['lt_field']);
164+
$this->assertContains('2023-01-02 is not less than or equal to 2023-01-01 (date)', $errors['lte_field']);
165+
$this->assertContains('2022-12-31 is not equal to 2023-01-01 (date)', $errors['eq_field']);
166+
$this->assertContains('2023-01-01 can not be equal to 2023-01-01 (date)', $errors['neq_field']);
167+
}
168+
126169
public function testCvv() : void
127170
{
128171
$crud = new \Tests\Fixtures\Record\Cvv();
@@ -653,22 +696,6 @@ public function testMonthDayYear() : void
653696
$this->assertContains('2-30-20 is not a valid date (M-D-Y)', $errors['month_day_year']);
654697
}
655698

656-
public function testUnique() : void
657-
{
658-
$crud = new \Tests\Fixtures\Record\Product(90);
659-
$this->assertEquals('NWTCFV-90', $crud->product_code);
660-
$validator = new \Tests\Fixtures\Record\Validation\Product($crud);
661-
// empty test
662-
$validator->validate();
663-
$this->assertEmpty($validator->getErrors());
664-
$crud->product_code = 'NWTCFV-91';
665-
$validator->validate();
666-
$errors = $validator->getErrors();
667-
// fwrite(STDERR, print_r($errors, true));
668-
$this->assertCount(1, $errors);
669-
$this->assertContains('NWTCFV-91 is not unique', $errors['product_code']);
670-
}
671-
672699
public function testMonthYear() : void
673700
{
674701
$crud = new \Tests\Fixtures\Record\Month_year();
@@ -940,6 +967,22 @@ public function testTime() : void
940967
$this->assertNotEmpty($validator->getErrors());
941968
}
942969

970+
public function testUnique() : void
971+
{
972+
$crud = new \Tests\Fixtures\Record\Product(90);
973+
$this->assertEquals('NWTCFV-90', $crud->product_code);
974+
$validator = new \Tests\Fixtures\Record\Validation\Product($crud);
975+
// empty test
976+
$validator->validate();
977+
$this->assertEmpty($validator->getErrors());
978+
$crud->product_code = 'NWTCFV-91';
979+
$validator->validate();
980+
$errors = $validator->getErrors();
981+
// fwrite(STDERR, print_r($errors, true));
982+
$this->assertCount(1, $errors);
983+
$this->assertContains('NWTCFV-91 is not unique', $errors['product_code']);
984+
}
985+
943986
public function testUrl() : void
944987
{
945988
$crud = new \Tests\Fixtures\Record\Url();

docs/10. Miscellaneous.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ Returne the value from the first field in the first row returned by the querry,
3030
### getValueArray(string $sql, array $input = []) : array<mixed>
3131
Returns the data values of the first element in each row of the query.
3232

33+
## **\PHPFUI\ORM\Record** Data Cleaning Helpers
34+
The Record class provides some simple data cleaning functions you can use where needed.
35+
* **cleanUpperCase**(string $field) Converts the field to all upper case
36+
* **cleanLowerCase**(string $field) Converts the field to all lower case
37+
* **cleanNumber**(string $field) removes all non-digits (0-9 and -) from string representation of a number
38+
* **cleanFloat**(string $field, int $decimalPoints = 2) removes all non-digits (0-9, . and -)
39+
* **cleanPhone**(string $field, string $regExSeparators = '\\-\\.') removes all non-digits (0-9) and regex separators
40+
* **cleanProperName**(string $field) Properly capitalizes proper names if in single case. Mixed case strings are not altered.
41+
* **cleanEmail**(string $field) Lowercases and strips invalid email characters. Does not validate email address.
42+
3343
## Example Scripts
3444
It is recommended to make versions of these example scripts customized to you needs:
3545
### cleanBackup.php

0 commit comments

Comments
 (0)