<?php

namespace KupShop\KupShopBundle\Tests;

use KupShop\KupShopBundle\Util\CachingStreamedResponse;

class CachingStreamedResponseTest extends \DatabaseTestCase
{
    /**
     * @dataProvider data_testFlockCache
     */
    public function testFlockCache($fnc1, $fnc2, $exp_result, $ttl = 86400)
    {
        $response = new CachingStreamedResponse();

        $response->setCacheName('testCachingStreamedResponse');
        $response->setTtl(0);

        $response->setCallback(
            $fnc1
        );
        // Pro lepsi debugovani output funkce
        $response->setChunkSize(2);
        $response->initialize();
        ob_start();
        $response->sendContent();
        ob_end_clean();

        ob_start();
        $response2 = new CachingStreamedResponse();
        $response2->setCacheName('testCachingStreamedResponse');
        $response2->setTtl($ttl);
        $response2->setCallback(
            $fnc2
        );
        $response2->initialize();
        $response2->sendContent();

        $result = ob_get_contents();
        ob_end_clean();

        $this->assertEquals($exp_result, $result);
    }

    public function data_testFlockCache()
    {
        return [
            // Correct caching
            [
                function () {
                    echo 'test 1';
                },
                function () {
                    echo 'test 2';
                },
                'test 1',
            ],
            // TTL 0
            [
                function () {
                    echo 'test 1';
                },
                function () {
                    echo 'test 2';
                },
                'test 2',
                0,
            ],
            // Exception
            [
                function () {
                    echo 'test 1';
                },
                function () {
                    throw new \Exception();
                },
                'test 1',
            ],
            // Error
            [
                function () {
                    echo 'test 1';
                },
                function () {
                    throw new \Error();
                },
                'test 1',
            ],
        ];
    }

    public function testDoubleFlockCache()
    {
        $response = new CachingStreamedResponse();

        $response->setCacheName('testCachingStreamedResponse');
        $response->setTtl(0);

        $response->setCallback(
            function () {
                echo 'test 1';
            }
        );

        // Pro lepsi debugovani output funkce
        $response->setChunkSize(2);
        $response->initialize();
        ob_start();
        $response->sendContent();
        ob_end_clean();
        $result3 = null;
        ob_start();
        $response2 = new CachingStreamedResponse();
        $response2->setCacheName('testCachingStreamedResponse');
        $response2->setTtl(0);
        $response2->setCallback(
            function () use (&$result3) {
                echo 'test 2';

                ob_start();
                $response3 = new CachingStreamedResponse();
                $response3->setCacheName('testCachingStreamedResponse');
                $response3->setCallback(
                    function () {
                        echo 'test 3';
                    }
                );
                $response3->initialize();
                $response3->sendContent();
                $result3 = ob_get_contents();
                ob_end_clean();
            }
        );
        $response2->initialize();
        $response2->sendContent();

        $result2 = ob_get_contents();
        ob_end_clean();

        $this->assertEquals('test 1', $result3);
        $this->assertEquals('test 2', $result2);
    }
}
