Skip to content

Commit 72fe617

Browse files
authored
Merge pull request #29 from WebFiori/dev
Added The Method `map` to The Class `ResultSet`
2 parents 8994d39 + 0234e3d commit 72fe617

25 files changed

+514
-184
lines changed

.github/workflows/php82.yml

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: Build PHP 8.2
2+
3+
on:
4+
push:
5+
branches: [ main, dev ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ${{ matrix.os }}
12+
services:
13+
sql.data:
14+
image: mcr.microsoft.com/mssql/server:2019-latest
15+
env:
16+
SA_PASSWORD: 1234567890@Eu
17+
ACCEPT_EULA: Y
18+
MSSQL_PID: Express
19+
ports:
20+
- "1433:1433"
21+
strategy:
22+
fail-fast: true
23+
matrix:
24+
os: [ ubuntu-latest ]
25+
php: [8.2]
26+
27+
name: PHP${{matrix.php}} - ${{matrix.os}}
28+
29+
steps:
30+
- name: Clone Repo
31+
uses: actions/checkout@v1
32+
33+
- name: Setup PHP
34+
uses: shivammathur/setup-php@v2
35+
with:
36+
php-version: ${{ matrix.php }}
37+
extensions: mysqli, mbstring, sqlsrv
38+
tools: phpunit:9.5.20, composer
39+
40+
- name: Shutdown Ubuntu MySQL
41+
run: sudo service mysql stop
42+
43+
- name: Set up MySQL
44+
uses: mirromutth/[email protected]
45+
with:
46+
mysql version: '5.7'
47+
mysql database: 'testing_db'
48+
mysql root password: 123456
49+
mysql user: 'root'
50+
mysql password: 123456
51+
52+
- name: Wait for MySQL
53+
run: |
54+
while ! mysqladmin ping --host=127.0.0.1 --password=123456 --silent; do
55+
sleep 1
56+
done
57+
58+
- name: Setup MSSQL
59+
run: sqlcmd -S localhost -U SA -P 1234567890@Eu -Q 'create database testing_db'
60+
61+
- name: Install Dependencies
62+
run: composer install --prefer-source --no-interaction --no-dev
63+
64+
- name: Execute Tests
65+
run: phpunit
66+
67+
- name: CodeCov
68+
uses: codecov/codecov-action@v1

tests/webfiori/database/tests/common/ConditionTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,32 @@ public function testEquals02() {
4040
$condition1 = new Condition('A', 'B', '=');
4141
$this->assertFalse($condition1->equals($condition0));
4242
}
43+
/**
44+
* @test
45+
*/
46+
public function testToString00() {
47+
$condition0 = new Condition('B', 'A', '=');
48+
$this->assertEquals('B = A', $condition0.'');
49+
}
50+
/**
51+
* @test
52+
*/
53+
public function testToString01() {
54+
$condition0 = new Condition(null, null, '=');
55+
$this->assertEquals('', $condition0.'');
56+
}
57+
/**
58+
* @test
59+
*/
60+
public function testToString02() {
61+
$condition0 = new Condition('A', null, '=');
62+
$this->assertEquals('A', $condition0.'');
63+
}
64+
/**
65+
* @test
66+
*/
67+
public function testToString03() {
68+
$condition0 = new Condition(null, 'B', '=');
69+
$this->assertEquals('B', $condition0.'');
70+
}
4371
}

tests/webfiori/database/tests/common/EntityMapperTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,23 @@ public function test00() {
6969
], $entityMapper->getEntityMethods());
7070
$this->assertTrue($entityMapper->create());
7171
}
72+
/**
73+
* @test
74+
*/
75+
public function test01() {
76+
$schema = new MySQLTestSchema();
77+
$entityMapper = new EntityMapper($schema->getTable('users'), '', '', '');
78+
$this->assertEquals('NewEntity', $entityMapper->getEntityName());
79+
$this->assertEquals('webfiori\\database\\entity', $entityMapper->getNamespace());
80+
$this->assertFalse($entityMapper->addAttribute(''));
81+
$this->assertFalse($entityMapper->addAttribute('0cool'));
82+
$this->assertFalse($entityMapper->addAttribute('not valid'));
83+
$this->assertFalse($entityMapper->addAttribute('also$not_valid'));
84+
$this->assertTrue($entityMapper->addAttribute('validName'));
85+
$this->assertFalse($entityMapper->addAttribute('validName '));
86+
$this->assertFalse($entityMapper->setEntityName('0Invalid'));
87+
$this->assertFalse($entityMapper->setEntityName('Invalid Class Name'));
88+
$this->assertTrue($entityMapper->setEntityName('ValidName'));
89+
$this->assertFalse($entityMapper->setEntityName('Invalid%Name'));
90+
}
7291
}

