S3FileEntityHandlerTest.php in Acquia Content Hub 8.2
File
modules/acquia_contenthub_s3/tests/src/Unit/S3FileEntityHandlerTest.php
View source
<?php
namespace Drupal\Tests\acquia_contenthub_s3\Unit;
use Acquia\ContentHubClient\CDF\CDFObject;
use Drupal\acquia_contenthub\Event\ParseCdfEntityEvent;
use Drupal\acquia_contenthub_s3\EventSubscriber\Cdf\S3FileEntityHandler;
use Drupal\acquia_contenthub_s3\S3FileMapper;
use Drupal\file\FileInterface;
use Drupal\node\NodeInterface;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\MethodProphecy;
class S3FileEntityHandlerTest extends UnitTestCase {
protected $s3FileMapper;
public function testOnParseCdfWithValidData() {
$file = $this
->prophesize(FileInterface::class);
$event = $this
->prophesize(ParseCdfEntityEvent::class);
$event
->getEntity()
->willReturn($file
->reveal())
->shouldBeCalledOnce();
$cdf = $this
->prophesize(CDFObject::class);
$cdf
->getAttribute(Argument::type('string'))
->willReturn('s3://example.png');
$event
->getCdf()
->willReturn($cdf
->reveal())
->shouldBeCalledOnce();
$this
->assertMapS3FileMethodThat([
$this,
'shouldBeCalledOnce',
], $event
->reveal());
}
public function testOnParseCdfWithInvalidEntity() {
$node = $this
->prophesize(NodeInterface::class);
$event = $this
->prophesize(ParseCdfEntityEvent::class);
$event
->getEntity()
->willReturn($node
->reveal())
->shouldBeCalledOnce();
$event
->getCdf()
->shouldNotBeCalled();
$this
->assertMapS3FileMethodThat([
$this,
'shouldNotBeCalled',
], $event
->reveal());
}
public function testOnParseCdfWithEmptyFileUriAttribute() {
$file = $this
->prophesize(FileInterface::class);
$event = $this
->prophesize(ParseCdfEntityEvent::class);
$event
->getEntity()
->willReturn($file
->reveal())
->shouldBeCalledOnce();
$cdf = $this
->prophesize(CDFObject::class);
$cdf
->getAttribute(Argument::type('string'))
->willReturn(NULL);
$event
->getCdf()
->willReturn($cdf
->reveal())
->shouldBeCalledOnce();
$this
->assertMapS3FileMethodThat([
$this,
'shouldNotBeCalled',
], $event
->reveal());
}
protected function assertMapS3FileMethodThat(callable $callback, ParseCdfEntityEvent $event) : void {
$file_mapper = $this
->prophesize(S3FileMapper::class);
$method = $file_mapper
->mapS3File(Argument::type(CDFObject::class), Argument::type(FileInterface::class));
$callback($method);
(new S3FileEntityHandler($file_mapper
->reveal()))
->onParseCdf($event);
}
protected function shouldBeCalledOnce(MethodProphecy $method) : void {
$method
->shouldBeCalledOnce();
}
protected function shouldNotBeCalled(MethodProphecy $method) : void {
$method
->shouldNotBeCalled();
}
}