<?php

Designer::load('Designer_App_Chunk');
Designer::load('Designer_Tests_TestCase');

class Designer_Tests_ChunkTest extends Designer_Tests_TestCase
{
    private $_contentFile;

    private function _createChunks($chunkSize = 100000/*bytes*/)
    {
        $this->contentFile = realpath(dirname(__FILE__)) . '/assets/content.json';
        $content = file_get_contents($this->contentFile);
        $length = strlen($content);
        if ($length > $chunkSize) {
            for ($offset = 0; $offset < $length; $offset += $chunkSize) {
                $chunks[] = substr($content, $offset, $chunkSize);
            }
        } else {
            $chunks[] = $content;
        }
        return $chunks;
    }

    public function saveAndCompleteChunk()
    {
        $chunks = $this->_createChunks();
        $id = md5(round(microtime(true)));
        for ($i = 1; $i <= count($chunks); $i++) {
            $info = array(
                'id' => $id,
                'content' => $chunks[$i - 1],
                'current' => $i,
                'total' => count($chunks)
            );
            $chunkObj = new Chunk();
            $chunkObj->save($info);
            $this->assertFileExists($chunkObj->UPLOAD_PATH . $id . '/' . $i);
            if ($chunkObj->last()) {
                $actual = $chunkObj->complete();
            }
        }
        $expected = file_get_contents($this->contentFile);
        $this->assertEquals($expected, $actual);
    }

    public function chunkIdValidation()
    {
        $info = array(
            'id' => '0b588d9fb46179d0224b39e33e88ae9a',
            'content' => 'file content',
            'current' => 1,
            'total' => 3
        );
        $chunkObj = new Chunk();
        $expected = '';
        $actual = $chunkObj->validate($info);
        $this->assertEquals($expected, $actual);

        unset($info['id']);
        $actual = $chunkObj->validate($info);
        $this->assertNotEquals($expected, $actual);
    }

    public function chunkContentValidation()
    {
        $info = array(
            'id' => '0b588d9fb46179d0224b39e33e88ae9a',
            'content' => 'file content',
            'current' => 1,
            'total' => 3
        );
        unset($info['content']);
        $chunkObj = new Chunk();
        $actual = $chunkObj->validate($info);
        $expected = '';
        $this->assertNotEquals($expected, $actual);
    }

    public function chunkCurrentValidation()
    {
        $info = array(
            'id' => '0b588d9fb46179d0224b39e33e88ae9a',
            'content' => 'file content',
            'current' => 1,
            'total' => 3
        );
        unset($info['current']);
        $chunkObj = new Chunk();
        $actual = $chunkObj->validate($info);
        $expected = '';
        $this->assertNotEquals($expected, $actual);
    }

    public function chunkTotalValidation()
    {
        $info = array(
            'id' => '0b588d9fb46179d0224b39e33e88ae9a',
            'content' => 'file content',
            'current' => 1,
            'total' => 3
        );
        unset($info['total']);
        $chunkObj = new Chunk();
        $actual = $chunkObj->validate($info);
        $expected = '';
        $this->assertNotEquals($expected, $actual);
    }
}
