Skip to content

Commit

Permalink
Write new test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
GKFX committed Dec 7, 2018
1 parent c0475d1 commit 5e1bebb
Show file tree
Hide file tree
Showing 3 changed files with 279 additions and 0 deletions.
117 changes: 117 additions & 0 deletions tests/CamdramBundle/Service/ContactEntityServiceTest.php
@@ -0,0 +1,117 @@
<?php

namespace Camdram\Tests\CamdramBundle\Service;

use Acts\CamdramBundle\Entity\Show;
use Acts\CamdramBundle\Entity\Society;
use Acts\CamdramBundle\Entity\Venue;
use Acts\CamdramSecurityBundle\Entity\User;
use Acts\CamdramBundle\Service\ContactEntityService;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;

class ContactEntityServiceTest extends TestCase
{
/**
* @var MockObject
*/
private $aclProvider;

/**
* @var MockObject
*/
private $mailer;

private $users = [];

/**
* @var ContactEntityService
*/
private $contactEntityService;

public function setUp()
{
$this->mailer = $this->getMockBuilder('\Swift_Mailer')
->disableOriginalConstructor()->disableOriginalClone()->getMock();
$this->aclProvider = $this->getMockBuilder('\Acts\\CamdramSecurityBundle\\Security\\Acl\\AclProvider')
->disableOriginalConstructor()->getMock();

$this->contactEntityService = new ContactEntityService($this->mailer, $this->aclProvider);

for ($i = 0; $i < 6; $i++) {
$user = new User();
$user->setName("User ".$i);
$user->setEmail("user".$i."@camdram.net");
$user->setIsEmailVerified(!($i&1));
$this->users[] = $user;
}
}

public function testEmailEntity() {
$showA = new Show();
$showB = new Show();
$showC = new Show();
$showD = new Show();
$showE = new Show();
$venue = new Venue();
$society = new Society();

$society->setName("Society 1");
$venue->setName("Venue 1");
$showA->setName("Show A")->setVenue($venue)->getSocieties()->add($society);
$showB->setName("Show B")->setVenue($venue)->getSocieties()->add($society);
$showC->setName("Show C")->getSocieties()->add($society);
$showD->setName("Show D")->setVenue($venue);
$showE->setName("Show E");
$this->aclProvider->method('getOwners')->will($this->returnValueMap([
[$showA, [$this->users[0], $this->users[1]]],
[$showB, []],
[$showC, []],
[$showD, []],
[$showE, []],
[$society, [$this->users[2], $this->users[3]]],
[$venue, [$this->users[4], $this->users[5]]],
]));
$this->mailer->expects($this->exactly(7))->method("send")
->with($this->callback(function($msg) {
if ($msg->getFrom()['support@camdram.net'] != 'Camdram') return false;
if (strpos($msg->getSubject(), 'Email Test') === false) return false;
$matches = [];
if (!preg_match('/receiving.*because.*(Show [A-E]|Society \d|Venue \d)/',
$msg->getBody(), $matches)) return false;
switch($matches[1]) {
case "Show A":
$this->assertSame(['user0@camdram.net' => 'User 0'], $msg->getTo());
return true;
case "Show B":
$this->assertSame(['user2@camdram.net' => 'User 2'], $msg->getTo());
return true;
case "Show C":
$this->assertSame(['user2@camdram.net' => 'User 2'], $msg->getTo());
return true;
case "Show D":
$this->assertSame(['user4@camdram.net' => 'User 4'], $msg->getTo());
return true;
case "Show E":
$this->assertSame(['support@camdram.net' => NULL], $msg->getTo());
return true;
case "Society 1":
$this->assertSame(['user2@camdram.net' => 'User 2'], $msg->getTo());
return true;
case "Venue 1":
$this->assertSame(['user4@camdram.net' => 'User 4'], $msg->getTo());
return true;
default:
return false;
}
}));

$this->contactEntityService->emailEntity($showA, "Camdram", "support@camdram.net", "Email Test", "A message");
$this->contactEntityService->emailEntity($showB, "Camdram", "support@camdram.net", "Email Test", "A message");
$this->contactEntityService->emailEntity($showC, "Camdram", "support@camdram.net", "Email Test", "A message");
$this->contactEntityService->emailEntity($showD, "Camdram", "support@camdram.net", "Email Test", "A message");
$this->contactEntityService->emailEntity($showE, "Camdram", "support@camdram.net", "Email Test", "A message");
$this->contactEntityService->emailEntity($society, "Camdram", "support@camdram.net", "Email Test", "A message");
$this->contactEntityService->emailEntity($venue, "Camdram", "support@camdram.net", "Email Test", "A message");
}
}
22 changes: 22 additions & 0 deletions tests/CamdramBundle/Service/EmailDispatcherTest.php
Expand Up @@ -3,6 +3,7 @@
namespace Camdram\Tests\CamdramBundle\Service;

use Acts\CamdramBundle\Entity\Show;
use Acts\CamdramBundle\Entity\Society;
use Acts\CamdramSecurityBundle\Entity\User;
use Acts\CamdramBundle\Service\EmailDispatcher;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -59,4 +60,25 @@ public function testSendShowCreatedEmail()

$this->emailDispatcher->sendShowCreatedEmail($show, $owners, $recipients, $admins);
}

