|
2 | 2 |
|
3 | 3 | use database\migrations\Test; |
4 | 4 | use database\migrations\TestCategory; |
5 | | -use MaplePHP\Query\Exceptions\ConnectException; |
6 | | -use MaplePHP\Query\Exceptions\DBQueryException; |
7 | | -use MaplePHP\Query\Exceptions\DBValidationException; |
| 5 | +use MaplePHP\Query\Handlers\PostgreSQLHandler; |
8 | 6 | use MaplePHP\Query\Handlers\SQLiteHandler; |
9 | 7 | use MaplePHP\Unitary\Unit; |
10 | 8 | use MaplePHP\Query\Connect; |
11 | 9 | use MaplePHP\Query\DB; |
12 | 10 |
|
13 | 11 | // Only validate if there is a connection open! |
14 | | -try { |
15 | | - if (Connect::hasInstance() && Connect::getInstance()->hasConnection()) { |
16 | | - $unit = new Unit(); |
17 | | - |
18 | | - // Add a title to your tests (not required) |
19 | | - $unit->addTitle("Testing MaplePHP Query library!"); |
20 | | - $unit->add("MySql Query builder", function ($inst) { |
21 | | - |
22 | | - $db = Connect::getInstance(); |
23 | | - $select = $db::select("id,a.name,b.name AS cat", ["test", "a"])->whereParent(0)->where("status", 0, ">")->limit(6); |
24 | | - $select->join(["test_category", "b"], "tid = id"); |
25 | | - |
26 | | - // 3 queries |
27 | | - $obj = $select->get(); |
28 | | - $arr = $select->fetch(); |
29 | | - $pluck = DB::table("test")->pluck("name")->get(); |
30 | | - |
31 | | - $inst->add($obj, [ |
32 | | - "isObject" => [], |
33 | | - "missingColumn" => function () use ($obj) { |
34 | | - return (isset($obj->name) && isset($obj->cat)); |
35 | | - } |
36 | | - ], "Data is missing"); |
37 | | - |
38 | | - $inst->add($arr, [ |
39 | | - "isArray" => [], |
40 | | - "noRows" => function () use ($arr) { |
41 | | - return (count($arr) > 0); |
42 | | - } |
43 | | - ], "Fetch feed empty"); |
44 | | - |
45 | | - $inst->add($pluck, [ |
46 | | - "isString" => [], |
47 | | - "length" => [1] |
48 | | - ], "Pluck is expected to return string"); |
49 | | - |
50 | | - $select = $db::select("id,test.name,test_category.name AS cat", new Test)->whereParent(0)->where("status", 0, ">")->limit(6); |
51 | | - $select->join(new TestCategory); |
52 | | - $obj = $select->obj(); |
53 | | - |
54 | | - $inst->add($obj, [ |
55 | | - "isObject" => [], |
56 | | - "missingColumn" => function () use ($obj) { |
57 | | - return (isset($obj->name) && isset($obj->cat)); |
58 | | - } |
59 | | - ], "Data is missing"); |
60 | | - }); |
61 | | - |
62 | | - /** |
63 | | - * This will test multiple databases AND |
64 | | - * validate sqLite database |
65 | | - */ |
66 | | - $unit->add("sqLite Query builder", function ($inst) { |
67 | | - |
68 | | - $sqLiteHandler = new SQLiteHandler(__DIR__ . "/database.sqlite"); |
69 | | - $sqLiteHandler->setPrefix("mp_"); |
70 | | - $connect = Connect::setHandler($sqLiteHandler, "mp"); |
71 | | - $connect->execute(); |
72 | | - |
73 | | - // Access sqLite connection |
74 | | - $select = Connect::getInstance("mp")::select("id,name,content", "test")->whereStatus(1)->limit(3); |
75 | | - $result = $select->fetch(); |
76 | | - $inst->add($select->fetch(), [ |
77 | | - "isArray" => [], |
78 | | - "rows" => function () use ($result) { |
79 | | - return (count($result) === 3); |
80 | | - } |
81 | | - ], "Fetch should equal to 3"); |
82 | | - }); |
83 | | - |
84 | | - $unit->execute(); |
85 | | - } |
86 | | - |
87 | | -} catch (ConnectException|DBValidationException|DBQueryException $e) { |
88 | | - echo $e->getMessage(); |
| 12 | +if (Connect::hasInstance() && Connect::getInstance()->hasConnection()) { |
| 13 | + |
| 14 | + $unit = new Unit(); |
| 15 | + |
| 16 | + // Add a title to your tests (not required) |
| 17 | + $unit->addTitle("Testing MaplePHP Query library!"); |
| 18 | + $unit->add("MySql Query builder", function ($inst) { |
| 19 | + |
| 20 | + $db = Connect::getInstance(); |
| 21 | + $select = $db::select("id,a.name,b.name AS cat", ["test", "a"])->whereParent(0)->where("status", 0, ">")->limit(6); |
| 22 | + $select->join(["test_category", "b"], "tid = id"); |
| 23 | + |
| 24 | + // 3 queries |
| 25 | + $obj = $select->get(); |
| 26 | + $arr = $select->fetch(); |
| 27 | + $pluck = DB::table("test")->pluck("name")->get(); |
| 28 | + |
| 29 | + $inst->add($obj, [ |
| 30 | + "isObject" => [], |
| 31 | + "missingColumn" => function () use ($obj) { |
| 32 | + return (isset($obj->name) && isset($obj->cat)); |
| 33 | + } |
| 34 | + ], "Data is missing"); |
| 35 | + |
| 36 | + $inst->add($arr, [ |
| 37 | + "isArray" => [], |
| 38 | + "noRows" => function () use ($arr) { |
| 39 | + return (count($arr) > 0); |
| 40 | + } |
| 41 | + ], "Fetch feed empty"); |
| 42 | + |
| 43 | + $inst->add($pluck, [ |
| 44 | + "isString" => [], |
| 45 | + "length" => [1] |
| 46 | + ], "Pluck is expected to return string"); |
| 47 | + |
| 48 | + $select = $db::select("id,test.name,test_category.name AS cat", new Test)->whereParent(0)->where("status", 0, ">")->limit(6); |
| 49 | + $select->join(new TestCategory); |
| 50 | + $obj = $select->obj(); |
| 51 | + |
| 52 | + $inst->add($obj, [ |
| 53 | + "isObject" => [], |
| 54 | + "missingColumn" => function () use ($obj) { |
| 55 | + return (isset($obj->name) && isset($obj->cat)); |
| 56 | + } |
| 57 | + ], "Data is missing"); |
| 58 | + }); |
| 59 | + |
| 60 | + /** |
| 61 | + * This will test multiple databases AND |
| 62 | + * validate sqLite database |
| 63 | + */ |
| 64 | + $unit->add("sqLite Query builder", function ($inst) { |
| 65 | + |
| 66 | + $sqLiteHandler = new SQLiteHandler(__DIR__ . "/database.sqlite"); |
| 67 | + $sqLiteHandler->setPrefix("mp_"); |
| 68 | + $connect = Connect::setHandler($sqLiteHandler, "lite"); |
| 69 | + $connect->execute(); |
| 70 | + |
| 71 | + // Access sqLite connection |
| 72 | + $select = Connect::getInstance("lite")::select("id,name,content", "test")->whereStatus(1)->limit(3); |
| 73 | + $result = $select->fetch(); |
| 74 | + $inst->add($result, [ |
| 75 | + "isArray" => [], |
| 76 | + "rows" => function () use ($result) { |
| 77 | + return (count($result) === 3); |
| 78 | + } |
| 79 | + ], "Fetch should equal to 3"); |
| 80 | + }); |
| 81 | + |
| 82 | + /** |
| 83 | + * This will test multiple databases AND |
| 84 | + * validate sqLite database |
| 85 | + */ |
| 86 | + $unit->add("sqLite Query builder", function ($inst) { |
| 87 | + |
| 88 | + $sqLiteHandler = new PostgreSQLHandler("127.0.0.1", "postgres", "", "maplephp"); |
| 89 | + $sqLiteHandler->setPrefix("maple_"); |
| 90 | + $connect = Connect::setHandler($sqLiteHandler, "psg"); |
| 91 | + $connect->execute(); |
| 92 | + |
| 93 | + // Access sqLite connection |
| 94 | + $select = Connect::getInstance("psg")::select("id,name", ["test", "a"])->limit(2); |
| 95 | + $result = $select->fetch(); |
| 96 | + $inst->add($result, [ |
| 97 | + "isArray" => [], |
| 98 | + "rows" => function () use ($result) { |
| 99 | + return (count($result) === 2); |
| 100 | + } |
| 101 | + ], "Fetch should equal to 2"); |
| 102 | + }); |
| 103 | + |
| 104 | + $unit->execute(); |
89 | 105 | } |
90 | | - |
91 | | - |
92 | | - |
93 | | - |
94 | | - |
|
0 commit comments