diff --git a/library/Zepto/FileLoader/MarkdownLoader.php b/library/Zepto/FileLoader/MarkdownLoader.php index f086d9b..5af5600 100644 --- a/library/Zepto/FileLoader/MarkdownLoader.php +++ b/library/Zepto/FileLoader/MarkdownLoader.php @@ -11,10 +11,18 @@ namespace Zepto\FileLoader; -use \Michelf\MarkdownExtra; - class MarkdownLoader extends \Zepto\FileLoader { + /** + * An object which parses Markdown to HTML + * @var Michelf\MarkdownInterface + */ + protected $parser; + + function __construct(\Michelf\MarkdownInterface $parser) { + $this->parser = $parser; + } + /** * Basically does the same job as the superclass, except this time we run * it through a post_process() method to work some magic on it @@ -29,12 +37,19 @@ public function load($file_path, $file_extension) } /** - * Where the magic happens, ladies and gentlemen + * Returns the parser object + * @return Michelf\MarkdownInterface + */ + public function get_parser() + { + return $this->parser; + } + + /** + * Where the magic happens, ladies and gentlemen. An array with the keys set to the name of + * the file and the values set to the processed Markdown text * @codeCoverageIgnore - * @param Parsedown $processor Instance of Parsedown - * @return array An array with the keys set to the name of - * the file and the values set to the processed - * Markdown text + * @return array */ private function post_process() { @@ -85,7 +100,7 @@ private function parse_meta($file) private function parse_content($file) { $content = preg_replace('#/\*.+?\*/#s', '', $file); - return MarkdownExtra::defaultTransform($content); + return $this->parser->defaultTransform($content); } } diff --git a/tests/Zepto/FileLoader/MarkdownLoaderTest.php b/tests/Zepto/FileLoader/MarkdownLoaderTest.php index 26d0047..fe1c916 100644 --- a/tests/Zepto/FileLoader/MarkdownLoaderTest.php +++ b/tests/Zepto/FileLoader/MarkdownLoaderTest.php @@ -7,53 +7,55 @@ class MarkdownLoaderTest extends \PHPUnit_Framework_TestCase { /** - * @var MarkdownLoader + * @covers Zepto\FileLoader\MarkdownLoader::__construct() + * @covers Zepto\FileLoader\MarkdownLoader::get_parser() */ - protected $object; - - /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. - */ - protected function setUp() + public function testConstructWithMarkdown() { - $this->object = new MarkdownLoader(); - } + $parser = $this->getMock('Michelf\Markdown', array('defaultTransform')); + $loader = new MarkdownLoader($parser); - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - $this->object = null; + $this->assertInstanceOf('Michelf\Markdown', $loader->get_parser()); } /** - * @covers Zepto\FileLoader\MarkdownLoader::load() - * @expectedException Exception + * @covers Zepto\FileLoader\MarkdownLoader::__construct() + * @covers Zepto\FileLoader\MarkdownLoader::get_parser() */ - public function testLoad() + public function testConstructWithMarkdownExtra() { - $this->object->load('@£@', 'aa'); + $parser = $this->getMock('Michelf\MarkdownExtra', array('defaultTransform')); + $loader = new MarkdownLoader($parser); + + $this->assertInstanceOf('Michelf\MarkdownExtra', $loader->get_parser()); } /** * @covers Zepto\FileLoader\MarkdownLoader::load - * @todo Implement testLoad(). */ - public function testLoadSingleFile() + public function testLoad() { + $parsed_text = "

Error 404

" . PHP_EOL. PHP_EOL + . "

Woops. Looks like this page doesn't exist.

". PHP_EOL; + + // Create a stub for the SomeClass class. + $parser = $this->getMock('Michelf\MarkdownInterface', array('defaultTransform', 'transform')); + + $parser::staticExpects($this->any()) + ->method('defaultTransform') + ->will($this->returnValue($parsed_text)); + + $loader = new MarkdownLoader($parser); + $files['404.md'] = array( 'meta' => array( 'title' => 'Error 404', 'robots' => 'noindex,nofollow' ), - 'content' => '

Error 404

' . PHP_EOL. PHP_EOL - . '

Woops. Looks like this page doesn\'t exist.

'. PHP_EOL + 'content' => $parsed_text ); - $result = $this->object->load(ROOT_DIR . 'content/404.md', array('md')); + $result = $loader->load(ROOT_DIR . 'content/404.md', array('md')); $this->assertEquals($files, $result); } @@ -62,29 +64,42 @@ public function testLoadSingleFile() */ public function testLoadMultipleFiles() { + $done_text_1 = "

This is a Sub Page Index

" . PHP_EOL . PHP_EOL + . "

This is index.md in the 'sub' folder.

" . PHP_EOL . PHP_EOL + . "

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

" . PHP_EOL . PHP_EOL + . "

Donec ultricies tristique nulla et mattis.

" . PHP_EOL. PHP_EOL + . "

Phasellus id massa eget nisl congue blandit sit amet id ligula.

" . PHP_EOL; + + $done_text_2 = "

This is a Sub Page

" . PHP_EOL . PHP_EOL + . "

This is page.md in the 'sub' folder.

" . PHP_EOL . PHP_EOL + . "

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

" . PHP_EOL . PHP_EOL + . "

Donec ultricies tristique nulla et mattis.

" . PHP_EOL . PHP_EOL + . "

Phasellus id massa eget nisl congue blandit sit amet id ligula.

". PHP_EOL; + + // Create a stub for the SomeClass class. + $parser = $this->getMock('Michelf\MarkdownInterface', array('defaultTransform', 'transform')); + + $parser::staticExpects($this->any()) + ->method('defaultTransform') + ->will($this->onConsecutiveCalls($done_text_1, $done_text_2)); + + $loader = new MarkdownLoader($parser); + $files['sub/index.md'] = array( 'meta' => array( 'title' => 'Sub Page Index' ), - 'content' => '

This is a Sub Page Index

' . PHP_EOL. PHP_EOL - . '

This is index.md in the "sub" folder.

' . PHP_EOL. PHP_EOL - . '

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

' . PHP_EOL. PHP_EOL - . '

Donec ultricies tristique nulla et mattis.

' . PHP_EOL. PHP_EOL - . '

Phasellus id massa eget nisl congue blandit sit amet id ligula.

'. PHP_EOL + 'content' => $done_text_1 ); $files['sub/page.md'] = array( 'meta' => array( 'title' => 'Sub Page' ), - 'content' => '

This is a Sub Page

' . PHP_EOL. PHP_EOL - . '

This is page.md in the "sub" folder.

' . PHP_EOL. PHP_EOL - . '

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

' . PHP_EOL. PHP_EOL - . '

Donec ultricies tristique nulla et mattis.

' . PHP_EOL. PHP_EOL - . '

Phasellus id massa eget nisl congue blandit sit amet id ligula.

'. PHP_EOL + 'content' => $done_text_2 ); - $result = $this->object->load(ROOT_DIR . 'content/sub', array('md')); + $result = $loader->load(ROOT_DIR . 'content/sub', array('md')); $this->assertEquals($files, $result); }