AcquiadamKernelTestBase.php in Media: Acquia DAM 8
File
tests/src/Kernel/AcquiadamKernelTestBase.php
View source
<?php
namespace Drupal\Tests\media_acquiadam\Kernel;
use cweagans\webdam\Entity\Asset;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\media\Entity\Media;
use Drupal\media\MediaInterface;
use Drupal\media_acquiadam\ClientFactory;
use Drupal\media_acquiadam_test\TestClient;
use Drupal\Tests\media_acquiadam\Traits\AcquiadamAssetDataTrait;
abstract class AcquiadamKernelTestBase extends EntityKernelTestBase {
use AcquiadamAssetDataTrait;
const DEFAULT_BUNDLE = 'acquia_dam_asset';
public static $modules = [
'fallback_formatter',
'file',
'image',
'media',
'media_acquiadam',
'media_acquiadam_test',
];
protected $testClient;
protected function setUp() {
parent::setUp();
$this
->setTestClient();
$this
->installConfig('media_acquiadam_test');
$this
->installEntitySchema('file');
$this
->installEntitySchema('media');
$this
->installSchema('file', [
'file_usage',
]);
$this
->installSchema('media_acquiadam', [
'acquiadam_assets_data',
]);
}
protected function setTestClient() {
$this->testClient = new TestClient();
$acquiadam_client_factory = $this
->getMockBuilder(ClientFactory::class)
->disableOriginalConstructor()
->getMock();
$acquiadam_client_factory
->expects($this
->any())
->method('get')
->willReturn($this->testClient);
$this->container
->set('media_acquiadam.client_factory', $acquiadam_client_factory);
\Drupal::setContainer($this->container);
}
protected function createMedia(int $asset_id, string $bundle = self::DEFAULT_BUNDLE) {
$media = Media::create([
'bundle' => $bundle,
'field_acquiadam_asset_id' => $asset_id,
]);
$media
->save();
return $media;
}
public function getAssetFileEntity(MediaInterface $media) {
return current($media
->get('field_acquiadam_asset_file')
->referencedEntities());
}
protected function getAssetUri(Asset $asset, MediaInterface $media) {
$destination_folder = $this->container
->get('media_acquiadam.asset_file.helper')
->getDestinationFromEntity($media, 'field_acquiadam_asset_file');
return sprintf('%s/%s', $destination_folder, $asset->filename);
}
}