Created
January 13, 2014 16:15
-
-
Save wouterj/8403005 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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)) { | |
| }*/ | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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