<?php

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

class Designer_Tests_CacheTest extends Designer_Tests_TestCase
{
    private $cacheSources = array(
            '/prototypes/vmenu' => array(
                '/style.css' => '.bd-vmenu-1 {top: 60%;} .bd-vmenu-1 {display: block;}',
                '/script.js' => 'jQuery(function ($) { var menu = $(\'.bd-vmenu-1\'); });'),
            '/prototypes/searchWidget' => array(
                '/style.css' => '.bd-searchwidget-1 { position: relative; }')
            );
    private $cacheFile;

    public function buildCache()
    {
        $this->cacheFile = realpath(dirname(__FILE__)) . '/assets/cache.json';
        $cache = new Cache($this->cacheFile);
        $cache->refresh($this->cacheSources, true);
        $cache->save();
        $expected = json_encode($this->cacheSources);
        $actual = file_get_contents($this->cacheFile);

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

        return $cache;
    }

    public function getCacheFromObject()
    {
        $cache = $this->buildCache();

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

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

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

    public function refreshCacheAfterDeletingFiles()
    {
        $cache = $this->buildCache();

        $sources = json_decode($cache->get(), true);
        $sources['/prototypes/vmenu']['/script.js'] = '[DELETED]';
        $sources['/prototypes/searchWidget']['/style.css'] = '[DELETED]';

        $result = array(
        '/prototypes/vmenu' => array(
            '/style.css' => '.bd-vmenu-1 {top: 60%;} .bd-vmenu-1 {display: block;}'));

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

        $expected = json_encode($result);

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

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

    public function refreshCacheAfterChangingFiles()
    {
        $cache = $this->buildCache();

        $sources = json_decode($cache->get(), true);
        $sources['/prototypes/vmenu']['/script.js'] = 'jQuery(function ($) { var menu = $(\'.bd-vmenu-1\'), ITEM = null });';
        $sources['/prototypes/searchWidget']['/style.css'] = '.bd-searchwidget-1 { position: relative; LEFT: 12px }';

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

        $expected = json_encode($sources);

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

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

    public function refreshCacheAfterAddingFiles()
    {
        $cache = $this->buildCache();

        $sources = json_decode($cache->get(), true);
        $sources['/prototypes/vmenu']['/test.php'] = '<?php echo $context; ?>';

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

        $expected = json_encode($sources);

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

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

    public function refreshCacheAfterAddingNewControl()
    {
        $cache = $this->buildCache();

        $sources = json_decode($cache->get(), true);
        $sources['/prototypes/hmenu']['/test.php'] = '<?php echo $context; ?>';

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

        $expected = json_encode($sources);

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

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