Write tests that verify the truth of #1373

This commit is contained in:
Samuel Georges 2015-08-15 13:39:44 +10:00
parent 85933facbc
commit 2c5f9eba6b
26 changed files with 296 additions and 18 deletions

View File

@ -205,7 +205,7 @@ class ComponentManager
if (!class_exists($className)) {
throw new SystemException(sprintf(
'Component class not found %s.Check the component plugin.',
'Component class not found %s. Check the component plugin.',
$className
));
}

View File

@ -58,6 +58,10 @@ class PartialStack
*/
public function getComponent($name)
{
if (!$this->activePartial) {
return null;
}
$component = $this->findComponentFromStack($name, $this->activePartial);
if ($component !== null) {
return $component;

View File

@ -18,7 +18,9 @@ class Plugin extends PluginBase
{
return [
'October\Tester\Components\Archive' => 'testArchive',
'October\Tester\Components\Post' => 'testPost'
'October\Tester\Components\Post' => 'testPost',
'October\Tester\Components\MainMenu' => 'testMainMenu',
'October\Tester\Components\ContentBlock' => 'testContentBlock'
];
}

View File

@ -0,0 +1,25 @@
<?php namespace October\Tester\Components;
use Cms\Classes\ComponentBase;
class ContentBlock extends ComponentBase
{
public function componentDetails()
{
return [
'name' => 'Content Block Dummy Component',
'description' => 'Displays a editable content block.'
];
}
public function onRender()
{
if ($output = $this->property('output')) {
return 'Custom output: '.$output;
}
return 'Pass';
}
}

View File

@ -0,0 +1,21 @@
<?php namespace October\Tester\Components;
use Cms\Classes\ComponentBase;
class MainMenu extends ComponentBase
{
public function componentDetails()
{
return [
'name' => 'Menu Dummy Component',
'description' => 'Displays a really cool menu.'
];
}
public function menuItems()
{
return ['Home', 'Blog', 'About', 'Contact'];
}
}

View File

@ -0,0 +1,4 @@
<h4>DEFAULT MARKUP: Menu</h4>
<ul>
{% partial __SELF__ ~ "::items" items=__SELF__.menuItems %}
</ul>

View File

@ -0,0 +1,3 @@
{% for item in items %}
<li>DEFAULT: {{ item }}</li>
{% endfor %}

View File

@ -0,0 +1 @@
<p>DEFAULT MARKUP: I am a post yay</p>

View File

@ -0,0 +1,10 @@
url = "/component-custom-render"
[October\Tester\Components\ContentBlock theblock]
==
{% component 'theblock' %}
{% component 'theblock' output="Would you look over Picasso's shoulder" %}
{% component 'theblock' output='And tell him about his brush strokes?' %}

View File

@ -0,0 +1,3 @@
url = "/component-partial-nesting"
==
{% partial 'nesting/level1' %}

View File

@ -0,0 +1,5 @@
url = "/component-partial-override"
[October\Tester\Components\Post override1]
==
{% component 'override1' %}

View File

@ -0,0 +1,5 @@
url = "/component-partial"
[October\Tester\Components\Post post]
==
{% component 'post' %}

View File

@ -0,0 +1,5 @@
url = "/no-component-class"
[PeterPan\Nevernever\Land noComponentExist]
==
<p>Hey</p>

View File

@ -0,0 +1,3 @@
url = "/no-component"
==
<p>Hey</p>{% component 'noComponentExist' %}

View File

@ -1,5 +1,4 @@
url = "/no-partial"
layout = "caramba"
==
<p>Hey</p>
{{ partial('caramba') }}

View File

@ -0,0 +1,6 @@
[October\Tester\Components\MainMenu override2]
[October\Tester\Components\Post override3]
==
<h1>Level 1</h1>
{% component 'override2' %}
{% component 'override3' %}

View File

@ -0,0 +1,6 @@
[October\Tester\Components\Post post]
[October\Tester\Components\Post override4]
==
<h1>Level 2</h1>
{% component 'post' %}
{% component 'override4' %}

View File

@ -0,0 +1,4 @@
[October\Tester\Components\MainMenu mainMenu]
==
<h1>Level 3</h1>
{% component 'mainMenu' %}

View File

@ -0,0 +1 @@
<p>I am an override partial! Yay</p>

View File

@ -0,0 +1,7 @@
<ul>
{% partial __SELF__ ~ "::items" items=__SELF__.menuItems %}
{% partial __SELF__ ~ "::items" items=__SELF__.menuItems %}
{% partial __SELF__ ~ "::items" items=__SELF__.menuItems %}
</ul>
{% partial 'nesting/level2' %}

View File

@ -0,0 +1,3 @@
{% for item in items %}
<strong>{{ item }}</strong>
{% endfor %}

View File

@ -0,0 +1 @@
<p>Insert post here</p>

View File

@ -0,0 +1,3 @@
<p>I am another post, deep down</p>
{% partial 'nesting/level3' %}

View File

@ -22,6 +22,8 @@ class ComponentManagerTest extends TestCase
{
include_once base_path() . '/tests/fixtures/plugins/october/tester/components/Archive.php';
include_once base_path() . '/tests/fixtures/plugins/october/tester/components/Post.php';
include_once base_path() . '/tests/fixtures/plugins/october/tester/components/MainMenu.php';
include_once base_path() . '/tests/fixtures/plugins/october/tester/components/ContentBlock.php';
$manager = ComponentManager::instance();
$components = $manager->listComponentDetails();

View File

@ -5,6 +5,31 @@ use Cms\Classes\Controller;
class ControllerTest extends TestCase
{
public function testThemeUrl()
{
$theme = Theme::load('test');
$controller = new Controller($theme);
$url = $controller->themeUrl();
$this->assertEquals('http://localhost/themes/test', $url);
$url = $controller->themeUrl('foo/bar.css');
$this->assertEquals('http://localhost/themes/test/foo/bar.css', $url);
//
// These tests seem to bear different results
//
// $url = $controller->themeUrl(['assets/css/style1.css', 'assets/css/style2.css']);
// $url = substr($url, 0, strpos($url, '-'));
// $this->assertEquals('/combine/88634b8fa6f4f6442ce830d38296640a', $url);
// $url = $controller->themeUrl(['assets/js/script1.js', 'assets/js/script2.js']);
// $url = substr($url, 0, strpos($url, '-'));
// $this->assertEquals('/combine/860afc990164a60a8e90682d04da27ee', $url);
}
public function test404()
{
/*
@ -107,7 +132,7 @@ class ControllerTest extends TestCase
}
/**
* @expectedException Cms\Classes\CmsException
* @expectedException Twig_Error_Runtime
* @expectedExceptionMessage is not found
*/
public function testPartialNotFound()
@ -341,28 +366,99 @@ ESC;
$this->assertEquals('page', $content['ajax-result']);
}
public function testThemeUrl()
/**
* @expectedException October\Rain\Exception\SystemException
* @expectedExceptionMessage is not registered for the component
*/
public function testComponentClassNotFound()
{
$theme = Theme::load('test');
$controller = new Controller($theme);
$response = $controller->run('/no-component-class')->getContent();
}
$url = $controller->themeUrl();
$this->assertEquals('http://localhost/themes/test', $url);
$url = $controller->themeUrl('foo/bar.css');
$this->assertEquals('http://localhost/themes/test/foo/bar.css', $url);
public function testComponentNotFound()
{
//
// These tests seem to bear different results
// This test should probably be throwing an exception... -sg
//
$theme = Theme::load('test');
$controller = new Controller($theme);
$response = $controller->run('/no-component')->getContent();
// $url = $controller->themeUrl(['assets/css/style1.css', 'assets/css/style2.css']);
// $url = substr($url, 0, strpos($url, '-'));
// $this->assertEquals('/combine/88634b8fa6f4f6442ce830d38296640a', $url);
$this->assertEquals('<p>Hey</p>', $response);
}
// $url = $controller->themeUrl(['assets/js/script1.js', 'assets/js/script2.js']);
// $url = substr($url, 0, strpos($url, '-'));
// $this->assertEquals('/combine/860afc990164a60a8e90682d04da27ee', $url);
public function testComponentPartial()
{
$theme = Theme::load('test');
$controller = new Controller($theme);
$response = $controller->run('/component-partial')->getContent();
$this->assertEquals('<p>DEFAULT MARKUP: I am a post yay</p>', $response);
}
public function testComponentPartialOverride()
{
$theme = Theme::load('test');
$controller = new Controller($theme);
$response = $controller->run('/component-partial-override')->getContent();
$this->assertEquals('<p>I am an override partial! Yay</p>', $response);
}
public function testComponentPartialNesting()
{
$theme = Theme::load('test');
$controller = new Controller($theme);
$response = $controller->run('/component-partial-nesting')->getContent();
$content = <<<ESC
<h1>Level 1</h1>
<ul>
<strong>Home</strong>
<strong>Blog</strong>
<strong>About</strong>
<strong>Contact</strong>
<strong>Home</strong>
<strong>Blog</strong>
<strong>About</strong>
<strong>Contact</strong>
<strong>Home</strong>
<strong>Blog</strong>
<strong>About</strong>
<strong>Contact</strong>
</ul>
<h1>Level 2</h1>
<p>DEFAULT MARKUP: I am a post yay</p><p>I am another post, deep down</p>
<h1>Level 3</h1>
<h4>DEFAULT MARKUP: Menu</h4>
<ul>
<li>DEFAULT: Home</li>
<li>DEFAULT: Blog</li>
<li>DEFAULT: About</li>
<li>DEFAULT: Contact</li>
</ul>
<p>Insert post here</p>
ESC;
$this->assertEquals($content, $response);
}
public function testComponentWithOnRender()
{
$theme = Theme::load('test');
$controller = new Controller($theme);
$response = $controller->run('/component-custom-render')->getContent();
$content = <<<ESC
Pass
Custom output: Would you look over Picasso's shoulder
Custom output: And tell him about his brush strokes?
ESC;
$this->assertEquals($content, $response);
}
}

View File

@ -0,0 +1,59 @@
<?php
use Cms\Classes\PartialStack;
class PartialStackTest extends TestCase
{
public function testStackPartials()
{
$stack = new PartialStack;
/*
* Stack em up
*/
$stack->stackPartial();
$stack->addComponent('override1', 'October\Tester\Components\MainMenu');
$stack->addComponent('override2', 'October\Tester\Components\ContentBlock');
$stack->stackPartial();
$stack->addComponent('override3', 'October\Tester\Components\Post');
$stack->addComponent('post', 'October\Tester\Components\Post');
$stack->stackPartial();
$stack->addComponent('mainMenu', 'October\Tester\Components\MainMenu');
/*
* Knock em down
*/
$this->assertEquals('October\Tester\Components\MainMenu', $stack->getComponent('mainMenu'));
$this->assertEquals('October\Tester\Components\MainMenu', $stack->getComponent('override1'));
$stack->unstackPartial();
$this->assertNull($stack->getComponent('mainMenu'));
$this->assertEquals('October\Tester\Components\ContentBlock', $stack->getComponent('override2'));
$this->assertEquals('October\Tester\Components\Post', $stack->getComponent('override3'));
$stack->unstackPartial();
$this->assertNull($stack->getComponent('mainMenu'));
$this->assertNull($stack->getComponent('post'));
$this->assertEquals('October\Tester\Components\MainMenu', $stack->getComponent('override1'));
$stack->unstackPartial();
$this->assertNull($stack->getComponent('post'));
$this->assertNull($stack->getComponent('mainMenu'));
$this->assertNull($stack->getComponent('override1'));
$this->assertNull($stack->getComponent('override2'));
$this->assertNull($stack->getComponent('override3'));
}
public function testEmptyStack()
{
$stack = new PartialStack;
$this->assertNull($stack->getComponent('xxx'));
}
}