|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * @author Etienne Lamoureux <etienne.lamoureux@crystalgorithm.com> |
| 5 | + * @copyright 2014 Etienne Lamoureux |
| 6 | + * @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Crystalgorithm\PhpJsonIterator\Iterators; |
| 10 | + |
| 11 | +use PHPUnit_Framework_TestCase; |
| 12 | +use Crystalgorithm\PhpJsonIterator\JsonIteratorFactory; |
| 13 | + |
| 14 | +final class JsonIteratorTest extends PHPUnit_Framework_TestCase { |
| 15 | + |
| 16 | + const FLAT_JSON_OBJECT_AS_STRING = '{"foo":"bar","fizz":"buzz"}'; |
| 17 | + const NESTED_JSON_OBJECT_AS_STRING = '{"foo":"bar","fizz":{"id":1,"nested":true}}'; |
| 18 | + |
| 19 | + protected $counter; |
| 20 | + |
| 21 | + protected function setUp() { |
| 22 | + $this->counter = 0; |
| 23 | + } |
| 24 | + |
| 25 | + public function testGivenFlatJsonObjectsThenParseOneByOne() { |
| 26 | + $json = $this::buildTestJson(5, self::FLAT_JSON_OBJECT_AS_STRING); |
| 27 | + $iterator = JsonIteratorFactory::buildJsonIterator($json); |
| 28 | + |
| 29 | + $this->goThroughIterator($iterator); |
| 30 | + |
| 31 | + $this->assertEquals(5, $this->counter); |
| 32 | + } |
| 33 | + |
| 34 | + protected static function buildTestJson($nbObjects, $jsonObject) { |
| 35 | + $jsonObjects = array_fill(0, $nbObjects, $jsonObject); |
| 36 | + $json = implode(",", $jsonObjects); |
| 37 | + |
| 38 | + return $json; |
| 39 | + } |
| 40 | + |
| 41 | + protected function goThroughIterator($iterator) { |
| 42 | + foreach ($iterator as $parsedJsonObject) { |
| 43 | + // Here you can do something with $parsedJsonObject as an array, |
| 44 | + // has it had gone through json_decode already |
| 45 | + $this->counter++; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | +} |
0 commit comments