diff options
Diffstat (limited to 'tests/Formats/JsonFormatTest.php')
-rw-r--r-- | tests/Formats/JsonFormatTest.php | 66 |
1 files changed, 9 insertions, 57 deletions
diff --git a/tests/Formats/JsonFormatTest.php b/tests/Formats/JsonFormatTest.php index d99c55b3..3b9f8d47 100644 --- a/tests/Formats/JsonFormatTest.php +++ b/tests/Formats/JsonFormatTest.php @@ -6,70 +6,22 @@ namespace RssBridge\Tests\Formats; -use FormatFactory; -use PHPUnit\Framework\TestCase; +require_once __DIR__ . '/BaseFormatTest.php'; -class JsonFormatTest extends TestCase { - const PATH_SAMPLES = __DIR__ . '/samples/'; - const PATH_EXPECTED = __DIR__ . '/samples/expectedJsonFormat/'; +use PHPUnit\Framework\TestCase; - private $sample; - private $format; - private $data; +class JsonFormatTest extends BaseFormatTest { + private const PATH_EXPECTED = self::PATH_SAMPLES . 'expectedJsonFormat/'; /** * @dataProvider sampleProvider * @runInSeparateProcess */ - public function testOutput($path) { - $this->setSample($path); - $this->initFormat(); - - $this->assertJsonStringEqualsJsonFile($this->sample->expected, $this->data); - } - - public function sampleProvider() { - $samples = array(); - foreach (glob(self::PATH_SAMPLES . '*.json') as $path) { - $samples[basename($path, '.json')] = array($path); - } - return $samples; - } - - private function setSample($path) { - $data = json_decode(file_get_contents($path), true); - if (isset($data['meta']) && isset($data['items'])) { - if (!empty($data['server'])) - $this->setServerVars($data['server']); - - $items = array(); - foreach($data['items'] as $item) { - $items[] = new \FeedItem($item); - } - - $this->sample = (object)array( - 'meta' => $data['meta'], - 'items' => $items, - 'expected' => self::PATH_EXPECTED . basename($path) - ); - } else { - $this->fail('invalid test sample: ' . basename($path, '.json')); - } - } - - private function setServerVars($list) { - $_SERVER = array_merge($_SERVER, $list); - } - - private function initFormat() { - $formatFac = new FormatFactory(); - $formatFac->setWorkingDir(PATH_LIB_FORMATS); - $this->format = $formatFac->create('Json'); - $this->format->setItems($this->sample->items); - $this->format->setExtraInfos($this->sample->meta); - $this->format->setLastModified(strtotime('2000-01-01 12:00:00 UTC')); + public function testOutput(string $name, string $path) { + $data = $this->formatData('Json', $this->loadSample($path)); + $this->assertNotNull(json_decode($data), 'invalid JSON output: ' . json_last_error_msg()); - $this->data = $this->format->stringify(); - $this->assertNotNull(json_decode($this->data), 'invalid JSON output: ' . json_last_error_msg()); + $expected = self::PATH_EXPECTED . $name . '.json'; + $this->assertJsonStringEqualsJsonFile($expected, $data); } } |