Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Refactor Decoder and Renderer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stil committed Oct 14, 2016
1 parent 1ff9d34 commit 33498f8
Show file tree
Hide file tree
Showing 6 changed files with 1,859 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/output/*
tests/Resources/output
tests/gifs/*.gif

# Created by .ignore support plugin (hsz.mobi)
Expand Down
42 changes: 19 additions & 23 deletions tests/DecoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,43 @@
namespace GIFEndec\Tests;

use GIFEndec\Decoder;
use GIFEndec\MemoryStream;
use GIFEndec\Frame;
use GIFEndec\Events\FrameDecodedEvent;
use GIFEndec\IO\FileStream;
use GIFEndec\Tests\Resources\Sample;
use GIFEndec\Tests\Resources\SampleCollection;

class DecoderTest extends TestCase
{
public function testDecode()
{
for ($i = 1; $i <= 6; $i++) {
$this->decode("test$i");
$samples = new SampleCollection();
foreach ($samples->read() as $sample) {
$this->decode($sample);
}
}

protected function decode($name)
protected function decode(Sample $sample)
{
$action = "split";
$animation = __DIR__."/gifs/$name.gif";
$checksumPath = __DIR__."/gifs/$name.$action.json";
$dir = __DIR__."/output/$action/$name";

$frameCount = 0;
$checksums = [];
$hasChecksums = $this->loadChecksums($checksumPath, $checksums);
$this->createDirOrClear($dir);

$dir = $sample->emptyRawFramesDir();

$stream = new MemoryStream();
$stream->loadFromFile($animation);
$stream = new FileStream($sample->localPath());
$decoder = new Decoder($stream);
$decoder->decode(function (Frame $frame, $index) use (&$checksums, $hasChecksums, &$frameCount, $name, $dir) {

$decoder->decode(function (FrameDecodedEvent $event) use (&$checksums, &$frameCount, $dir, $sample) {
$frameCount++;
$paddedIndex = str_pad($index, 3, '0', STR_PAD_LEFT);

$frame->getStream()->copyContentsToFile("$dir/frame{$paddedIndex}.gif");
$stream = $event->decodedFrame->getStream();
//$paddedIndex = str_pad($event->frameIndex, 3, '0', STR_PAD_LEFT);
//$stream->copyContentsToFile("$dir/frame{$paddedIndex}.gif");

$checksum = sha1($frame->getStream()->getContents());
if ($hasChecksums) {
$this->assertEquals($checksum, $checksums[$index]);
}
$checksums[$index] = $checksum;
$checksum = sha1($stream->getContents());
$this->assertEquals($checksum, $sample->getFrameRawSHA1($event->frameIndex));
$checksums[$event->frameIndex] = $checksum;
});

file_put_contents("$dir/$name.$action.json", json_encode($checksums, JSON_PRETTY_PRINT));
file_put_contents("$dir/_sha1.json", json_encode($checksums, JSON_PRETTY_PRINT));
}
}
49 changes: 26 additions & 23 deletions tests/RendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,51 @@
namespace GIFEndec\Tests;

use GIFEndec\Decoder;
use GIFEndec\MemoryStream;
use GIFEndec\Events\FrameRenderedEvent;
use GIFEndec\IO\FileStream;
use GIFEndec\IO\MemoryStream;
use GIFEndec\Renderer;
use GIFEndec\Tests\Resources\Sample;
use GIFEndec\Tests\Resources\SampleCollection;

class RendererTest extends TestCase
{
public function testRender()
{
for ($i = 1; $i <= 6; $i++) {
$this->render("test$i");
$samples = new SampleCollection();
foreach ($samples->read() as $sample) {
$this->render($sample);
}
}

protected function render($name)
protected function render(Sample $sample)
{
$action = "render";
$animation = __DIR__."/gifs/$name.gif";
$checksumPath = __DIR__."/gifs/$name.$action.json";
$dir = __DIR__."/output/$action/$name";

$frameCount = 0;
$checksums = [];
$hasChecksums = $this->loadChecksums($checksumPath, $checksums);
$this->createDirOrClear($dir);

$stream = new MemoryStream();
$stream->loadFromFile($animation);
$dir = $sample->emptyRenderedFramesDir();

$stream = new FileStream($sample->localPath());
$decoder = new Decoder($stream);
$renderer = new Renderer($decoder);
$renderer->start(function ($gd, $index) use (&$checksums, $hasChecksums, &$frameCount, $name, $dir) {

$renderer->start(function (FrameRenderedEvent $event) use (&$checksums, &$frameCount, $dir, $sample) {
$frameCount++;
$paddedIndex = str_pad($index, 3, '0', STR_PAD_LEFT);

$outputPath = "$dir/frame{$paddedIndex}.png";
imagepng($gd, $outputPath, 4, PNG_ALL_FILTERS);
$stream = new MemoryStream();
ob_start();
imagepng($event->renderedFrame, null, 4, PNG_ALL_FILTERS);
$stream->writeString(ob_get_contents());
ob_end_clean();

//$paddedIndex = str_pad($event->frameIndex, 3, '0', STR_PAD_LEFT);
//$stream->copyContentsToFile("$dir/frame{$paddedIndex}.gif");

$checksum = sha1(file_get_contents($outputPath));
if ($hasChecksums) {
$this->assertEquals($checksum, $checksums[$index]);
}
$checksums[$index] = $checksum;
$checksum = sha1($stream->getContents());
$this->assertEquals($checksum, $sample->getFrameRenderedSHA1($event->frameIndex));
$checksums[$event->frameIndex] = $checksum;
});

file_put_contents("$dir/$name.$action.json", json_encode($checksums, JSON_PRETTY_PRINT));
file_put_contents("$dir/_sha1.json", json_encode($checksums, JSON_PRETTY_PRINT));
}
}
103 changes: 103 additions & 0 deletions tests/Resources/Sample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
namespace GIFEndec\Tests\Resources;

class Sample
{
/**
* @var string
*/
private $name;