public function testSendShowSocietyChangedEmail() {
$show = new Show();
$show->getSocieties()->add(new Society());
$owners = array('owner1', 'owner2');

$user1 = new User();
$user1->setEmail('user1@camdram.net');
$user2 = new User();
$user2->setEmail('user2@camdram.net');
$moderators = array(
$user1, $user2
);

$this->twig->expects($this->exactly(1))->method('render')
->with($this->anything(), array('owners' => $owners, 'show' => $show))
->will($this->returnValue('The message'));

$this->mailer->expects($this->once())->method('send');
$this->emailDispatcher->sendShowSocietyChangedEmail($show, $owners, $moderators);
}
}
140 changes: 140 additions & 0 deletions tests/CamdramBundle/Service/ModerationManagerTest.php
@@ -0,0 +1,140 @@
<?php

namespace Camdram\Tests\CamdramBundle\Service;

use Acts\CamdramBundle\Entity\Show;
use Acts\CamdramBundle\Entity\Society;
use Acts\CamdramBundle\Entity\Venue;
use Acts\CamdramBundle\Service\ModerationManager;
use Acts\CamdramSecurityBundle\Entity\AccessControlEntry;
use Acts\CamdramSecurityBundle\Entity\User;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;

class ModerationManagerTest extends TestCase
{
/**
* @var MockObject
*/
private $entityManager;

/**
* @var MockObject
*/
private $dispatcher;

/**
* @var MockObject
*/
private $aclProvider;

/**
* @var MockObject
*/
private $authorizationChecker;

/**
* @var MockObject
*/
private $tokenStorage;

/**
* @var MockObject
*/
private $logger;

/**
* @var MockObject
*/
private $userRepo;

/**
* @var \Acts\CamdramBundle\Service\ModerationManager
*/
private $moderationManager;

# Useful shared entities with some mocking common to all tests.
private $users = [];
private $admin;
private $society;
private $venue;
private $ownedShow;

public function setUp()
{
$this->entityManager = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')
->disableOriginalConstructor()->getMock();
$this->dispatcher = $this->getMockBuilder('\\Acts\\CamdramBundle\\Service\\EmailDispatcher')
->disableOriginalConstructor()->getMock();
$this->aclProvider = $this->getMockBuilder('\\Acts\\CamdramSecurityBundle\\Security\\Acl\\AclProvider')
->disableOriginalConstructor()->getMock();
$this->authorizationChecker = $this->getMockBuilder('\\Symfony\\Component\\Security\\Core\\Authorization\\AuthorizationCheckerInterface')
->disableOriginalConstructor()->getMock();
$this->tokenStorage = $this->getMockBuilder('\\Symfony\\Component\\Security\\Core\\Authentication\\Token\\Storage\\TokenStorageInterface')
->disableOriginalConstructor()->getMock();
$this->logger = $this->getMockBuilder('\\Psr\\Log\\LoggerInterface')
->disableOriginalConstructor()->getMock();
$this->userRepo = $this->getMockBuilder('\\Acts\\CamdramSecurityBundle\\Entity\\UserRepository')
->disableOriginalConstructor()->getMock();

// Build mock entities
for ($i = 0; $i < 6; $i++) {
$user = new User();
$user->setName("User ".$i);
$user->setEmail("user".$i."@camdram.net");
$this->users[] = $user;
}
$this->admin = new User();
$this->admin->setName("Admin User");
$this->admin->setEmail("admin@camdram.net");
$this->admin->setIsEmailVerified(true);
$this->society = new Society();
$this->venue = new Venue();
$this->ownedShow = new Show();

// Any test should be able to retrieve the userRepo without further set-up.
$this->entityManager->expects($this->any())->method('getRepository')
->will($this->returnValueMap([['ActsCamdramSecurityBundle:User', $this->userRepo]]));
$this->userRepo->method('findAdmins')->with(AccessControlEntry::LEVEL_FULL_ADMIN)
->will($this->returnValue([$this->admin]));
$this->userRepo->method('getEntityOwners')->will($this->returnValueMap([
[$this->society, [$this->users[0], $this->users[1]]],
[$this->venue, [$this->users[2], $this->users[3]]],
[$this->ownedShow, [$this->users[4]]]
]));

$this->moderationManager = new ModerationManager($this->entityManager, $this->dispatcher,
$this->aclProvider, $this->authorizationChecker, $this->tokenStorage, $this->logger);
}

/**
* @covers Acts\CamdramBundle\Service\ModerationManager::getModeratorAdmins
* @covers Acts\CamdramBundle\Service\ModerationManager::getModeratorsForEntity
*/
public function testGetModerators()
{
$show0 = new Show();
$showS = new Show();
$showV = new Show();
$showSV = new Show();
$showS->getSocieties()->add($this->society);
$showV->setVenue($this->venue);
$showSV->setVenue($this->venue)->getSocieties()->add($this->society);

$this->assertSame($this->moderationManager->getModeratorAdmins(), [$this->admin]);
$this->assertSame($this->moderationManager->getModeratorsForEntity($show0), [$this->admin]);
$this->assertSame($this->moderationManager->getModeratorsForEntity($showS), array_slice($this->users, 0, 2));
$this->assertSame($this->moderationManager->getModeratorsForEntity($showV), array_slice($this->users, 2, 2));
$this->assertSame($this->moderationManager->getModeratorsForEntity($showSV), array_slice($this->users, 0, 4));
}

public function testNotifySocietyChanged() {
$this->dispatcher->expects($this->once())->method('sendShowSocietyChangedEmail')
->with($this->ownedShow, [$this->users[4]], array_slice($this->users, 0, 2));

# Show owned by users[4] and society, no token stored
$this->tokenStorage->method('getToken')->willReturn(null);
$this->ownedShow->getSocieties()->add($this->society);
$this->moderationManager->notifySocietyChanged($this->ownedShow);
}
}

0 comments on commit 5e1bebb

Please sign in to comment.