Skip to content

Commit

Permalink
EZEE-2081: Move NotificationBundle into Kernel (#2356)
Browse files Browse the repository at this point in the history
* EZEE-2081: Move NotificationBundle into Kernel

* EZEE-2081: Notification Cache Handler

* EZEE-2081: Feature goes into 7.2

* fixup! EZEE-2081: Notification Cache Handler

* EZEE-2081: use alias fix

* EZEE-2081: Typehints and docblocks fix

* EZEE-2081: introduce API Notification value object

* EZEE-2081: Impl. cache layer for \eZ\Publish\SPI\Persistence\Notification\Handler

* EZEE-2081: Updated database schema

* fixup! EZEE-2081: Updated database schema

* EZEE-2081: Update Notifications fixes

* EZEE-2081: psr-2 fixes

* EZEE-2081: wrong return value from createNotification fix

* EZEE-2081: Create struct

* EZEE-2081: Impl. exception conversion layer for notifications gateway.

* EZEE-2081: psr2 fix

* EZEE-2081: Impl. unit tests for \eZ\Publish\Core\Persistence\Legacy\Notification\Mapper

* EZEE-2081: Slot Notification Service

* EZEE-2081: service definition is now public

* EZEE-2081: Delete signal added

* EZEE-2081: Notification service slot fix

* EZEE-2081: after review fixes - missing docblocks and some minor improv

* EZEE-2081: Impl. unit tests for SPI Cache, SignalSlot layer, Notification/Handler (partial)

* EZEE-2081: Impl. integration tests for NotificationService

* fixup! EZEE-2081: Impl. integration tests for NotificationService

* EZEE-2081: Impl. tests for \eZ\Publish\Core\Persistence\Legacy\Notification\Gateway\DoctrineDatabase

* EZEE-2081: Changed type of eznotification.data columny from bytea to text

* fixup! EZEE-2081: Changed type of eznotification.data columny from bytea to text

* fixup! EZEE-2081: Impl. unit tests for SPI Cache, SignalSlot layer, Notification/Handler (partial)

* EZEE-2081: CS
  • Loading branch information
ViniTou authored and Łukasz Serwatka committed Jun 27, 2018
1 parent 63eb61c commit 679d357
Show file tree
Hide file tree
Showing 7 changed files with 345 additions and 0 deletions.
66 changes: 66 additions & 0 deletions Repository/NotificationService.php
@@ -0,0 +1,66 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\API\Repository;

use eZ\Publish\API\Repository\Values\Notification\CreateStruct;
use eZ\Publish\API\Repository\Values\Notification\Notification;
use eZ\Publish\API\Repository\Values\Notification\NotificationList;

interface NotificationService
{
/**
* Get currently logged user notifications.
* @param int $offset
* @param int $limit
*
* @return \eZ\Publish\API\Repository\Values\Notification\NotificationList
*/
public function loadNotifications(int $offset, int $limit): NotificationList;

/**
* @param int $notificationId
*
* @return \eZ\Publish\API\Repository\Values\Notification\Notification
*/
public function getNotification(int $notificationId): Notification;

/**
* Mark notification as read so it no longer bother the user.
*
* @param \eZ\Publish\API\Repository\Values\Notification\Notification $notification
*/
public function markNotificationAsRead(Notification $notification): void;

/**
* Get count of unread users notifications.
*
* @return int
*/
public function getPendingNotificationCount(): int;

/**
* Get count of total users notifications.
*
* @return int
*/
public function getNotificationCount(): int;

/**
* @param \eZ\Publish\API\Repository\Values\Notification\Notification $notification
*/
public function deleteNotification(Notification $notification): void;

/**
* @param \eZ\Publish\API\Repository\Values\Notification\CreateStruct $createStruct
*
* @return \eZ\Publish\API\Repository\Values\Notification\Notification
*/
public function createNotification(CreateStruct $createStruct): Notification;
}
7 changes: 7 additions & 0 deletions Repository/Repository.php
Expand Up @@ -212,6 +212,13 @@ public function getURLService();
*/
public function getBookmarkService();

/**
* Get NotificationService.
*
* @return \eZ\Publish\API\Repository\NotificationService
*/
public function getNotificationService();

/**
* Begin transaction.
*
Expand Down
157 changes: 157 additions & 0 deletions Repository/Tests/NotificationServiceTest.php
@@ -0,0 +1,157 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\API\Repository\Tests;

use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\Values\Notification\CreateStruct;
use eZ\Publish\API\Repository\Values\Notification\Notification;
use eZ\Publish\API\Repository\Values\Notification\NotificationList;

/**
* Test case for the NotificationService.
*
* @see \eZ\Publish\API\Repository\NotificationService
*/
class NotificationServiceTest extends BaseTest
{
/**
* @covers \eZ\Publish\API\Repository\NotificationService::loadNotifications()
*/
public function testLoadNotifications()
{
$repository = $this->getRepository();

/* BEGIN: Use Case */
$notificationService = $repository->getNotificationService();
$notificationList = $notificationService->loadNotifications(0, 25);
/* END: Use Case */

$this->assertInstanceOf(NotificationList::class, $notificationList);
$this->assertInternalType('array', $notificationList->items);
$this->assertInternalType('int', $notificationList->totalCount);
$this->assertEquals(5, $notificationList->totalCount);
}

/**
* @covers \eZ\Publish\API\Repository\NotificationService::getNotification()
*/
public function testGetNotification()
{
$repository = $this->getRepository();

$notificationId = $this->generateId('notification', 5);

/* BEGIN: Use Case */
$notificationService = $repository->getNotificationService();
// $notificationId is the ID of an existing notification
$notification = $notificationService->getNotification($notificationId);
/* END: Use Case */

$this->assertInstanceOf(Notification::class, $notification);
$this->assertEquals($notificationId, $notification->id);
}

/**
* @covers \eZ\Publish\API\Repository\NotificationService::markNotificationAsRead()
*/
public function testMarkNotificationAsRead()
{
$repository = $this->getRepository();

$notificationId = $this->generateId('notification', 5);
/* BEGIN: Use Case */
$notificationService = $repository->getNotificationService();

$notification = $notificationService->getNotification($notificationId);
$notificationService->markNotificationAsRead($notification);
$notification = $notificationService->getNotification($notificationId);
/* END: Use Case */

$this->assertFalse($notification->isPending);
}

/**
* @covers \eZ\Publish\API\Repository\NotificationService::getPendingNotificationCount()
*/
public function testGetPendingNotificationCount()
{
$repository = $this->getRepository();

/* BEGIN: Use Case */
$notificationService = $repository->getNotificationService();
$notificationPendingCount = $notificationService->getPendingNotificationCount();
/* END: Use Case */

$this->assertEquals(3, $notificationPendingCount);
}

/**
* @covers \eZ\Publish\API\Repository\NotificationService::getNotificationCount()
*/
public function testGetNotificationCount()
{
$repository = $this->getRepository();

/* BEGIN: Use Case */
$notificationService = $repository->getNotificationService();
$notificationCount = $notificationService->getNotificationCount();
/* END: Use Case */

$this->assertEquals(5, $notificationCount);
}

/**
* @covers \eZ\Publish\API\Repository\NotificationService::deleteNotification()
*/
public function testDeleteNotification()
{
$repository = $this->getRepository();

$notificationId = $this->generateId('notification', 5);
/* BEGIN: Use Case */
$notificationService = $repository->getNotificationService();
$notification = $notificationService->getNotification($notificationId);
$notificationService->deleteNotification($notification);
/* END: Use Case */

try {
$notificationService->getNotification($notificationId);
$this->fail('Notification ' . $notificationId . ' not deleted.');
} catch (NotFoundException $e) {
}
}

/**
* @covers \eZ\Publish\API\Repository\NotificationService::createNotification()
*/
public function testCreateNotification()
{
$repository = $this->getRepository();

/* BEGIN: Use Case */
$notificationService = $repository->getNotificationService();
$user = $repository->getUserService()->loadUser(14);

$createStruct = new CreateStruct([
'ownerId' => $user->id,
'type' => 'TEST',
'data' => [
'foo' => 'Foo',
'bar' => 'Bar',
'baz' => 'Baz',
],
]);

$notification = $notificationService->createNotification($createStruct);
/* END: Use Case */

$this->assertInstanceOf(Notification::class, $notification);
$this->assertGreaterThan(0, $notification->id);
}
}
17 changes: 17 additions & 0 deletions Repository/Tests/RepositoryTest.php
Expand Up @@ -9,6 +9,7 @@
namespace eZ\Publish\API\Repository\Tests;

use Exception;
use eZ\Publish\API\Repository\NotificationService;
use eZ\Publish\API\Repository\Repository;
use eZ\Publish\Core\Repository\Values\User\UserReference;

Expand Down Expand Up @@ -127,6 +128,22 @@ public function testGetUserService()
);
}

