|
| 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 | +} |
0 commit comments