class DemoImageTest in Panopoly 8.2
Tests for the 'panopoly_demo_image' migrate process plugin.
@coversDefaultClass \Drupal\panopoly_core\Plugin\migrate\process\DemoImage
@group PanopolyCore
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\panopoly_core\Unit\Plugin\migrate\process\DemoImageTest
Expanded class hierarchy of DemoImageTest
File
- modules/
panopoly/ panopoly_core/ tests/ src/ Unit/ Plugin/ migrate/ process/ DemoImageTest.php, line 24
Namespace
Drupal\Tests\panopoly_core\Unit\Plugin\migrate\processView source
class DemoImageTest extends UnitTestCase {
/**
* Mock of the migration class.
*
* @var \Drupal\migrate\Plugin\MigrationInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $migration;
/**
* Mock of the file entity storage.
*
* @var \Drupal\file\FileStorageInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $fileStorage;
/**
* Mock of the module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $moduleHandler;
/**
* Mock of the filesystem service.
*
* @var \Drupal\Core\File\FileSystemInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $filesystem;
/**
* Mock of the image factory service.
*
* @var \Drupal\Core\Image\ImageFactory|\Prophecy\Prophecy\ObjectProphecy
*/
protected $imageFactory;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
// @todo This seems wrong. We just need the FILE_STATUS_PERMANENT constant.
// Should this be a kernel test instead?
require_once './includes/file.inc';
$this->migration = $this
->prophesize(MigrationInterface::class);
$this->fileStorage = $this
->prophesize(FileStorageInterface::class);
$this->moduleHandler = $this
->prophesize(ModuleHandlerInterface::class);
$this->filesystem = $this
->prophesize(FileSystemInterface::class);
$this->imageFactory = $this
->prophesize(ImageFactory::class);
}
/**
* Creates a new plugin to test.
*
* @param array $configuration
* The plugin configuration.
* @param array|null $methods
* The list of methods to mock.
*
* @return \Drupal\panopoly_core\Plugin\migrate\process\DemoImage|\PHPUnit_Framework_MockObject_MockObject
* A mock plugin to test.
*/
protected function createPlugin(array $configuration, array $methods = NULL) {
return $this
->getMockBuilder(DemoImage::class)
->setConstructorArgs([
$configuration,
'demo_image',
[],
$this->migration
->reveal(),
$this->fileStorage
->reveal(),
$this->moduleHandler
->reveal(),
$this->filesystem
->reveal(),
$this->imageFactory
->reveal(),
])
->onlyMethods($methods)
->getMock();
}
/**
* Invokes a normally inaccessible method.
*
* @param object $object
* Object to invoke the method on.
* @param string $method
* The name of the method.
* @param array $arguments
* The arguments to pass to the method.
*
* @return mixed
* The result of the invoked method.
*/
protected function invokeMethod($object, $method, array $arguments = []) {
$method = new \ReflectionMethod(get_class($object), $method);
$method
->setAccessible(TRUE);
return $method
->invokeArgs($object, $arguments);
}
/**
* Tests a successful call to transform().
*
* @covers ::transform
*/
public function testTransformSuccess() {
$image = $this
->prophesize(ImageInterface::class);
$image
->getWidth()
->willReturn(300);
$image
->getHeight()
->willReturn(200);
$this->imageFactory
->get('public://demo_images/image.jpg')
->willReturn($image);
$executable = $this
->prophesize(MigrateExecutableInterface::class);
$row = $this
->prophesize(Row::class);
$row
->getSourceProperty('image_filename')
->willReturn('image.jpg');
$plugin = $this
->createPlugin([
'filename_property' => 'image_filename',
], [
'getSourcePath',
'getDestinationPath',
'checkFile',
'findOrCreateFile',
'getOptionalProperty',
]);
$plugin
->method('getSourcePath')
->willReturn('modules/demo_module/images');
$plugin
->method('getDestinationPath')
->willReturn('public://demo_images');
$plugin
->method('checkFile')
->willReturn(TRUE);
$file = $this
->prophesize(File::class);
$file
->id()
->willReturn(32);
$plugin
->method('findOrCreateFile')
->with('modules/demo_module/images/image.jpg', 'public://demo_images/image.jpg')
->willReturn($file
->reveal());
$plugin
->expects($this
->exactly(3))
->method('getOptionalProperty')
->willReturnMap([
[
'description_property',
$row
->reveal(),
NULL,
],
[
'alt_property',
$row
->reveal(),
'This is the alternative text',
],
[
'title',
$row
->reveal(),
'',
],
]);
$result = $plugin
->transform(NULL, $executable
->reveal(), $row
->reveal(), 'field_image');
$this
->assertEquals([
'target_id' => 32,
'display' => 1,
'description' => '',
'alt' => 'This is the alternative text',
'title' => '',
'width' => 300,
'height' => 200,
], $result);
}
/**
* Tests calling transform() with bad configuration.
*
* @covers ::transform
*/
public function testTransformBadConfiguration() {
$plugin = $this
->createPlugin([]);
$executable = $this
->prophesize(MigrateExecutableInterface::class);
$row = $this
->prophesize(Row::class);
$this
->assertEquals([], $plugin
->transform(NULL, $executable
->reveal(), $row
->reveal(), 'field_image'));
}
/**
* Tests that transform() throws an exception when a file is inaccessible.
*
* @covers ::transform
*
* @expectedException \Drupal\migrate\MigrateException
* @expectedExceptionMessage Cannot find source image: modules/demo_module/images/image.jpg
*/
public function testTransformBadSourceFile() {
$executable = $this
->prophesize(MigrateExecutableInterface::class);
$row = $this
->prophesize(Row::class);
$row
->getSourceProperty('image_filename')
->willReturn('image.jpg');
$plugin = $this
->createPlugin([
'filename_property' => 'image_filename',
], [
'getSourcePath',
'getDestinationPath',
'checkFile',
]);
$plugin
->method('getSourcePath')
->willReturn('modules/demo_module/images');
$plugin
->method('getDestinationPath')
->willReturn('public://demo_images');
$plugin
->method('checkFile')
->willReturn(FALSE);
$plugin
->transform(NULL, $executable
->reveal(), $row
->reveal(), 'field_image');
}
/**
* Tests the getOptionalProperty() method.
*
* @covers ::getOptionalProperty
*/
public function testGetOptionalProperty() {
$plugin = $this
->createPlugin([
'test_property' => 'source_property_name',
]);
$row = $this
->prophesize(Row::class);
$row
->getSourceProperty('source_property_name')
->willReturn('test_value');
$this
->assertEquals('test_value', $this
->invokeMethod($plugin, 'getOptionalProperty', [
'test_property',
$row
->reveal(),
]));
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DemoImageTest:: |
protected | property | Mock of the file entity storage. | |
DemoImageTest:: |
protected | property | Mock of the filesystem service. | |
DemoImageTest:: |
protected | property | Mock of the image factory service. | |
DemoImageTest:: |
protected | property | Mock of the migration class. | |
DemoImageTest:: |
protected | property | Mock of the module handler. | |
DemoImageTest:: |
protected | function | Creates a new plugin to test. | |
DemoImageTest:: |
protected | function | Invokes a normally inaccessible method. | |
DemoImageTest:: |
public | function |
Overrides UnitTestCase:: |
|
DemoImageTest:: |
public | function | Tests the getOptionalProperty() method. | |
DemoImageTest:: |
public | function | Tests calling transform() with bad configuration. | |
DemoImageTest:: |
public | function | Tests that transform() throws an exception when a file is inaccessible. | |
DemoImageTest:: |
public | function | Tests a successful call to transform(). | |
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 factory that behaves according to the passed array. | |
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. |