|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use HiFolks\DataType\Block; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | + |
| 8 | +final class BlockAppendTest extends TestCase |
| 9 | +{ |
| 10 | + public function testAppendsJsons(): void |
| 11 | + { |
| 12 | + $data1 = Block::fromJsonFile( |
| 13 | + __DIR__ . "/data/commits-json/commits-10-p1.json", |
| 14 | + ); |
| 15 | + $data2 = Block::fromJsonFile( |
| 16 | + __DIR__ . "/data/commits-json/commits-10-p2.json", |
| 17 | + ); |
| 18 | + $data3 = Block::fromJsonFile( |
| 19 | + __DIR__ . "/data/commits-json/commits-10-p3.json", |
| 20 | + ); |
| 21 | + |
| 22 | + $this->assertCount(10, $data1); |
| 23 | + $this->assertCount(10, $data2); |
| 24 | + |
| 25 | + $data1->append($data2); |
| 26 | + $this->assertCount(20, $data1); |
| 27 | + |
| 28 | + $arrayData3 = $data3->toArray(); |
| 29 | + $this->assertIsArray($arrayData3); |
| 30 | + $this->assertCount(10, $arrayData3); |
| 31 | + |
| 32 | + $this->assertCount(20, $data1); |
| 33 | + |
| 34 | + $data1->append($arrayData3); |
| 35 | + $this->assertCount(30, $data1); |
| 36 | + } |
| 37 | + |
| 38 | + public function testAppendsArray(): void |
| 39 | + { |
| 40 | + $data1 = Block::make(["a", "b"]); |
| 41 | + $arrayData2 = ["c", "d"]; |
| 42 | + |
| 43 | + $this->assertCount(2, $data1); |
| 44 | + |
| 45 | + $data1->append($arrayData2); |
| 46 | + |
| 47 | + $this->assertCount(4, $data1); |
| 48 | + |
| 49 | + $this->assertSame( |
| 50 | + ["a", "b", "c", "d"], |
| 51 | + array_values($data1->toArray()), |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + public function testAppendsItem(): void |
| 56 | + { |
| 57 | + $data1 = Block::make(["a", "b"]); |
| 58 | + $arrayData2 = ["c", "d"]; |
| 59 | + |
| 60 | + $this->assertCount(2, $data1); |
| 61 | + |
| 62 | + // appendItem adds the whole array as a single element |
| 63 | + $data1->appendItem($arrayData2); |
| 64 | + |
| 65 | + $this->assertCount(3, $data1); |
| 66 | + |
| 67 | + $this->assertSame( |
| 68 | + ["a", "b", ["c", "d"]], |
| 69 | + array_values($data1->toArray()), |
| 70 | + ); |
| 71 | + } |
| 72 | +} |
0 commit comments