public function testOnAuthorSetsAuthorsInTwigEnvironment()
 {
     $author = new Author('test');
     $authors = new AuthorRegistry();
     $authors->addAuthor($author);
     $generator = $this->getMock('Carew\\Plugin\\Authors\\Generator\\AuthorIndexGenerator', array(), array(), '', false);
     $generator->expects($this->any())->method('generateAuthorIndices')->will($this->returnValue(array()));
     $twig = $this->getMock('Twig_Environment');
     $subscriber = new AuthorsEventSubscriber($authors, $generator, $twig);
     $twig->expects($this->once())->method('addGlobal')->with('authors', array($author));
     $subscriber->onAuthors(new CarewEvent());
 }
 /**
  * @param Carew $carew
  */
 public function register(Carew $carew)
 {
     $container = $carew->getContainer();
     $registry = new AuthorRegistry();
     $eventDispatcher = $carew->getEventDispatcher();
     $config = $container->offsetGet('config');
     if (isset($config['authors'])) {
         foreach ($config['authors'] as $handle => $attributes) {
             $registry->addAuthor(new Author($handle, $attributes));
         }
     }
     $eventDispatcher->addSubscriber(new IndexesEventSubscriber($registry));
     $eventDispatcher->addSubscriber(new AuthorsEventSubscriber($registry, new AuthorIndexGenerator($container['base_dir'], new Finder()), $container->offsetGet('twig')));
 }
 /**
  * @dataProvider getContentTypes
  *
  * @param string $contentType
  */
 public function testOnIndexesAddsDocumentsToAuthors($contentType)
 {
     $author = new Author('seiffert');
     $authors = new AuthorRegistry();
     $authors->addAuthor($author);
     $subscriber = new IndexesEventSubscriber($authors);
     $document = new Document();
     $document->setMetadatas(array('author' => 'seiffert'));
     $index = new Document();
     $index->setVars(array($contentType => array($document)));
     $subscriber->onIndexes($this->createEvent(array($index)));
     $document->setMetadatas(array('author' => $author), true);
     $this->assertSame(array($document), $author->getDocuments());
 }
 /**
  * @param CarewEvent $event
  */
 public function onIndexes(CarewEvent $event)
 {
     /** @var array|Document[] $index */
     $indexes = $event->getSubject();
     $docs = $this->getDocumentsFromIndexes($indexes);
     foreach ($docs as $doc) {
         $data = $doc->getMetadatas();
         if (isset($data['author'])) {
             $author = $this->authors->getAuthor($data['author']);
             $author->addDocument($doc);
             $data['author'] = $author;
             $doc->setMetadatas($data);
         }
     }
     $authorsEvent = new CarewEvent(array());
     $event->getDispatcher()->dispatch(AuthorEvents::AUTHORS, $authorsEvent);
     $event->setSubject(array_merge($indexes, $authorsEvent->getSubject()));
 }
 /**
  * @expectedException \InvalidArgumentException
  */
 public function testGetUnkownAuthor()
 {
     $this->registry->getAuthor('test');
 }