OgMembershipStateCacheContextTest.php in Organic groups 8
File
tests/src/Unit/Cache/Context/OgMembershipStateCacheContextTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\og\Unit\Cache\Context;
use Drupal\Core\Session\AccountInterface;
use Drupal\og\Cache\Context\OgMembershipStateCacheContext;
use Drupal\og\MembershipManagerInterface;
use Drupal\og\OgMembershipInterface;
class OgMembershipStateCacheContextTest extends OgContextCacheContextTestBase {
protected $membership;
protected $membershipManager;
protected $user;
protected function setUp() : void {
parent::setUp();
$this->user = $this
->prophesize(AccountInterface::class);
$this->membership = $this
->prophesize(OgMembershipInterface::class);
$this->membershipManager = $this
->prophesize(MembershipManagerInterface::class);
}
public function testWithoutContext() {
$this
->expectGroupContext();
$result = $this
->getContextResult();
$this
->assertEquals(OgMembershipStateCacheContext::NO_CONTEXT, $result);
}
protected function setupExpectedContext($context) {
$this
->expectGroupContext($this->group
->reveal());
$this
->expectMembership($context);
}
protected function getCacheContext() {
return new OgMembershipStateCacheContext($this->user
->reveal(), $this->ogContext
->reveal(), $this->membershipManager
->reveal());
}
protected function expectMembership($state) {
if ($state) {
$this->membership
->getState()
->willReturn($state);
$state = $this->membership;
}
$states = [
OgMembershipInterface::STATE_ACTIVE,
OgMembershipInterface::STATE_PENDING,
OgMembershipInterface::STATE_BLOCKED,
];
$this->membershipManager
->getMembership($this->group
->reveal(), $this->user
->reveal()
->id(), $states)
->willReturn($state);
}
public function contextProvider() {
return [
[
FALSE,
OgMembershipStateCacheContext::NO_CONTEXT,
],
[
OgMembershipInterface::STATE_ACTIVE,
OgMembershipInterface::STATE_ACTIVE,
],
[
OgMembershipInterface::STATE_PENDING,
OgMembershipInterface::STATE_PENDING,
],
[
OgMembershipInterface::STATE_BLOCKED,
OgMembershipInterface::STATE_BLOCKED,
],
];
}
}