Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
Updated Zepto\FileLoader\MarkdownLoader to use dependency injecti…
Browse files Browse the repository at this point in the history
…on and related test class
  • Loading branch information
hassankhan committed Jan 24, 2014
1 parent e599e1b commit b47e8ec
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 45 deletions.
31 changes: 23 additions & 8 deletions library/Zepto/FileLoader/MarkdownLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
{
Expand Down Expand Up @@ -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);
}

}
89 changes: 52 additions & 37 deletions tests/Zepto/FileLoader/MarkdownLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<h1>Error 404</h1>" . PHP_EOL. PHP_EOL
. "<p>Woops. Looks like this page doesn't exist.</p>". 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' => '<h1>Error 404</h1>' . PHP_EOL. PHP_EOL
. '<p>Woops. Looks like this page doesn\'t exist.</p>'. 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);
}

Expand All @@ -62,29 +64,42 @@ public function testLoadSingleFile()
*/
public function testLoadMultipleFiles()
{
$done_text_1 = "<h2>This is a Sub Page Index</h2>" . PHP_EOL . PHP_EOL
. "<p>This is index.md in the 'sub' folder.</p>" . PHP_EOL . PHP_EOL
. "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>" . PHP_EOL . PHP_EOL
. "<p>Donec ultricies tristique nulla et mattis.</p>" . PHP_EOL. PHP_EOL
. "<p>Phasellus id massa eget nisl congue blandit sit amet id ligula.</p>" . PHP_EOL;

$done_text_2 = "<h2>This is a Sub Page</h2>" . PHP_EOL . PHP_EOL
. "<p>This is page.md in the 'sub' folder.</p>" . PHP_EOL . PHP_EOL
. "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>" . PHP_EOL . PHP_EOL
. "<p>Donec ultricies tristique nulla et mattis.</p>" . PHP_EOL . PHP_EOL
. "<p>Phasellus id massa eget nisl congue blandit sit amet id ligula.</p>". 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' => '<h2>This is a Sub Page Index</h2>' . PHP_EOL. PHP_EOL
. '<p>This is index.md in the "sub" folder.</p>' . PHP_EOL. PHP_EOL
. '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>' . PHP_EOL. PHP_EOL
. '<p>Donec ultricies tristique nulla et mattis.</p>' . PHP_EOL. PHP_EOL
. '<p>Phasellus id massa eget nisl congue blandit sit amet id ligula.</p>'. PHP_EOL
'content' => $done_text_1
);

$files['sub/page.md'] = array(
'meta' => array(
'title' => 'Sub Page'
),
'content' => '<h2>This is a Sub Page</h2>' . PHP_EOL. PHP_EOL
. '<p>This is page.md in the "sub" folder.</p>' . PHP_EOL. PHP_EOL
. '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>' . PHP_EOL. PHP_EOL
. '<p>Donec ultricies tristique nulla et mattis.</p>' . PHP_EOL. PHP_EOL
. '<p>Phasellus id massa eget nisl congue blandit sit amet id ligula.</p>'. 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);
}

Expand Down

0 comments on commit b47e8ec

Please sign in to comment.