TeamMembershipManagerTest.php in Apigee Edge 8
File
modules/apigee_edge_teams/tests/src/Functional/TeamMembershipManagerTest.php
View source
<?php
namespace Drupal\Tests\apigee_edge_teams\Functional;
class TeamMembershipManagerTest extends ApigeeEdgeTeamsFunctionalTestBase {
protected $developerStorage;
protected $teamStorage;
protected $team;
protected $developers;
protected function setUp() {
parent::setUp();
$this->developerStorage = $this->container
->get('entity_type.manager')
->getStorage('developer');
$this->teamStorage = $this->container
->get('entity_type.manager')
->getStorage('team');
for ($i = 0; $i < 2; $i++) {
$name = strtolower($this
->randomMachineName());
$developer = $this->developerStorage
->create([
'email' => $name . '@example.com',
'userName' => $name,
'firstName' => $this
->getRandomGenerator()
->word(8),
'lastName' => $this
->getRandomGenerator()
->word(8),
]);
$developer
->save();
$this->developers[$i] = $developer;
}
$this->team = $this->teamStorage
->create([
'name' => $this
->getRandomGenerator()
->name(),
]);
$this->team
->save();
}
protected function tearDown() {
foreach ($this->developers as $developer) {
try {
$this->developerStorage
->delete([
$developer,
]);
} catch (\Exception $exception) {
$this
->logException($exception);
}
}
if ($this->team !== NULL) {
try {
$this->teamStorage
->delete([
$this->team,
]);
} catch (\Exception $exception) {
$this
->logException($exception);
}
}
parent::tearDown();
}
public function testTeamMembershipManager() {
$team_membership_manager = $this->container
->get('apigee_edge_teams.team_membership_manager');
$team_membership_cache = $this->container
->get('apigee_edge_teams.cache.company_membership_object');
foreach ($this->developers as $developer) {
$this
->assertEmpty($developer
->getCompanies());
$this
->assertEmpty($team_membership_manager
->getTeams($developer
->getEmail()));
$this
->assertEmpty($team_membership_manager
->getMembers($this->team
->getName()));
}
$this
->assertEmpty($team_membership_cache
->getMembership($this->team
->getName())
->getMembers());
$team_membership_manager
->addMembers($this->team
->getName(), [
$this->developers[0]
->getEmail(),
$this->developers[1]
->getEmail(),
]);
foreach ($this->developers as $developer) {
$this
->assertContains($this->team
->getName(), $team_membership_manager
->getTeams($developer
->getEmail()));
$this
->assertContains($developer
->getEmail(), $team_membership_manager
->getMembers($this->team
->getName()));
$this
->assertArrayHasKey($developer
->getEmail(), $team_membership_cache
->getMembership($this->team
->getName())
->getMembers());
}
foreach ($this->developers as $developer) {
$team_membership_manager
->removeMembers($this->team
->getName(), [
$developer
->getEmail(),
]);
$this
->assertNotContains($this->team
->getName(), $team_membership_manager
->getTeams($developer
->getEmail()));
$this
->assertNotContains($developer
->getEmail(), $team_membership_manager
->getMembers($this->team
->getName()));
$this
->assertArrayNotHasKey($developer
->getEmail(), $team_membership_cache
->getMembership($this->team
->getName())
->getMembers());
}
$this
->assertEmpty($team_membership_cache
->getMembership($this->team
->getName())
->getMembers());
$team_membership_manager
->addMembers($this->team
->getName(), [
$this->developers[0]
->getEmail(),
]);
$this->developerStorage
->delete([
$this->developers[0],
]);
$this
->assertNotContains($this->developers[0]
->getEmail(), $team_membership_manager
->getMembers($this->team
->getName()));
$this
->assertArrayNotHasKey($this->developers[0]
->getEmail(), $team_membership_cache
->getMembership($this->team
->getName())
->getMembers());
$this->teamStorage
->delete([
$this->team,
]);
$this
->assertNull($team_membership_cache
->getMembership($this->team
->getName()));
}
}