<?php

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

class Designer_Tests_HashTest extends Designer_Tests_TestCase
{
    private $hashSources = array(
        '/CloudZoom.js' => '3975589650',
        '/blogTemplate.html' => '337008561',
        '/bootstrap.css' => '1786884465',
    );

    private $hashFile;

    public function buildHash()
    {
        $this->hashFile = realpath(dirname(__FILE__)) . '/assets/hash.json';
        $hash = new Hash($this->hashFile);
        $hash->refresh($this->hashSources, true);
        $hash->save();
        $expected = json_encode($this->hashSources);
        $actual = file_get_contents($this->hashFile);

        $this->assertEquals($expected, $actual);

        return $hash;
    }

    public function getHashFromObject()
    {
        $hash = $this->buildHash();

        $expected = json_encode($this->hashSources);

        $actual = $hash->get(); //from object

        $this->assertEquals($expected, $actual);
    }

    public function refreshHashAfterDeletingFiles()
    {
        $hash = $this->buildHash();

        $sources = json_decode($hash->get(), true);
        $sources['/bootstrap.css'] = '[DELETED]';

        $result = array(
            '/CloudZoom.js' => '3975589650',
            '/blogTemplate.html' => '337008561',
            );

        $hash->refresh($sources);
        $hash->save();

        $expected = json_encode($result);

        $actual = file_get_contents($this->hashFile); //from disk
        $actual2 = $hash->get(); //from object

        $this->assertEquals($expected, $actual);
        $this->assertEquals($expected, $actual2);
    }

    public function refreshHashAfterChangingFiles()
    {
        $hash = $this->buildHash();

        $sources = json_decode($hash->get(), true);
        $sources['/CloudZoom.js'] = '55555555';

        $hash->refresh($sources);
        $hash->save();

        $expected = json_encode($sources);

        $actual = file_get_contents($this->hashFile); //from disk
        $actual2 = $hash->get(); //from object

        $this->assertEquals($expected, $actual);
        $this->assertEquals($expected, $actual2);
    }

    public function refreshHashAfterAddingFiles()
    {
        $hash = $this->buildHash();

        $sources = json_decode($hash->get(), true);
        $sources['/component.php'] = '3823947291';

        $hash->refresh($sources);
        $hash->save();

        $expected = json_encode($sources);

        $actual = file_get_contents($this->hashFile); //from disk
        $actual2 = $hash->get(); //from object

        $this->assertEquals($expected, $actual);
        $this->assertEquals($expected, $actual2);
    }
}