| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- App::uses('StringTemplate', 'Tools.View');
- class StringTemplateTest extends CakeTestCase {
- /**
- * setUp
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- $this->template = new StringTemplate();
- }
- /**
- * Test adding templates through the constructor.
- *
- * @return void
- */
- public function testConstructorAdd() {
- $templates = [
- 'link' => '<a href="{{url}}">{{text}}</a>'
- ];
- $template = new StringTemplate($templates);
- debug($template->config('link'));
- $this->assertEquals($templates['link'], $template->config('link'));
- }
- /**
- * test adding templates.
- *
- * @return void
- */
- public function testAdd() {
- $templates = [
- 'link' => '<a href="{{url}}">{{text}}</a>'
- ];
- $result = $this->template->add($templates);
- $this->assertSame(
- $this->template,
- $result,
- 'The same instance should be returned'
- );
- $this->assertEquals($templates['link'], $this->template->config('link'));
- }
- /**
- * Test remove.
- *
- * @return void
- */
- public function testRemove() {
- $templates = [
- 'link' => '<a href="{{url}}">{{text}}</a>'
- ];
- $this->template->add($templates);
- $this->assertNull($this->template->remove('link'), 'No return');
- $this->assertNull($this->template->config('link'), 'Template should be gone.');
- }
- /**
- * Test formatting strings.
- *
- * @return void
- */
- public function testFormat() {
- $templates = [
- 'link' => '<a href="{{url}}">{{text}}</a>'
- ];
- $this->template->add($templates);
- $result = $this->template->format('not there', []);
- $this->assertSame('', $result);
- $result = $this->template->format('link', [
- 'url' => '/',
- 'text' => 'example'
- ]);
- $this->assertEquals('<a href="/">example</a>', $result);
- }
- /**
- * Test loading templates files in the app.
- *
- * @return void
- */
- public function testLoad() {
- $this->skipIf(true, 'Find a way to mock the path from /Tools/Config to /Tools/Test/test_app/Config');
- $this->template->remove('attribute');
- $this->template->remove('compactAttribute');
- $this->assertEquals([], $this->template->config());
- $this->assertNull($this->template->load('Tools.test_templates'));
- $this->assertEquals('<a href="{{url}}">{{text}}</a>', $this->template->config('link'));
- }
- /**
- * Test that loading non-existing templates causes errors.
- *
- * @expectedException ConfigureException
- * @expectedExceptionMessage Could not load configuration file
- */
- public function testLoadErrorNoFile() {
- $this->template->load('no_such_file');
- }
- /**
- * Test formatting compact attributes.
- *
- * @return void
- */
- public function testFormatAttributesCompact() {
- $attrs = ['disabled' => true, 'selected' => 1, 'checked' => '1', 'multiple' => 'multiple'];
- $result = $this->template->formatAttributes($attrs);
- $this->assertEquals(
- ' disabled="disabled" selected="selected" checked="checked" multiple="multiple"',
- $result
- );
- $attrs = ['disabled' => false, 'selected' => 0, 'checked' => '0', 'multiple' => null];
- $result = $this->template->formatAttributes($attrs);
- $this->assertEquals(
- '',
- $result
- );
- }
- /**
- * Test formatting normal attributes.
- *
- * @return void
- */
- public function testFormatAttributes() {
- $attrs = ['name' => 'bruce', 'data-hero' => '<batman>'];
- $result = $this->template->formatAttributes($attrs);
- $this->assertEquals(
- ' name="bruce" data-hero="<batman>"',
- $result
- );
- $attrs = ['escape' => false, 'name' => 'bruce', 'data-hero' => '<batman>'];
- $result = $this->template->formatAttributes($attrs);
- $this->assertEquals(
- ' name="bruce" data-hero="<batman>"',
- $result
- );
- $attrs = ['name' => 'bruce', 'data-hero' => '<batman>'];
- $result = $this->template->formatAttributes($attrs, ['name']);
- $this->assertEquals(
- ' data-hero="<batman>"',
- $result
- );
- }
- /**
- * Test formatting array attributes.
- *
- * @return void
- */
- public function testFormatAttributesArray() {
- $attrs = ['name' => ['bruce', 'wayne']];
- $result = $this->template->formatAttributes($attrs);
- $this->assertEquals(
- ' name="bruce wayne"',
- $result
- );
- }
- /**
- * Tests that compile information is refreshed on adds and removes
- *
- * @return void
- */
- public function testCopiledInfoRefresh() {
- $compilation = $this->template->compile('link');
- $this->template->add([
- 'link' => '<a bar="{{foo}}">{{baz}}</a>'
- ]);
- $this->assertNotEquals($compilation, $this->template->compile('link'));
- $this->template->remove('link');
- $this->assertEquals([null, null], $this->template->compile('link'));
- }
- }
|