You are here

class DirectoryFetcherTest in Feeds 8.3

Same name in this branch
  1. 8.3 tests/src/Unit/Feeds/Fetcher/DirectoryFetcherTest.php \Drupal\Tests\feeds\Unit\Feeds\Fetcher\DirectoryFetcherTest
  2. 8.3 tests/src/Kernel/Feeds/Fetcher/DirectoryFetcherTest.php \Drupal\Tests\feeds\Kernel\Feeds\Fetcher\DirectoryFetcherTest

@coversDefaultClass \Drupal\feeds\Feeds\Fetcher\DirectoryFetcher @group feeds

Hierarchy

Expanded class hierarchy of DirectoryFetcherTest

File

tests/src/Unit/Feeds/Fetcher/DirectoryFetcherTest.php, line 16

Namespace

Drupal\Tests\feeds\Unit\Feeds\Fetcher
View source
class DirectoryFetcherTest extends FeedsUnitTestCase {

  /**
   * The Feeds fetcher plugin under test.
   *
   * @var \Drupal\feeds\Feeds\Fetcher\DirectoryFetcher
   */
  protected $fetcher;

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

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

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $feed_type = $this
      ->createMock('Drupal\\feeds\\FeedTypeInterface');
    $container = new ContainerBuilder();
    $container
      ->set('stream_wrapper_manager', $this
      ->getMockStreamWrapperManager());
    $this->fetcher = new DirectoryFetcher([
      'feed_type' => $feed_type,
    ], 'directory', []);
    $this->fetcher
      ->setStringTranslation($this
      ->getStringTranslationStub());
    $this->state = new State();
    $this->feed = $this
      ->createMock('Drupal\\feeds\\FeedInterface');
    $this->feed
      ->expects($this
      ->any())
      ->method('getSource')
      ->will($this
      ->returnValue('vfs://feeds'));

    // Prepare filesystem.
    touch('vfs://feeds/test_file_1.txt');
    touch('vfs://feeds/test_file_2.txt');
    touch('vfs://feeds/test_file_3.txt');
    touch('vfs://feeds/test_file_3.mp3');
    chmod('vfs://feeds/test_file_3.txt', 0333);
    mkdir('vfs://feeds/subdir');
    touch('vfs://feeds/subdir/test_file_4.txt');
    touch('vfs://feeds/subdir/test_file_4.mp3');
  }

  /**
   * Tests fetching a file.
   *
   * @covers ::fetch
   */
  public function testFetchFile() {
    $feed = $this
      ->createMock('Drupal\\feeds\\FeedInterface');
    $feed
      ->expects($this
      ->any())
      ->method('getSource')
      ->will($this
      ->returnValue('vfs://feeds/test_file_1.txt'));
    $result = $this->fetcher
      ->fetch($feed, $this->state);
    $this
      ->assertSame('vfs://feeds/test_file_1.txt', $result
      ->getFilePath());
  }

  /**
   * Tests fetching from a directory on which we don't have read permissions.
   *
   * @covers ::fetch
   */
  public function testFetchDir() {
    $result = $this->fetcher
      ->fetch($this->feed, $this->state);
    $this
      ->assertSame($this->state->total, 2);
    $this
      ->assertSame('vfs://feeds/test_file_1.txt', $result
      ->getFilePath());
    $this
      ->assertSame('vfs://feeds/test_file_2.txt', $this->fetcher
      ->fetch($this->feed, $this->state)
      ->getFilePath());
    chmod('vfs://feeds', 0333);
    $this
      ->expectException(RuntimeException::class);
    $result = $this->fetcher
      ->fetch($this->feed, $this->state);
  }

  /**
   * Tests fetching a directory resursively.
   *
   * @covers ::fetch
   */
  public function testRecursiveFetchDir() {
    $this->fetcher
      ->setConfiguration([
      'recursive_scan' => TRUE,
    ]);
    $result = $this->fetcher
      ->fetch($this->feed, $this->state);
    $this
      ->assertSame($this->state->total, 3);
    $this
      ->assertSame('vfs://feeds/test_file_1.txt', $result
      ->getFilePath());
    $this
      ->assertSame('vfs://feeds/test_file_2.txt', $this->fetcher
      ->fetch($this->feed, $this->state)
      ->getFilePath());
    $this
      ->assertSame('vfs://feeds/subdir/test_file_4.txt', $this->fetcher
      ->fetch($this->feed, $this->state)
      ->getFilePath());
  }

  /**
   * Tests fetching an empty directory.
   *
   * @covers ::fetch
   */
  public function testEmptyDirectory() {
    mkdir('vfs://feeds/emptydir');
    $feed = $this
      ->createMock('Drupal\\feeds\\FeedInterface');
    $feed
      ->expects($this
      ->any())
      ->method('getSource')
      ->will($this
      ->returnValue('vfs://feeds/emptydir'));
    $this
      ->expectException(EmptyFeedException::class);
    $result = $this->fetcher
      ->fetch($feed, $this->state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DirectoryFetcherTest::$feed protected property The feed entity.
DirectoryFetcherTest::$fetcher protected property The Feeds fetcher plugin under test.
DirectoryFetcherTest::$state protected property The state object.
DirectoryFetcherTest::setUp public function Overrides FeedsUnitTestCase::setUp
DirectoryFetcherTest::testEmptyDirectory public function Tests fetching an empty directory.
DirectoryFetcherTest::testFetchDir public function Tests fetching from a directory on which we don't have read permissions.
DirectoryFetcherTest::testFetchFile public function Tests fetching a file.
DirectoryFetcherTest::testRecursiveFetchDir public function Tests fetching a directory resursively.
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.