class AcquiadamServiceTest in Media: Acquia DAM 8
Acquia DAM REST extension tests.
@group media_acquiadam
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\media_acquiadam\Unit\AcquiadamServiceTest uses AcquiadamConfigTrait
Expanded class hierarchy of AcquiadamServiceTest
File
- tests/
src/ Unit/ AcquiadamServiceTest.php, line 21
Namespace
Drupal\Tests\media_acquiadam\UnitView source
class AcquiadamServiceTest extends UnitTestCase {
use AcquiadamConfigTrait, AcquiadamLoggerFactoryTrait, AcquiadamAssetDataTrait;
/**
* Container builder helper.
*
* @var \Drupal\Core\DependencyInjection\ContainerBuilder
*/
protected $container;
/**
* Media: Acquia DAM client.
*
* @var \Drupal\media_acquiadam\Acquiadam
*/
protected $acquiaDamClient;
/**
* Validate the static cache helper works as expected.
*/
public function testStaticAssetCache() {
$asset = $this
->getAssetData();
$this
->assertNull($this->acquiaDamClient
->staticAssetCache('get', $asset->id));
$this->acquiaDamClient
->staticAssetCache('set', $asset->id, FALSE);
$this
->assertFalse($this->acquiaDamClient
->staticAssetCache('get', $asset->id));
$this->acquiaDamClient
->staticAssetCache('set', $asset->id, $asset);
$this
->assertInstanceOf(Asset::class, $this->acquiaDamClient
->staticAssetCache('get', $asset->id));
$this->acquiaDamClient
->staticAssetCache('clear');
$this
->assertNull($this->acquiaDamClient
->staticAssetCache('get', $asset->id));
}
/**
* Validates we can fetch an asset.
*/
public function testGetAsset() {
$asset = $this
->getAssetData();
// No assets should be primed.
$this
->assertNull($this->acquiaDamClient
->staticAssetCache('get', $asset->id));
// Asset should be primed.
$this
->assertInstanceOf(Asset::class, $this->acquiaDamClient
->getAsset($asset->id));
$this
->assertInstanceOf(Asset::class, $this->acquiaDamClient
->staticAssetCache('get', $asset->id));
// Simulate a cached failed fetch.
$this->acquiaDamClient
->staticAssetCache('set', $asset->id, FALSE);
$this
->assertFalse($this->acquiaDamClient
->getAsset($asset->id));
$this
->assertFalse($this->acquiaDamClient
->getAsset(1234567890));
}
/**
* Tests that flattened folder output matches what is expected.
*/
public function testGetFlattenedFolderList() {
// Test that the top level flattening works as expected.
$folders = $this->acquiaDamClient
->getFlattenedFolderList();
$this
->assertArrayEquals($this
->getFlattenedTopLevelFoldersData(), $folders);
// Test that a parent item gets its proper child items.
$folders = $this->acquiaDamClient
->getFlattenedFolderList(90672);
$this
->assertArrayEquals([
90673 => 'Slideshows',
90674 => 'Ad Ideas',
], $folders);
// Test that a parent item gets its proper child items.
$folders = $this->acquiaDamClient
->getFlattenedFolderList(90786);
$this
->assertArrayEquals([
90787 => 'Spreadsheets',
90788 => 'Logos',
], $folders);
// Test that items with no children reflect that.
$folders = $this->acquiaDamClient
->getFlattenedFolderList(90832);
$this
->assertArrayEquals([], $folders);
// Test that child items with no subchildren reflect that.
$folders = $this->acquiaDamClient
->getFlattenedFolderList(90673);
$this
->assertArrayEquals([], $folders);
}
/**
* Validate our helper method for testing folder data works as expected.
*/
public function testSelfGetFolderData() {
// Test that we can get parent folders.
$folder = $this
->getFolderData(90786);
$this
->assertObjectHasAttribute('id', $folder);
$this
->assertEquals(90786, $folder->id);
// Test that we can get child folders.
$folder = $this
->getFolderData(90788);
$this
->assertObjectHasAttribute('id', $folder);
$this
->assertEquals(90788, $folder->id);
}
/**
* The flattened top level folder data.
*
* @return array
* The array of flattened folders keyed by ID.
*/
protected function getFlattenedTopLevelFoldersData() {
return [
90672 => 'Marketing',
90673 => 'Slideshows',
90674 => 'Ad Ideas',
90786 => 'Sales',
90787 => 'Spreadsheets',
90788 => 'Logos',
90832 => 'Support',
];
}
/**
* Get the specific folder from an array of folders.
*
* @param int $folderId
* The ID of the folder to retrieve.
* @param array $folders
* The array of folders to look for. Defaults to getTopLevelFoldersData().
*
* @return object|null
* The folder if found, NULL otherwise.
*/
protected function getFolderData($folderId, array $folders = []) {
if (empty($folders)) {
$folders = $this
->getTopLevelFoldersData();
}
foreach ($folders as $folder) {
if (!empty($folder->id) && $folder->id == $folderId) {
return $folder;
}
elseif (!empty($folder->folders)) {
$child = $this
->getFolderData($folderId, $folder->folders);
if (!empty($child)) {
return $child;
}
}
}
return NULL;
}
/**
* Sample data for the top level folder content.
*
* @return array
* A minimal array of test content as the API would return.
*/
protected function getTopLevelFoldersData() {
return [
(object) [
'id' => '90672',
'name' => 'Marketing',
'folders' => [
(object) [
'id' => '90673',
'name' => 'Slideshows',
],
(object) [
'id' => '90674',
'name' => 'Ad Ideas',
],
],
],
(object) [
'id' => '90786',
'name' => 'Sales',
'folders' => [
(object) [
'id' => '90787',
'name' => 'Spreadsheets',
],
(object) [
'id' => '90788',
'name' => 'Logos',
],
],
],
(object) [
'id' => '90832',
'name' => 'Support',
],
];
}
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$dam_client = $this
->getMockBuilder(Client::class)
->disableOriginalConstructor()
->getMock();
$dam_client
->expects($this
->any())
->method('getFolder')
->willReturnCallback(function ($folderId) {
return $this
->getFolderData($folderId);
});
$dam_client
->expects($this
->any())
->method('getTopLevelFolders')
->willReturn($this
->getTopLevelFoldersData());
$dam_client
->method('getAsset')
->willReturnMap([
[
$this
->getAssetData()->id,
TRUE,
$this
->getAssetData(),
],
[
$this
->getAssetData()->id,
FALSE,
$this
->getAssetData(),
],
]);
// We need to make sure we get our mocked class instead of the original.
$acquiadam_client_factory = $this
->getMockBuilder(ClientFactory::class)
->disableOriginalConstructor()
->getMock();
$acquiadam_client_factory
->expects($this
->any())
->method('get')
->willReturn($dam_client);
$acquiadam_asset_data = $this
->getMockBuilder(AssetDataInterface::class)
->disableOriginalConstructor()
->getMock();
$this->container = new ContainerBuilder();
$this->container
->set('string_translation', $this
->getStringTranslationStub());
$this->container
->set('config.factory', $this
->getConfigFactoryStub());
$this->container
->set('logger.factory', $this
->getLoggerFactoryStub());
$this->container
->set('media_acquiadam.client_factory', $acquiadam_client_factory);
$this->container
->set('media_acquiadam.asset_data', $acquiadam_asset_data);
\Drupal::setContainer($this->container);
$this->acquiaDamClient = Acquiadam::create($this->container);
}
/**
* {@inheritdoc}
*/
public function tearDown() {
// Reset the static cache because it will persist between tests.
$this->acquiaDamClient
->staticAssetCache('clear');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AcquiadamConfigTrait:: |
public | function | ||
AcquiadamServiceTest:: |
protected | property | Media: Acquia DAM client. | |
AcquiadamServiceTest:: |
protected | property | Container builder helper. | |
AcquiadamServiceTest:: |
protected | function | The flattened top level folder data. | |
AcquiadamServiceTest:: |
protected | function | Get the specific folder from an array of folders. | |
AcquiadamServiceTest:: |
protected | function | Sample data for the top level folder content. | |
AcquiadamServiceTest:: |
protected | function |
Overrides UnitTestCase:: |
|
AcquiadamServiceTest:: |
public | function | ||
AcquiadamServiceTest:: |
public | function | Validates we can fetch an asset. | |
AcquiadamServiceTest:: |
public | function | Tests that flattened folder output matches what is expected. | |
AcquiadamServiceTest:: |
public | function | Validate our helper method for testing folder data works as expected. | |
AcquiadamServiceTest:: |
public | function | Validate the static cache helper works as expected. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |