Skip to content

Instantly share code, notes, and snippets.

@wouterj
Created January 13, 2014 16:15
Show Gist options
  • Select an option

  • Save wouterj/8403005 to your computer and use it in GitHub Desktop.

Select an option

Save wouterj/8403005 to your computer and use it in GitHub Desktop.
<?php
namespace Wj\Stoffer;
use Wj\Stoffer\Event\LineEvent;
use Wj\Stoffer\Scissors\ScissorsInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* @author Wouter J <wouter@wouterj.nl>
*/
class Kernel
{
/**
* @var ScissorsInterface
*/
private $scissors;
/**
* @var EventDispatcherInterface
*/
private $dispatcher;
/**
* @param ScissorsInterface $scissors
* @param EventDispatcherInterface $dispatcher
*/
public function __construct($scissors, $dispatcher)
{
$this->setScissors($scissors);
$this->setDispatcher($dispatcher);
}
public function parse($rst)
{
$this->getScissors()->setInput($rst);
$errors = array();
foreach ($this->getScissors() as $lineNr => $line) {
var_dump($line);
$event = new LineEvent($line);
$event->setParameter('line_number', $lineNr);
$this->dispatcher->dispatch(LineEvent::NAME, $event);
if (0 < count($event->getErrors())) {
foreach ($event->getErrors() as $error) {
$errors[] = $error;
}
}
}
return $errors;
}
public function setScissors(ScissorsInterface $scissors)
{
$this->scissors = $scissors;
}
/**
* @return ScissorsInterface
*/
public function getScissors()
{
return $this->scissors;
}
/**
* @return EventDispatcherInterface
*/
protected function getDispatcher()
{
return $this->dispatcher;
}
private function setDispatcher(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
/* if (!$dispatcher->hasListeners(LineEvent::NAME)) {
}*/
}
}
<?php
namespace spec\Wj\Stoffer;
use Wj\Stoffer\Event\LineEvent;
use Wj\Stoffer\Scissors\ScissorsInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class KernelSpec extends ObjectBehavior
{
function let(ScissorsInterface $scissors, EventDispatcherInterface $dispatcher)
{
$this->beConstructedWith($scissors, $dispatcher);
}
function it_lints_rst(ScissorsInterface $scissors, EventDispatcherInterface $dispatcher)
{
$rst = <<<RST
Headline
========
Some *sample* text.
RST;
$scissors->setInput(Argument::exact($rst))->shouldBeCalled();
$returns = array(
"Headline\n",
"========\n",
"\n",
"Some *sample* text.",
);
$scissors->current()->willReturn(current($returns));
$scissors->next()->will(function () use (&$returns) {
$this->current()->willReturn(next($returns));
});
$scissors->valid()->willReturn(function () use (&$returns) {
return false !== current($returns);
});
$dispatcher->dispatch(
Argument::exact(LineEvent::NAME),
Argument::type('Wj\Stoffer\Event\LineEvent')
)->shouldBeCalledTimes(4);
$this->parse($rst);
}
<?php
namespace Wj\Stoffer\Scissors;
/**
* @author Wouter J <wouter@wouterj.nl>
*/
interface ScissorsInterface extends \Iterator
{
/**
* Returns all snippets.
*
* @return array
*/
public function getSnippets();
public function setInput($str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment