FeedsMockingTrait.php in Feeds 8.3
File
tests/src/Traits/FeedsMockingTrait.php
View source
<?php
namespace Drupal\Tests\feeds\Traits;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\feeds\FeedInterface;
use Drupal\feeds\FeedTypeInterface;
use Prophecy\Argument;
trait FeedsMockingTrait {
protected function getMockFeedType() {
$feed_type = $this
->createMock(FeedTypeInterface::class);
$feed_type->id = 'test_feed_type';
$feed_type->description = 'This is a test feed type';
$feed_type->label = 'Test feed type';
$feed_type
->expects($this
->any())
->method('label')
->will($this
->returnValue($feed_type->label));
return $feed_type;
}
protected function getMockFeed() {
$feed = $this
->createMock(FeedInterface::class);
$feed
->expects($this
->any())
->method('getType')
->will($this
->returnValue($this
->getMockFeedType()));
return $feed;
}
protected function getMockedAccountSwitcher() {
$switcher = $this
->prophesize(AccountSwitcherInterface::class);
$switcher
->switchTo(Argument::type(AccountInterface::class))
->will(function () use ($switcher) {
$switcher
->switchBack()
->shouldBeCalled();
return $switcher
->reveal();
});
return $switcher
->reveal();
}
protected function getMockAccount(array $perms = []) {
$account = $this
->createMock(AccountInterface::class);
if ($perms) {
$map = [];
foreach ($perms as $perm => $has) {
$map[] = [
$perm,
$has,
];
}
$account
->expects($this
->any())
->method('hasPermission')
->will($this
->returnValueMap($map));
}
return $account;
}
protected function getMockFieldDefinition(array $settings = []) {
$definition = $this
->createMock(FieldDefinitionInterface::class);
$definition
->expects($this
->any())
->method('getSettings')
->will($this
->returnValue($settings));
return $definition;
}
protected function getMockFileSystem() {
$definition = $this
->createMock(FileSystemInterface::class);
$definition
->expects($this
->any())
->method('tempnam')
->will($this
->returnCallback(function () {
$args = func_get_args();
$dir = $args[1];
mkdir('vfs://feeds/' . $dir);
$file = 'vfs://feeds/' . $dir . '/' . mt_rand(10, 1000);
touch($file);
return $file;
}));
return $definition;
}
}