tests/webfiori/database/tests/common/ExpressionTest.php

+66-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testWhereExpression01() {
6363
/**
6464
* @test
6565
*/
66-
public function testSelectExpression0() {
66+
public function testSelectExpression00() {
6767
$t = new MySQLTable();
6868
$t->addColumns([
6969
'col-0' => [], 'col-1' => [], 'col-2' => [], 'col-3' => []
@@ -127,4 +127,69 @@ public function testSelectExpression0() {
127127
$expression->orderBy('col-2', 'a');
128128
$this->assertEquals(' order by `new_table`.`col_0`, `new_table`.`col_1` desc, `new_table`.`col_2` asc', $expression->getOrderBy());
129129
}
130+
/**
131+
* @test
132+
*/
133+
public function testSelectExpression01() {
134+
$t = new MySQLTable();
135+
$t->addColumns([
136+
'col-0' => [], 'col-1' => [], 'col-2' => [], 'col-3' => []
137+
]);
138+
$expression = new SelectExpression($t);
139+
$expression->addColumn('not-exist');
140+
$this->assertEquals(1, $expression->getColsCount());
141+
$this->assertEquals('select `new_table`.`not_exist` from `new_table`', $expression->getValue());
142+
}
143+
/**
144+
* @test
145+
*/
146+
public function testSelectExpression02() {
147+
$t = new MySQLTable();
148+
$t->addColumns([
149+
'col-0' => [], 'col-1' => [], 'col-2' => [], 'col-3' => []
150+
]);
151+
$expression = new SelectExpression($t);
152+
$expression->orderBy('not-exist-1');
153+
$expression->groupBy('not-exist-2');
154+
$this->assertEquals(' order by `new_table`.`not_exist_1`', $expression->getOrderBy());
155+
$this->assertEquals(' group by `new_table`.`not_exist_2`', $expression->getGroupBy());
156+
}
157+
/**
158+
* @test
159+
*/
160+
public function testSelectExpression03() {
161+
$t = new MySQLTable();
162+
$t->addColumns([
163+
'col-0' => [], 'col-1' => [], 'col-2' => [], 'col-3' => []
164+
]);
165+
$expression = new SelectExpression($t);
166+
$expression->addWhere('col-1', 'col-2', '=');
167+
$this->assertEquals(' where col-1 = col-2', $expression->getWhereStr());
168+
$expression->addWhere('A', 'B', '!=', 'super');
169+
$this->assertEquals(' where col-1 = col-2 and A != B', $expression->getWhereStr());
170+
}
171+
/**
172+
* @test
173+
*/
174+
public function testSelectExpression04() {
175+
$t = new MySQLTable();
176+
$t->addColumns([
177+
'col-0' => [], 'col-1' => [], 'col-2' => [], 'col-3' => []
178+
]);
179+
$expression = new SelectExpression($t);
180+
$expression->addWhere('col-1', null, '=');
181+
$this->assertEquals(' where col-1 is null', $expression->getWhereStr());
182+
}
183+
/**
184+
* @test
185+
*/
186+
public function testSelectExpression05() {
187+
$t = new MySQLTable();
188+
$t->addColumns([
189+
'col-0' => [], 'col-1' => [], 'col-2' => [], 'col-3' => []
190+
]);
191+
$expression = new SelectExpression($t);
192+
$expression->addWhere('col-1', null, '!=');
193+
$this->assertEquals(' where col-1 is not null', $expression->getWhereStr());
194+
}
130195
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace webfiori\database\tests\common;
4+
5+
use webfiori\database\ResultSet;
6+
use PHPUnit\Framework\TestCase;
7+
/**
8+
* Description of ResultSetTest
9+
*
10+
* @author Ibrahim
11+
*/
12+
class ResultSetTest extends TestCase {
13+
/**
14+
* @test
15+
*/
16+
public function test00() {
17+
$set = new ResultSet([
18+
['col_1' => 'Super', 'col_2' => 'not_ok', 'col_3' => 'Cool', 'col_4' => 0],
19+
['col_1' => 'Super-2', 'col_2' => 'ok', 'col_3' => 'Not Cool', 'col_4' => 1],
20+
['col_1' => 'Super-3', 'col_2' => 'ok', 'col_3' => 'Cool', 'col_4' => 2],
21+
['col_1' => 'Super-4', 'col_2' => 'not_ok', 'col_3' => 'Cool', 'col_4' => 3],
22+
['col_1' => 'Super-5', 'col_2' => 'ok', 'col_3' => 'Not Cool', 'col_4' => 4],
23+
['col_1' => 'Super-6', 'col_2' => 'ok', 'col_3' => 'Cool', 'col_4' => 5],
24+
['col_1' => 'Super-7', 'col_2' => 'not_ok', 'col_3' => 'Cool', 'col_4' => 6]
25+
]);
26+
$this->assertEquals(7, $set->getRowsCount());
27+
$this->assertEquals(7, $set->getMappedRowsCount());
28+
$this->assertEquals([
29+
['col_1' => 'Super', 'col_2' => 'not_ok', 'col_3' => 'Cool', 'col_4' => 0],
30+
['col_1' => 'Super-2', 'col_2' => 'ok', 'col_3' => 'Not Cool', 'col_4' => 1],
31+
['col_1' => 'Super-3', 'col_2' => 'ok', 'col_3' => 'Cool', 'col_4' => 2],
32+
['col_1' => 'Super-4', 'col_2' => 'not_ok', 'col_3' => 'Cool', 'col_4' => 3],
33+
['col_1' => 'Super-5', 'col_2' => 'ok', 'col_3' => 'Not Cool', 'col_4' => 4],
34+
['col_1' => 'Super-6', 'col_2' => 'ok', 'col_3' => 'Cool', 'col_4' => 5],
35+
['col_1' => 'Super-7', 'col_2' => 'not_ok', 'col_3' => 'Cool', 'col_4' => 6]
36+
], $set->getRows());
37+
$this->assertEquals([
38+
['col_1' => 'Super', 'col_2' => 'not_ok', 'col_3' => 'Cool', 'col_4' => 0],
39+
['col_1' => 'Super-2', 'col_2' => 'ok', 'col_3' => 'Not Cool', 'col_4' => 1],
40+
['col_1' => 'Super-3', 'col_2' => 'ok', 'col_3' => 'Cool', 'col_4' => 2],
41+
['col_1' => 'Super-4', 'col_2' => 'not_ok', 'col_3' => 'Cool', 'col_4' => 3],
42+
['col_1' => 'Super-5', 'col_2' => 'ok', 'col_3' => 'Not Cool', 'col_4' => 4],
43+
['col_1' => 'Super-6', 'col_2' => 'ok', 'col_3' => 'Cool', 'col_4' => 5],
44+
['col_1' => 'Super-7', 'col_2' => 'not_ok', 'col_3' => 'Cool', 'col_4' => 6]
45+
], $set->getMappedRows());
46+
47+
$index = 0;
48+
49+
foreach ($set as $record) {
50+
$this->assertEquals($index, $record['col_4']);
51+
$index++;
52+
}
53+
$set->clearSet();
54+
55+
$this->assertEquals(0, $set->getRowsCount());
56+
$this->assertEquals(0, $set->getMappedRowsCount());
57+
$this->assertEquals([], $set->getRows());
58+
$this->assertEquals([], $set->getMappedRows());
59+
}
60+
61+
/**
62+
* @test
63+
*/
64+
public function test01() {
65+
$set = new ResultSet();
66+
$this->assertEquals(0, $set->getRowsCount());
67+
$this->assertEquals(0, $set->getMappedRowsCount());
68+
$this->assertEquals([], $set->getRows());
69+
$this->assertEquals([], $set->getMappedRows());
70+
71+
$set->setData([
72+
['col_1' => 'Super', 'col_2' => 'not_ok', 'col_3' => 'Cool', 'col_4' => 0],
73+
['col_1' => 'Super-2', 'col_2' => 'ok', 'col_3' => 'Not Cool', 'col_4' => 1],
74+
['col_1' => 'Super-3', 'col_2' => 'ok', 'col_3' => 'Cool', 'col_4' => 2],
75+
['col_1' => 'Super-4', 'col_2' => 'not_ok', 'col_3' => 'Cool', 'col_4' => 3],
76+
['col_1' => 'Super-5', 'col_2' => 'ok', 'col_3' => 'Not Cool', 'col_4' => 4],
77+
['col_1' => 'Super-6', 'col_2' => 'ok', 'col_3' => 'Cool', 'col_4' => 5],
78+
['col_1' => 'Super-7', 'col_2' => 'not_ok', 'col_3' => 'Cool', 'col_4' => 6]
79+
]);
80+
81+
$this->assertEquals(7, $set->getRowsCount());
82+
$this->assertEquals(7, $set->getMappedRowsCount());
83+
$data = $set->map(function ($data) {
84+
$retVal = [];
85+
foreach ($data as $record) {
86+
if ($record['col_2'] == 'ok') {
87+
$retVal[] = $record['col_4'];
88+
}
89+
}
90+
return $retVal;
91+
});
92+
$this->assertEquals(7, $set->getRowsCount());
93+
$this->assertEquals(4, $set->getMappedRowsCount());
94+
$this->assertEquals([1, 2, 4, 5], $data);
95+
96+
$count = 0;
97+
foreach ($set as $record) {
98+
$count++;
99+
}
100+
$this->assertEquals(4, $count);
101+
102+
$data2 = $set->map(function ($data) {
103+
$retVal = [];
104+
foreach ($data as $record) {
105+
if ($record['col_3'] == 'Cool') {
106+
$retVal[] = $record['col_4'];
107+
}
108+
}
109+
return $retVal;
110+
});
111+
$this->assertEquals(7, $set->getRowsCount());
112+
$this->assertEquals(5, $set->getMappedRowsCount());
113+
$this->assertEquals([0,2,3,5,6], $data2);
114+
}
115+
}

tests/webfiori/database/tests/mssql/MSSQLColumnTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ public function testGetPHPType01() {
227227
*/
228228
public function testGetPHPType02() {
229229
$colObj = new MSSQLColumn('col', 'boolean');
230-
$this->assertEquals('boolean', $colObj->getPHPType());
230+
$this->assertEquals('bool', $colObj->getPHPType());
231231
$colObj->setIsNull(true);
232-
$this->assertEquals('boolean', $colObj->getPHPType());
232+
$this->assertEquals('bool', $colObj->getPHPType());
233233
}
234234
/**
235235
* @test

tests/webfiori/database/tests/mssql/MSSQLQueryBuilderTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use webfiori\database\ConnectionInfo;
88
use webfiori\database\mssql\MSSQLConnection;
99
use webfiori\database\DatabaseException;
10+
use webfiori\database\Expression;
1011
/**
1112
* Description of MSSQLQueryBuilderTest
1213
*
@@ -859,4 +860,12 @@ public function testWhereBetween05() {
859860
/**
860861
* @test
861862
*/
863+
public function testAfreggate07() {
864+
$schema = new MSSQLTestSchema();
865+
$schema->table('reports_list')
866+
->select(['function', new Expression('count(*) as count')])
867+
->orderBy(['count'])
868+
->groupBy('function');
869+
$this->assertEquals('select [reports_list].[function], count(*) as count from [reports_list] group by [reports_list].[function] order by [reports_list].[count]', $schema->getLastQuery());
870+
}
862871
}

tests/webfiori/database/tests/mysql/MySQLColumnTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -730,18 +730,18 @@ public function testGetPHPType00() {
730730
*/
731731
public function testGetPHPType01() {
732732
$colObj = new MySQLColumn('col', 'bool');
733-
$this->assertEquals('boolean', $colObj->getPHPType());
733+
$this->assertEquals('bool', $colObj->getPHPType());
734734
$colObj->setIsNull(true);
735-
$this->assertEquals('boolean', $colObj->getPHPType());
735+
$this->assertEquals('bool', $colObj->getPHPType());
736736
}
737737
/**
738738
* @test
739739
*/
740740
public function testGetPHPType02() {
741741
$colObj = new MySQLColumn('col', 'boolean');
742-
$this->assertEquals('boolean', $colObj->getPHPType());
742+
$this->assertEquals('bool', $colObj->getPHPType());
743743
$colObj->setIsNull(true);
744-
$this->assertEquals('boolean', $colObj->getPHPType());
744+
$this->assertEquals('bool', $colObj->getPHPType());
745745
}
746746
/**
747747
* @test

0 commit comments

Comments
 (0)