Skip to content

Commit

Permalink
[MPFE-997] some refactoring, UID class has been moved to this bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeil committed Apr 26, 2017
1 parent 5fe055f commit 470124f
Show file tree
Hide file tree
Showing 8 changed files with 847 additions and 469 deletions.
8 changes: 7 additions & 1 deletion Dispatching/NotificationCenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
namespace Modera\NotificationBundle\Dispatching;

use Doctrine\ORM\EntityManager;
use Modera\NotificationBundle\Transport\UID;
use Modera\NotificationBundle\Entity\NotificationDefinition;
use Modera\NotificationBundle\Entity\UserNotificationInstance;
use Modera\NotificationBundle\Model\NotificationInterface;
use Modera\NotificationBundle\Service\NotificationService;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Component\Security\Core\User\UserInterface;

/**
* Service provides basic routines for manipulating notifications - dispatching(creating), querying, batch changing
Expand Down Expand Up @@ -63,6 +67,8 @@ public function createNotificationBuilder($message, $group)
* @throws ChannelNotFoundException
*
* @param NotificationBuilder $builder
*
* @return DeliveryReport
*/
public function dispatchUsingBuilder(NotificationBuilder $builder)
{
Expand Down Expand Up @@ -95,7 +101,7 @@ public function dispatchUsingBuilder(NotificationBuilder $builder)
$em->persist($def);
$em->flush();

$report = new DeliveryReport($builder, $def->getId(), function (array $metaToContribute) use ($def) {
$report = new DeliveryReport($builder, UID::create($def), function (array $metaToContribute) use ($def) {
$def->setMeta(array_merge($def->getMeta(), $metaToContribute));
});

Expand Down
26 changes: 26 additions & 0 deletions Service/NotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Modera\NotificationBundle\Transport\UID;
use Modera\NotificationBundle\Entity\NotificationDefinition;
use Modera\NotificationBundle\Entity\UserNotificationInstance;
use Modera\NotificationBundle\Model\NotificationInterface;
Expand Down Expand Up @@ -232,6 +234,30 @@ public function fetchOneBy(array $arrayQuery)
return $result[0];
}

/**
* @param UID $uid
* @param UserInterface $user
*
* @return NotificationInterface|null
*/
public function fetchOneByUIDAndRecipient(UID $uid, UserInterface $user)
{
if ($uid->isGeneralized()) {
throw new \InvalidArgumentException("Non-generalized UID is expected.");
}

$repository = $this->doctrineRegistry->getRepository(UserNotificationInstance::clazz());

if ($uid->isUserSpecific()) {
return $repository->find($uid->getNotification());
} else {
return $repository->findOneBy(array(
'recipient' => $user,
'definition' => $uid->getNotification(),
));
}
}

/**
* Saves/updates notification.
*
Expand Down
10 changes: 6 additions & 4 deletions Tests/Functional/Dispatching/NotificationCenterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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());
Expand Down
89 changes: 89 additions & 0 deletions Tests/Unit/Service/NotificationServiceTest.php
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')
;
}
}
113 changes: 113 additions & 0 deletions Tests/Unit/Transport/UIDTest.php
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());
}
}
37 changes: 37 additions & 0 deletions Transport/InvalidUIDFormatException.php
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;
}
}
Loading

0 comments on commit 470124f

Please sign in to comment.