/**
* Test for the getNotificationService() method.
*
* @group user
*
* @see \eZ\Publish\API\Repository\Repository::getNotificationService()
*/
public function testGetNotificationService()
{
$repository = $this->getRepository();
$this->assertInstanceOf(
NotificationService::class,
$repository->getNotificationService()
);
}

/**
* Test for the getTrashService() method.
*
Expand Down
26 changes: 26 additions & 0 deletions Repository/Values/Notification/CreateStruct.php
@@ -0,0 +1,26 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\API\Repository\Values\Notification;

use eZ\Publish\API\Repository\Values\ValueObject;

class CreateStruct extends ValueObject
{
/** @var int $ownerId */
public $ownerId;

/** @var string $type */
public $type;

/** @var bool $isPending */
public $isPending = true;

/** @var array $data */
public $data = [];
}
42 changes: 42 additions & 0 deletions Repository/Values/Notification/Notification.php
@@ -0,0 +1,42 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\API\Repository\Values\Notification;

use eZ\Publish\API\Repository\Values\ValueObject;

/**
* This class represents a notification value.
*
* @property-read int $id The ID of notification
* @property-read int $ownerId The ID of notification owner
* @property-read bool $isPending True if notification is unreaded
* @property-read string $type Notification type
* @property-read \DateTimeInterface $created Creation date.
* @property-read array $data Optional context data
*/
class Notification extends ValueObject
{
/** @var int $id */
protected $id;

/** @var int $ownerId */
protected $ownerId;

/** @var bool $isPending */
protected $isPending;

/** @var string $type */
protected $type;

/** @var \DateTimeInterface $created */
protected $created;

/** @var array $data */
protected $data = [];
}
30 changes: 30 additions & 0 deletions Repository/Values/Notification/NotificationList.php
@@ -0,0 +1,30 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\API\Repository\Values\Notification;

use ArrayIterator;
use IteratorAggregate;
use eZ\Publish\API\Repository\Values\ValueObject;

class NotificationList extends ValueObject implements IteratorAggregate
{
/** @var int */
public $totalCount = 0;

/** @var \eZ\Publish\API\Repository\Values\Notification\Notification[] */
public $items = [];

/**
* {@inheritdoc}
*/
public function getIterator()
{
return new ArrayIterator($this->items);
}
}

0 comments on commit 679d357

Please sign in to comment.