-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MPFE-997] some refactoring, UID class has been moved to this bundle
- Loading branch information
Showing
8 changed files
with
847 additions
and
469 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,16 +10,14 @@ | |
use Modera\NotificationBundle\Tests\Fixtures\Contributions\DummyChannel; | ||
use Modera\NotificationBundle\Tests\Fixtures\Entity\User; | ||
use Modera\NotificationBundle\Tests\Functional\AbstractDatabaseTest; | ||
use Modera\NotificationBundle\Transport\UID; | ||
|
||
/** | ||
* @author Sergei Lissovski <[email protected]> | ||
* @copyright 2016 Modera Foundation | ||
*/ | ||
class NotificationCenterTest extends AbstractDatabaseTest | ||
{ | ||
/** | ||
* @group zhopa | ||
*/ | ||
public function testCreateNotificationBuilder() | ||
{ | ||
/* @var ChannelProvider $provider */ | ||
|
@@ -64,7 +62,11 @@ public function testCreateNotificationBuilder() | |
|
||
$this->assertInstanceOf(DeliveryReport::class, $report); | ||
/* @var NotificationDefinition $def */ | ||
$def = $this->em()->find(NotificationDefinition::class, $report->getDispatchResult()); | ||
|
||
/* @var UID $uid */ | ||
$uid = $report->getDispatchResult(); | ||
|
||
$def = $this->em()->find(NotificationDefinition::class, $uid->getNotification()); | ||
|
||
$this->assertInstanceOf(NotificationDefinition::class, $def); | ||
$this->assertEquals('hello world', $def->getMessage()); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace Modera\NotificationBundle\Tests\Unit\Service; | ||
use Doctrine\ORM\EntityRepository; | ||
use Modera\NotificationBundle\Entity\UserNotificationInstance; | ||
use Modera\NotificationBundle\Service\NotificationService; | ||
use Modera\NotificationBundle\Transport\UID; | ||
use Symfony\Bridge\Doctrine\RegistryInterface; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
/** | ||
* @author Sergei Lissovski <[email protected]> | ||
* @copyright 2017 Modera Foundation | ||
*/ | ||
class NotificationServiceTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var NotificationService | ||
*/ | ||
private $ns; | ||
|
||
private $registryMock; | ||
|
||
private $repositoryMock; | ||
|
||
private $userMock; | ||
|
||
public function setUp() | ||
{ | ||
$this->repositoryMock = \Phake::mock(EntityRepository::class); | ||
$this->registryMock = \Phake::mock(RegistryInterface::class); | ||
$this->userMock = \Phake::mock(UserInterface::class); | ||
|
||
\Phake::when($this->registryMock) | ||
->getRepository(UserNotificationInstance::class) | ||
->thenReturn($this->repositoryMock) | ||
; | ||
|
||
$this->ns = new NotificationService($this->registryMock); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
*/ | ||
public function testFetchOneByUIDAndRecipient_generalized() | ||
{ | ||
$uid = UID::parse('foo'); | ||
|
||
$this->ns->fetchOneByUIDAndRecipient($uid, $this->userMock); | ||
} | ||
|
||
public function testFetchOneByUIDAndRecipient_userSpecific() | ||
{ | ||
\Phake::when($this->repositoryMock) | ||
->findOneBy($this->anything()) | ||
->thenReturn('foo-notification') | ||
; | ||
|
||
$uid = UID::parse('foo:1234'); | ||
|
||
$this->assertEquals( | ||
'foo-notification', | ||
$this->ns->fetchOneByUIDAndRecipient($uid, $this->userMock) | ||
); | ||
|
||
\Phake::verify($this->repositoryMock) | ||
->findOneBy(array('recipient' => $this->userMock, 'definition' => '1234')) | ||
; | ||
} | ||
|
||
public function testFetchOneByUIDAndRecipient() | ||
{ | ||
\Phake::when($this->repositoryMock) | ||
->find($this->anything()) | ||
->thenReturn('foo-notification') | ||
; | ||
|
||
$uid = UID::parse('foo:1234:true'); | ||
|
||
$this->assertEquals( | ||
'foo-notification', | ||
$this->ns->fetchOneByUIDAndRecipient($uid, $this->userMock) | ||
); | ||
|
||
\Phake::verify($this->repositoryMock) | ||
->find('1234') | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
namespace Modera\NotificationBundle\Tests\Unit\Transport; | ||
|
||
use Modera\NotificationBundle\Entity\NotificationDefinition; | ||
use Modera\NotificationBundle\Transport\InvalidUIDFormatException; | ||
use Modera\NotificationBundle\Transport\UID; | ||
use Modera\NotificationBundle\Model\NotificationInterface; | ||
|
||
/** | ||
* @author Sergei Lissovski <[email protected]> | ||
* @copyright 2016 Modera Foundation | ||
*/ | ||
class UIDTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testGeneralized() | ||
{ | ||
$uid = UID::parse('foo'); | ||
|
||
$this->assertEquals('foo', $uid->getGroup()); | ||
$this->assertNull($uid->getNotification()); | ||
$this->assertTrue($uid->isGeneralized()); | ||
$this->assertFalse($uid->isSpecific()); | ||
} | ||
|
||
public function testSpecific() | ||
{ | ||
$uid = UID::parse('foo:12'); | ||
|
||
$this->assertEquals('foo', $uid->getGroup()); | ||
$this->assertEquals('12', $uid->getNotification()); | ||
$this->assertFalse($uid->isGeneralized()); | ||
$this->assertTrue($uid->isSpecific()); | ||
} | ||
|
||
public function testWithInvalidSyntax() | ||
{ | ||
$thrownException = null; | ||
|
||
try { | ||
UID::parse('foo:bar:baz:yoyo'); | ||
} catch (InvalidUIDFormatException $e) { | ||
$thrownException = $e; | ||
} | ||
|
||
$this->assertNotNull($thrownException); | ||
$this->assertEquals('foo:bar:baz:yoyo', $thrownException->getUID()); | ||
} | ||
|
||
public function testCreateFromExistingNotification() | ||
{ | ||
$notification = \Phake::mock(NotificationInterface::class); | ||
|
||
\Phake::when($notification) | ||
->getId() | ||
->thenReturn('123') | ||
; | ||
\Phake::when($notification) | ||
->getGroup() | ||
->thenReturn('foo') | ||
; | ||
|
||
$uid = UID::create($notification); | ||
|
||
$this->assertEquals('foo', $uid->getGroup()); | ||
$this->assertEquals('123', $uid->getNotification()); | ||
$this->assertTrue($uid->isSpecific()); | ||
$this->assertTrue($uid->isUserSpecific()); | ||
} | ||
|
||
public function testCreateFromDefinition() | ||
{ | ||
$def = \Phake::mock(NotificationDefinition::class); | ||
|
||
\Phake::when($def) | ||
->getId() | ||
->thenReturn('123') | ||
; | ||
\Phake::when($def) | ||
->getGroupName() | ||
->thenReturn('foo') | ||
; | ||
|
||
$uid = UID::create($def); | ||
|
||
$this->assertEquals('foo', $uid->getGroup()); | ||
$this->assertEquals('123', $uid->getNotification()); | ||
$this->assertTrue($uid->isSpecific()); | ||
$this->assertFalse($uid->isUserSpecific()); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
*/ | ||
public function testCreateInvalidArgument() | ||
{ | ||
UID::create(new \stdClass()); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public function testUserSpecific() | ||
{ | ||
$uid = UID::parse('foo:12:true'); | ||
|
||
$this->assertEquals('foo', $uid->getGroup()); | ||
$this->assertEquals('12', $uid->getNotification()); | ||
$this->assertFalse($uid->isGeneralized()); | ||
$this->assertTrue($uid->isSpecific()); | ||
$this->assertTrue($uid->isUserSpecific()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Modera\NotificationBundle\Transport; | ||
|
||
/** | ||
* @author Sergei Lissovski <[email protected]> | ||
* @copyright 2016 Modera Foundation | ||
*/ | ||
class InvalidUIDFormatException extends \RuntimeException | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $uid; | ||
|
||
/** | ||
* @param string $uid | ||
* @param string $message | ||
* | ||
* @return InvalidUIDFormatException | ||
*/ | ||
public static function create($uid, $message) | ||
{ | ||
$me = new static($message); | ||
$me->uid = $uid; | ||
|
||
return $me; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getUID() | ||
{ | ||
return $this->uid; | ||
} | ||
} |
Oops, something went wrong.