You are here

class FileTest in Feeds 8.3

Same name in this branch
  1. 8.3 tests/src/Functional/Feeds/Target/FileTest.php \Drupal\Tests\feeds\Functional\Feeds\Target\FileTest
  2. 8.3 tests/src/Unit/Feeds/Target/FileTest.php \Drupal\Tests\feeds\Unit\Feeds\Target\FileTest
  3. 8.3 tests/src/Kernel/Feeds/Target/FileTest.php \Drupal\Tests\feeds\Kernel\Feeds\Target\FileTest

@coversDefaultClass \Drupal\feeds\Feeds\Target\File @group feeds

Hierarchy

Expanded class hierarchy of FileTest

File

tests/src/Unit/Feeds/Target/FileTest.php, line 19

Namespace

Drupal\Tests\feeds\Unit\Feeds\Target
View source
class FileTest extends FieldTargetTestBase {

  /**
   * The entity type manager prophecy used in the test.
   *
   * @var \Prophecy\Prophecy\ProphecyInterface|\Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The http client prophecy used in the test.
   *
   * @var \Prophecy\Prophecy\ProphecyInterface|\GuzzleHttp\ClientInterface
   */
  protected $client;

  /**
   * Token service.
   *
   * @var \Prophecy\Prophecy\ProphecyInterface|\Drupal\Core\Utility\Token
   */
  protected $token;

  /**
   * The file and stream wrapper helper.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * The Feeds entity finder service.
   *
   * @var \Drupal\feeds\EntityFinderInterface
   */
  protected $entityFinder;

  /**
   * The FeedsTarget plugin being tested.
   *
   * @var \Drupal\feeds\Feeds\Target\File
   */
  protected $targetPlugin;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $this->client = $this
      ->prophesize(ClientInterface::class);
    $this->token = $this
      ->prophesize(Token::class);
    $this->entityFieldManager = $this
      ->prophesize(EntityFieldManagerInterface::class);
    $this->entityFieldManager
      ->getFieldStorageDefinitions('file')
      ->willReturn([]);
    $this->entityFinder = $this
      ->prophesize(EntityFinderInterface::class);
    $this->fileSystem = $this
      ->prophesize(FileSystemInterface::class);

    // Made-up entity type that we are referencing to.
    $referenceable_entity_type = $this
      ->prophesize(EntityTypeInterface::class);
    $referenceable_entity_type
      ->getKey('label')
      ->willReturn('file label');
    $this->entityTypeManager
      ->getDefinition('file')
      ->willReturn($referenceable_entity_type)
      ->shouldBeCalled();
    $method = $this
      ->getMethod('Drupal\\feeds\\Feeds\\Target\\File', 'prepareTarget')
      ->getClosure();
    $field_definition_mock = $this
      ->getMockFieldDefinition([
      'display_field' => 'false',
      'display_default' => 'false',
      'uri_scheme' => 'public',
      'target_type' => 'file',
      'file_directory' => '[date:custom:Y]-[date:custom:m]',
      'file_extensions' => 'pdf doc docx txt jpg jpeg ppt xls png',
      'max_filesize' => '',
      'description_field' => 'true',
      'handler' => 'default:file',
      'handler_settings' => [],
    ]);
    $configuration = [
      'feed_type' => $this
        ->createMock('Drupal\\feeds\\FeedTypeInterface'),
      'target_definition' => $method($field_definition_mock),
    ];
    $this->targetPlugin = new File($configuration, 'file', [], $this->entityTypeManager
      ->reveal(), $this->client
      ->reveal(), $this->token
      ->reveal(), $this->entityFieldManager
      ->reveal(), $this->entityFinder
      ->reveal(), $this->fileSystem
      ->reveal());
  }

  /**
   * {@inheritdoc}
   */
  protected function getTargetClass() {
    return File::class;
  }

  /**
   * @covers ::prepareValue
   * @dataProvider dataProviderPrepareValue
   */
  public function testPrepareValue($expected, array $values, $expected_exception = NULL) {
    $method = $this
      ->getProtectedClosure($this->targetPlugin, 'prepareValue');
    if ($expected_exception) {
      $this
        ->expectException($expected_exception);
    }
    $method(0, $values);
    foreach ($expected as $key => $value) {
      $this
        ->assertEquals($value, $values[$key]);
    }
  }

  /**
   * Data provider for testPrepareValue().
   */
  public function dataProviderPrepareValue() {
    return [
      // Description.
      [
        'expected' => [
          'description' => 'mydescription',
          'display' => FALSE,
        ],
        'values' => [
          'description' => 'mydescription',
        ],
      ],
      // Empty file target value.
      [
        'expected' => [],
        'values' => [
          'target_id' => '',
        ],
        'expected_exception' => EmptyFeedException::class,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
FieldTargetTestBase::testPrepareTarget public function @covers ::prepareTarget 1
FileTest::$client protected property The http client prophecy used in the test.
FileTest::$entityFinder protected property The Feeds entity finder service.
FileTest::$entityTypeManager protected property The entity type manager prophecy used in the test.
FileTest::$fileSystem protected property The file and stream wrapper helper.
FileTest::$targetPlugin protected property The FeedsTarget plugin being tested.
FileTest::$token protected property Token service.
FileTest::dataProviderPrepareValue public function Data provider for testPrepareValue().
FileTest::getTargetClass protected function Returns the target class. Overrides FieldTargetTestBase::getTargetClass
FileTest::setUp public function Overrides FeedsUnitTestCase::setUp
FileTest::testPrepareValue public function @covers ::prepareValue @dataProvider dataProviderPrepareValue
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.