<?php

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

class Designer_Tests_PlaceHoldersStorageTest extends Designer_Tests_TestCase
{
    public function circularPlaceholders()
    {
        $placeholders = new PlaceHoldersStorage(array('variable1' => '{variable2}', 'variable2' => '{variable1}'));
        try {
            $placeholders->replace('{variable1}{variable2}');
            $this->assertTrue('"Circular reference ..." error should be thrown.');
        } catch (Exception $e) {
            $this->assertEquals($e->getMessage(), 'Circular reference in placeholder values.');
        }
    }

    public function replaceTypes()
    {
        $placeholders = new PlaceHoldersStorage(array('variable1' => 'var1',
                                'variable2' => 'var2', 'bool' => true, 'int' => -1));
        $expected = 'var1var21-1';
        $actual = $placeholders->replace('{variable1}{variable2}{bool}{int}');
        $this->assertEquals($expected, $actual);
    }

    public function emptyPlaceholder()
    {
        $placeholders = new PlaceHoldersStorage();
        $expected = '';
        $actual = $placeholders->replace('');
        $this->assertEquals($expected, $actual);
    }

    public function onePlaceholder()
    {
        $placeholders = new PlaceHoldersStorage();
        $placeholders->set('placeholder', 'ok');
        $expected = 'ok';
        $actual = $placeholders->replace('{placeholder}');
        $this->assertEquals($expected, $actual);
    }

    public function initialObject()
    {
        $placeholders = new PlaceHoldersStorage(array('test' => 'ok'));
        $expected = 'ok';
        $actual = $placeholders->replace('{test}');
        $this->assertEquals($expected, $actual);
    }

    public function initialPlaceholders()
    {
        $ph1 = new PlaceHoldersStorage(array('test' => 'ok'));
        $ph2 = new PlaceHoldersStorage($ph1);
        $expected = 'ok';
        $actual = $ph2->replace('{test}');
        $this->assertEquals($expected, $actual);
    }
}


