You are here

public function LazySubscriberTest::testOnInitImport in Feeds 8.3

@covers ::onInitImport

File

tests/src/Unit/EventSubscriber/LazySubscriberTest.php, line 125

Class

LazySubscriberTest
@coversDefaultClass \Drupal\feeds\EventSubscriber\LazySubscriber @group feeds

Namespace

Drupal\Tests\feeds\Unit\EventSubscriber

Code

public function testOnInitImport() {
  $fetcher_result = $this
    ->createMock('Drupal\\feeds\\Result\\FetcherResultInterface');
  $parser_result = new ParserResult();
  $parser_result
    ->addItem(new DynamicItem());
  $this->fetcher
    ->expects($this
    ->once())
    ->method('fetch')
    ->with($this->feed, $this->state)
    ->will($this
    ->returnValue($fetcher_result));
  $this->parser
    ->expects($this
    ->once())
    ->method('parse')
    ->with($this->feed, $fetcher_result, $this->state)
    ->will($this
    ->returnValue($parser_result));
  $this->processor
    ->expects($this
    ->once())
    ->method('process');
  $this->feedType
    ->expects($this
    ->once())
    ->method('getFetcher')
    ->will($this
    ->returnValue($this->fetcher));
  $this->feedType
    ->expects($this
    ->once())
    ->method('getParser')
    ->will($this
    ->returnValue($this->parser));
  $this->feedType
    ->expects($this
    ->once())
    ->method('getProcessor')
    ->will($this
    ->returnValue($this->processor));
  $subscriber = new LazySubscriber();

  // Fetch.
  $subscriber
    ->onInitImport(new InitEvent($this->feed, 'fetch'), FeedsEvents::INIT_IMPORT, $this->dispatcher);
  $fetch_event = $this->dispatcher
    ->dispatch(FeedsEvents::FETCH, new FetchEvent($this->feed));
  $this
    ->assertSame($fetcher_result, $fetch_event
    ->getFetcherResult());

  // Parse.
  $subscriber
    ->onInitImport(new InitEvent($this->feed, 'parse'), FeedsEvents::INIT_IMPORT, $this->dispatcher);
  $parse_event = $this->dispatcher
    ->dispatch(FeedsEvents::PARSE, new ParseEvent($this->feed, $fetcher_result));
  $this
    ->assertSame($parser_result, $parse_event
    ->getParserResult());

  // Process.
  $subscriber
    ->onInitImport(new InitEvent($this->feed, 'process'), FeedsEvents::INIT_IMPORT, $this->dispatcher);
  foreach ($parse_event
    ->getParserResult() as $item) {
    $this->dispatcher
      ->dispatch(FeedsEvents::PROCESS, new ProcessEvent($this->feed, $item));
  }

  // Call again.
  $subscriber
    ->onInitImport(new InitEvent($this->feed, 'fetch'), FeedsEvents::INIT_IMPORT, $this->explodingDispatcher);
}