You are here

class CsvParserTest in Feeds 8.3

Same name in this branch
  1. 8.3 tests/src/Unit/Component/CsvParserTest.php \Drupal\Tests\feeds\Unit\Component\CsvParserTest
  2. 8.3 tests/src/FunctionalJavascript/Feeds/Parser/CsvParserTest.php \Drupal\Tests\feeds\FunctionalJavascript\Feeds\Parser\CsvParserTest
  3. 8.3 tests/src/Unit/Feeds/Parser/CsvParserTest.php \Drupal\Tests\feeds\Unit\Feeds\Parser\CsvParserTest
  4. 8.3 tests/src/Kernel/Feeds/Parser/CsvParserTest.php \Drupal\Tests\feeds\Kernel\Feeds\Parser\CsvParserTest

@coversDefaultClass \Drupal\feeds\Feeds\Parser\CsvParser @group feeds

Hierarchy

Expanded class hierarchy of CsvParserTest

File

tests/src/Unit/Feeds/Parser/CsvParserTest.php, line 18

Namespace

Drupal\Tests\feeds\Unit\Feeds\Parser
View source
class CsvParserTest extends FeedsUnitTestCase {

  /**
   * The Feeds parser plugin under test.
   *
   * @var \Drupal\feeds\Feeds\Parser\CsvParser
   */
  protected $parser;

  /**
   * The feed type entity.
   *
   * @var \Drupal\feeds\FeedTypeInterface
   */
  protected $feedType;

  /**
   * The feed entity.
   *
   * @var \Drupal\feeds\FeedInterface
   */
  protected $feed;