/**
* @var array
*/
private $data;

/**
* Sample constructor.
* @param string $name
* @param array $data
*/
public function __construct($name, array $data)
{
$this->name = $name;
$this->data = $data;
}

public function name()
{
return $this->name;
}

public function url()
{
return $this->data['url'];
}

private function localDir()
{
$dir = __DIR__ . "/output/{$this->name}";
if (!is_dir($dir)) {
mkdir($dir, null, true);
}

return $dir;
}

public function localPath()
{
return $this->localDir() . "/original.gif";
}

public function getFrameRenderedSHA1($index)
{
return $this->data["frames_rendered_sha1"][$index];
}

public function getFrameRawSHA1($index)
{
return $this->data["frames_raw_sha1"][$index];
}

public function emptyRenderedFramesDir()
{
return $this->openOrClearOutputDir('rendered');
}

public function emptyRawFramesDir()
{
return $this->openOrClearOutputDir('raw');
}

public function download()
{
if (file_exists($this->localPath())) {
return;
}

$ch = curl_init($this->url());
$fp = fopen($this->localPath(), 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}

private function openOrClearOutputDir($type)
{
$dir = "{$this->localDir()}/$type/";
$this->createDirOrClear($dir);
return $dir;
}

private function createDirOrClear($dir)
{
if (!is_dir($dir)) {
mkdir($dir, null, true);
} else {
foreach (glob("$dir/*") as $file) {
unlink($file);
}
}
}
}
38 changes: 38 additions & 0 deletions tests/Resources/SampleCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
namespace GIFEndec\Tests\Resources;

class SampleCollection
{
/**
* @var Sample[]
*/
private $samples = [];

public function __construct()
{
$samples = json_decode(file_get_contents(__DIR__ . '/gifs.json'), true);
foreach ($samples as $name => $data) {
if (!$data['enabled']) {
continue;
}
$this->samples[] = new Sample($name, $data);
}

$this->downloadAll();
}

/**
* @return Sample[]
*/
public function read()
{
return $this->samples;
}

private function downloadAll()
{
foreach ($this->samples as $sample) {
$sample->download();
}
}
}
Loading

0 comments on commit 33498f8

Please sign in to comment.