  /**
   * The state object.
   *
   * @var \Drupal\feeds\StateInterface
   */
  protected $state;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->feedType = $this
      ->createMock(FeedTypeInterface::class);
    $configuration = [
      'feed_type' => $this->feedType,
      'line_limit' => 3,
    ];
    $this->parser = new CsvParser($configuration, 'csv', []);
    $this->parser
      ->setStringTranslation($this
      ->getStringTranslationStub());
    $this->state = new State();
    $this->feed = $this
      ->createMock(FeedInterface::class);
    $this->feed
      ->expects($this
      ->any())
      ->method('getType')
      ->will($this
      ->returnValue($this->feedType));
  }

  /**
   * Tests parsing a CSV file that succeeds.
   *
   * @covers ::parse
   */
  public function testParse() {
    $this->feedType
      ->method('getMappingSources')
      ->will($this
      ->returnValue([]));
    $this->feed
      ->expects($this
      ->any())
      ->method('getConfigurationFor')
      ->with($this->parser)
      ->will($this
      ->returnValue($this->parser
      ->defaultFeedConfiguration()));
    $file = $this
      ->resourcesPath() . '/csv/example.csv';
    $fetcher_result = new FetcherResult($file);
    $result = $this->parser
      ->parse($this->feed, $fetcher_result, $this->state);
    $this
      ->assertSame(count($result), 3);
    $this
      ->assertSame($result[0]
      ->get('Header A'), '"1"');

    // Parse again. Tests batching.
    $result = $this->parser
      ->parse($this->feed, $fetcher_result, $this->state);
    $this
      ->assertSame(count($result), 3);
    $this
      ->assertSame($result[0]
      ->get('Header B'), "new\r\nline 2");
  }

  /**
   * Tests parsing with the "no_headers" option enabled.
   */
  public function testParseWithoutHeaders() {

    // Enable "no_headers" option.
    $config = [
      'no_headers' => TRUE,
    ] + $this->parser
      ->defaultFeedConfiguration();
    $this->feed
      ->expects($this
      ->any())
      ->method('getConfigurationFor')
      ->with($this->parser)
      ->will($this
      ->returnValue($config));

    // Provide mapping sources.
    $this->feedType
      ->method('getMappingSources')
      ->will($this
      ->returnValue([
      'column1' => [
        'label' => 'Column 1',
        'value' => 0,
        'machine_name' => 'column1',
      ],
      'column2' => [
        'label' => 'Column 2',
        'value' => 1,
        'machine_name' => 'column2',
      ],
    ]));
    $file = $this
      ->resourcesPath() . '/csv/content.csv';
    $fetcher_result = new FetcherResult($file);
    $result = $this->parser
      ->parse($this->feed, $fetcher_result, $this->state);

    // Assert that there are three items.
    $this
      ->assertSame(count($result), 3);

    // Assert that each item has the expected value on the machine name.
    $this
      ->assertSame('guid', $result[0]
      ->get('column1'));
    $this
      ->assertSame('title', $result[0]
      ->get('column2'));
    $this
      ->assertSame('1', $result[1]
      ->get('column1'));
    $this
      ->assertSame('Lorem ipsum', $result[1]
      ->get('column2'));
    $this
      ->assertSame('2', $result[2]
      ->get('column1'));
    $this
      ->assertSame('Ut wisi enim ad minim veniam', $result[2]
      ->get('column2'));
  }

  /**
   * Tests parsing an empty CSV file.
   *
   * @covers ::parse
   */
  public function testEmptyFeed() {
    $this->feedType
      ->method('getMappingSources')
      ->will($this
      ->returnValue([]));
    touch('vfs://feeds/empty_file');
    $result = new FetcherResult('vfs://feeds/empty_file');
    $this
      ->expectException(EmptyFeedException::class);
    $this->parser
      ->parse($this->feed, $result, $this->state);
  }

  /**
   * Tests parsing a file with a few extra blank lines.
   */
  public function testFeedWithExtraBlankLines() {
    $this->feedType
      ->method('getMappingSources')
      ->will($this
      ->returnValue([]));

    // Set an high line limit.
    $configuration = [
      'feed_type' => $this->feedType,
      'line_limit' => 100,
    ];
    $this->parser = new CsvParser($configuration, 'csv', []);
    $this->parser
      ->setStringTranslation($this
      ->getStringTranslationStub());
    $this->feed
      ->expects($this
      ->any())
      ->method('getConfigurationFor')
      ->with($this->parser)
      ->will($this
      ->returnValue($this->parser
      ->defaultFeedConfiguration()));
    $file = $this
      ->resourcesPath() . '/csv/with-empty-lines.csv';
    $fetcher_result = new FetcherResult($file);
    $result = $this->parser
      ->parse($this->feed, $fetcher_result, $this->state);
    $this
      ->assertCount(9, $result);

    // Parse again.
    $result = $this->parser
      ->parse($this->feed, $fetcher_result, $this->state);
    $this
      ->assertCount(0, $result);

    // Assert that parsing has finished.
    $this
      ->assertEquals(StateInterface::BATCH_COMPLETE, $this->state->progress);
  }

  /**
   * @covers ::getMappingSources
   */
  public function testGetMappingSources() {

    // Not really much to test here.
    $this
      ->assertSame([], $this->parser
      ->getMappingSources());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CsvParserTest::$feed protected property The feed entity.
CsvParserTest::$feedType protected property The feed type entity.
CsvParserTest::$parser protected property The Feeds parser plugin under test.
CsvParserTest::$state protected property The state object.
CsvParserTest::setUp public function Overrides FeedsUnitTestCase::setUp
CsvParserTest::testEmptyFeed public function Tests parsing an empty CSV file.
CsvParserTest::testFeedWithExtraBlankLines public function Tests parsing a file with a few extra blank lines.
CsvParserTest::testGetMappingSources public function @covers ::getMappingSources
CsvParserTest::testParse public function Tests parsing a CSV file that succeeds.
CsvParserTest::testParseWithoutHeaders public function Tests parsing with the "no_headers" option enabled.
FeedsMockingTrait::getMockAccount protected function Mocks an account object.
FeedsMockingTrait::getMockedAccountSwitcher protected function Returns a mocked AccountSwitcher object.
FeedsMockingTrait::getMockFeed protected function Returns a mocked feed entity.
FeedsMockingTrait::getMockFeedType protected function Returns a mocked feed type entity.
FeedsMockingTrait::getMockFieldDefinition protected function Mocks a field definition. 1
FeedsMockingTrait::getMockFileSystem protected function Mocks the file system.
FeedsReflectionTrait::callProtectedMethod protected function Calls a protected method on the given object.
FeedsReflectionTrait::getMethod protected function Gets a ReflectionMethod for a class method.
FeedsReflectionTrait::getProtectedClosure protected function Returns a dynamically created closure for the object's method.
FeedsReflectionTrait::setProtectedProperty protected function Sets a protected property.
FeedsUnitTestCase::absolutePath protected function Returns the absolute directory path of the Feeds module.
FeedsUnitTestCase::defineConstants protected function Defines stub constants.
FeedsUnitTestCase::getMockStreamWrapperManager protected function Returns a mock stream wrapper manager.
FeedsUnitTestCase::resourcesPath protected function Returns the absolute directory path of the resources folder.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.