diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 96611d8..46ab887 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,7 +12,7 @@ jobs: strategy: fail-fast: false matrix: - php-version: ['8.1', '8.2', '8.3', '8.4'] + php-version: ['8.2', '8.3', '8.4'] os: [ubuntu-latest, windows-latest] steps: - uses: actions/checkout@v4 @@ -44,11 +44,11 @@ jobs: vendor/bin/phpunit --configuration=phpunit.xml --display-deprecations --display-incomplete --display-skipped --coverage-clover=coverage.xml - name: Run PHPUnit without coverage - if: matrix.php-version != '8.2' || matrix.os != 'ubuntu-latest' + if: matrix.php-version != '8.3' || matrix.os != 'ubuntu-latest' run: vendor/bin/phpunit --configuration=phpunit.xml - name: Submit code coverage - if: matrix.php-version == '8.2' && matrix.os == 'ubuntu-latest' + if: matrix.php-version == '8.3' && matrix.os == 'ubuntu-latest' uses: codecov/codecov-action@v3 cs-stan: @@ -61,7 +61,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.2' + php-version: '8.3' extensions: mbstring, intl, pdo_sqlite coverage: none tools: phive diff --git a/CHANGELOG.md b/CHANGELOG.md index 53e2019..4a6cc74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,19 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.1.0] + +### Added + +- `NotifiableTrait` for providing notification methods on table classes + +### Changed + +- **BREAKING**: Refactored `NotifiableBehavior` - notification methods moved to `NotifiableTrait` + - Behavior now only creates the `Notifications` association + - Table classes must use both `NotifiableTrait` and `NotifiableBehavior` + - See updated documentation for migration guide + ## [1.0.1] ### Added diff --git a/composer.json b/composer.json index 02bc2f7..fafec4c 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "crustum/plugin-manifest": "^1.0" }, "require-dev": { - "phpunit/phpunit": "^10.5", + "phpunit/phpunit": "^10.5.5 || ^11.1.3 || ^12.5.0", "ratchet/pawl": "^0.4", "cakephp/bake": "^3.0", "react/http": "^1.7", @@ -45,13 +45,21 @@ } }, "scripts": { + "check": [ + "@cs-check", + "@test", + "@analyse" + ], + "analyse": [ + "@stan" + ], "cs-check": "phpcs -p --standard=phpcs.xml src/ tests/", "cs-fix": "phpcbf --standard=phpcs.xml src/ tests/", "test": "phpunit", "test:unit": "phpunit --testsuite=Unit", "test:integration": "phpunit --testsuite=Integration", "test:coverage": "phpunit --coverage-html coverage", - "stan": "tools/phpstan analyse src/", + "stan": "tools/phpstan analyse src/ tests/", "psalm": "tools/psalm --show-info=false", "stan-tests": "tools/phpstan analyze -c tests/phpstan.neon", "stan-baseline": "tools/phpstan --generate-baseline" diff --git a/docs/index.md b/docs/index.md index 41426a7..2250ea2 100644 --- a/docs/index.md +++ b/docs/index.md @@ -92,16 +92,19 @@ bin/cake bake notification InvoicePaid --channels database,mail,slack ### Using the Notifiable Behavior -Notifications may be sent in two ways: using the `notify()` method provided by the `NotifiableBehavior` or using the `NotificationManager`. The `NotifiableBehavior` must be added to your Table class: +Notifications may be sent in two ways: using the `notify()` method provided by the `NotifiableTrait` or using the `NotificationManager`. To enable notifications on a table, you must use both the `NotifiableTrait` and add the `NotifiableBehavior`: ```php notify($user, new InvoicePaid($invoice)); ``` > [!NOTE] -> You may add the `Notifiable` behavior to any of your tables. You are not limited to only including it on your `Users` table. +> You may add the `NotifiableTrait` and `NotifiableBehavior` to any of your tables. You are not limited to only including it on your `Users` table. ### Using the NotificationManager diff --git a/phpstan.neon b/phpstan.neon index 92fd390..678d222 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -8,8 +8,5 @@ parameters: ignoreErrors: - identifier: missingType.generics - - - identifier: trait.unused - path: src/TestSuite/NotificationTrait.php services: diff --git a/src/Model/Behavior/NotifiableBehavior.php b/src/Model/Behavior/NotifiableBehavior.php index 6ceac28..30c44ad 100644 --- a/src/Model/Behavior/NotifiableBehavior.php +++ b/src/Model/Behavior/NotifiableBehavior.php @@ -3,25 +3,26 @@ namespace Crustum\Notification\Model\Behavior; -use Cake\Datasource\EntityInterface; use Cake\ORM\Behavior; -use Cake\ORM\Query\SelectQuery; -use Crustum\Notification\Notification; -use Crustum\Notification\NotificationManager; /** * Notifiable Behavior * - * Makes an entity/model notifiable by automatically creating association to Notifications table - * and providing methods to send notifications. + * Creates hasMany association to Notifications table. For methods, use NotifiableTrait. * * Usage: * ``` - * // In your UsersTable - * $this->addBehavior('Crustum/Notification.Notifiable'); + * use Crustum\Notification\Model\Trait\NotifiableTrait; * - * // Then use on entities - * $user->notify(new WelcomeNotification()); + * class UsersTable extends Table + * { + * use NotifiableTrait; + * + * public function initialize(array $config): void + * { + * $this->addBehavior('Crustum/Notification.Notifiable'); + * } + * } * ``` * * @property \Cake\ORM\Table $_table @@ -31,22 +32,9 @@ class NotifiableBehavior extends Behavior /** * Default configuration * - * Configuration options: - * - implementedMethods: Methods to expose on the table/entity - * * @var array */ - protected array $_defaultConfig = [ - 'implementedMethods' => [ - 'notify' => 'notify', - 'notifyNow' => 'notifyNow', - 'routeNotificationFor' => 'routeNotificationFor', - 'markNotificationAsRead' => 'markNotificationAsRead', - 'markAllNotificationsAsRead' => 'markAllNotificationsAsRead', - 'unreadNotifications' => 'unreadNotifications', - 'readNotifications' => 'readNotifications', - ], - ]; + protected array $_defaultConfig = []; /** * Initialize hook @@ -71,141 +59,4 @@ public function initialize(array $config): void 'cascadeCallbacks' => true, ]); } - - /** - * Send a notification to the given entity - * - * The notification will be sent through all channels defined in the notification's via() method. - * If the notification implements ShouldQueueInterface, it will be queued for async processing. - * - * @param \Cake\Datasource\EntityInterface $entity The entity to notify - * @param \Crustum\Notification\Notification $notification The notification instance - * @return void - */ - public function notify(EntityInterface $entity, Notification $notification): void - { - NotificationManager::send($entity, $notification); - } - - /** - * Send a notification immediately, bypassing the queue - * - * The notification will be sent immediately through the specified channels, - * even if it implements ShouldQueueInterface. - * - * @param \Cake\Datasource\EntityInterface $entity The entity to notify - * @param \Crustum\Notification\Notification $notification The notification instance - * @param array|null $channels Optional array of channel names to send through - * @return void - */ - public function notifyNow(EntityInterface $entity, Notification $notification, ?array $channels = null): void - { - NotificationManager::sendNow($entity, $notification, $channels); - } - - /** - * Get the routing information for a given notification channel - * - * This method checks for a specific routing method on the entity first, - * then falls back to default routing for known channels. - * - * @param object $entity The entity to get routing info for - * @param string $channel The channel name (e.g., 'database', 'mail', 'slack') - * @param \Crustum\Notification\Notification|null $notification The notification instance - * @return mixed Routing information for the channel - */ - public function routeNotificationFor(object $entity, string $channel, ?Notification $notification = null): mixed - { - $method = 'routeNotificationFor' . ucfirst($channel); - - if (method_exists($entity, $method)) { - return $entity->{$method}($notification); - } - - if ($channel === 'database') { - return $this->_table->getAssociation('Notifications'); - } - - return null; - } - - /** - * Mark a notification as read for this entity - * - * @param object $entity The entity - * @param string $notificationId The notification ID - * @return bool True if marked as read - */ - public function markNotificationAsRead(object $entity, string $notificationId): bool - { - /** @var \Crustum\Notification\Model\Table\NotificationsTable $notificationsTable */ - $notificationsTable = $this->_table->getAssociation('Notifications')->getTarget(); - - return $notificationsTable->markAsRead($notificationId); - } - - /** - * Mark all notifications as read for this entity - * - * @param \Cake\Datasource\EntityInterface $entity The entity - * @return int Number of notifications marked as read - */ - public function markAllNotificationsAsRead(EntityInterface $entity): int - { - /** @var \Crustum\Notification\Model\Table\NotificationsTable $notificationsTable */ - $notificationsTable = $this->_table->getAssociation('Notifications')->getTarget(); - $primaryKey = $this->_table->getPrimaryKey(); - if (is_array($primaryKey)) { - $primaryKey = $primaryKey[0]; - } - - return $notificationsTable->markAllAsRead( - $this->_table->getAlias(), - (string)$entity->get($primaryKey), - ); - } - - /** - * Get unread notifications query for this entity - * - * @param \Cake\Datasource\EntityInterface $entity The entity - * @return \Cake\ORM\Query\SelectQuery<\Crustum\Notification\Model\Entity\Notification> - * @phpstan-return \Cake\ORM\Query\SelectQuery<\Crustum\Notification\Model\Entity\Notification> - */ - public function unreadNotifications(EntityInterface $entity): SelectQuery - { - /** @var \Crustum\Notification\Model\Table\NotificationsTable $notificationsTable */ - $notificationsTable = $this->_table->getAssociation('Notifications')->getTarget(); - $primaryKey = $this->_table->getPrimaryKey(); - if (is_array($primaryKey)) { - $primaryKey = $primaryKey[0]; - } - - /** @phpstan-var \Cake\ORM\Query\SelectQuery<\Crustum\Notification\Model\Entity\Notification> */ - return $notificationsTable - ->find('forModel', model: $this->_table->getAlias(), foreign_key: (string)$entity->get($primaryKey)) - ->find('unread'); - } - - /** - * Get read notifications query for this entity - * - * @param \Cake\Datasource\EntityInterface $entity The entity - * @return \Cake\ORM\Query\SelectQuery<\Crustum\Notification\Model\Entity\Notification> - * @phpstan-return \Cake\ORM\Query\SelectQuery<\Crustum\Notification\Model\Entity\Notification> - */ - public function readNotifications(EntityInterface $entity): SelectQuery - { - /** @var \Crustum\Notification\Model\Table\NotificationsTable $notificationsTable */ - $notificationsTable = $this->_table->getAssociation('Notifications')->getTarget(); - $primaryKey = $this->_table->getPrimaryKey(); - if (is_array($primaryKey)) { - $primaryKey = $primaryKey[0]; - } - - /** @phpstan-var \Cake\ORM\Query\SelectQuery<\Crustum\Notification\Model\Entity\Notification> */ - return $notificationsTable - ->find('forModel', model: $this->_table->getAlias(), foreign_key: (string)$entity->get($primaryKey)) - ->find('read'); - } } diff --git a/src/Model/Trait/NotifiableTrait.php b/src/Model/Trait/NotifiableTrait.php new file mode 100644 index 0000000..3f57921 --- /dev/null +++ b/src/Model/Trait/NotifiableTrait.php @@ -0,0 +1,170 @@ +addBehavior('Crustum/Notification.Notifiable'); + * } + * } + * ``` + */ +trait NotifiableTrait +{ + /** + * Send a notification to the given entity + * + * The notification will be sent through all channels defined in the notification's via() method. + * If the notification implements ShouldQueueInterface, it will be queued for async processing. + * + * @param \Cake\Datasource\EntityInterface $entity The entity to notify + * @param \Crustum\Notification\Notification $notification The notification instance + * @return void + */ + public function notify(EntityInterface $entity, Notification $notification): void + { + NotificationManager::send($entity, $notification); + } + + /** + * Send a notification immediately, bypassing the queue + * + * The notification will be sent immediately through the specified channels, + * even if it implements ShouldQueueInterface. + * + * @param \Cake\Datasource\EntityInterface $entity The entity to notify + * @param \Crustum\Notification\Notification $notification The notification instance + * @param array|null $channels Optional array of channel names to send through + * @return void + */ + public function notifyNow(EntityInterface $entity, Notification $notification, ?array $channels = null): void + { + NotificationManager::sendNow($entity, $notification, $channels); + } + + /** + * Get the routing information for a given notification channel + * + * This method checks for a specific routing method on the entity first, + * then falls back to default routing for known channels. + * + * @param object $entity The entity to get routing info for + * @param string $channel The channel name (e.g., 'database', 'mail', 'slack') + * @param \Crustum\Notification\Notification|null $notification The notification instance + * @return mixed Routing information for the channel + */ + public function routeNotificationFor(object $entity, string $channel, ?Notification $notification = null): mixed + { + $method = 'routeNotificationFor' . ucfirst($channel); + + if (method_exists($entity, $method)) { + return $entity->{$method}($notification); + } + + if ($channel === 'database') { + return $this->getAssociation('Notifications'); + } + + return null; + } + + /** + * Mark a notification as read for this entity + * + * @param object $entity The entity + * @param string $notificationId The notification ID + * @return bool True if marked as read + */ + public function markNotificationAsRead(object $entity, string $notificationId): bool + { + /** @var \Crustum\Notification\Model\Table\NotificationsTable $notificationsTable */ + $notificationsTable = $this->getAssociation('Notifications')->getTarget(); + + return $notificationsTable->markAsRead($notificationId); + } + + /** + * Mark all notifications as read for this entity + * + * @param \Cake\Datasource\EntityInterface $entity The entity + * @return int Number of notifications marked as read + */ + public function markAllNotificationsAsRead(EntityInterface $entity): int + { + /** @var \Crustum\Notification\Model\Table\NotificationsTable $notificationsTable */ + $notificationsTable = $this->getAssociation('Notifications')->getTarget(); + $primaryKey = $this->getPrimaryKey(); + if (is_array($primaryKey)) { + $primaryKey = $primaryKey[0]; + } + + return $notificationsTable->markAllAsRead( + $this->getAlias(), + (string)$entity->get($primaryKey), + ); + } + + /** + * Get unread notifications query for this entity + * + * @param \Cake\Datasource\EntityInterface $entity The entity + * @return \Cake\ORM\Query\SelectQuery<\Crustum\Notification\Model\Entity\Notification> + * @phpstan-return \Cake\ORM\Query\SelectQuery<\Crustum\Notification\Model\Entity\Notification> + */ + public function unreadNotifications(EntityInterface $entity): SelectQuery + { + /** @var \Crustum\Notification\Model\Table\NotificationsTable $notificationsTable */ + $notificationsTable = $this->getAssociation('Notifications')->getTarget(); + $primaryKey = $this->getPrimaryKey(); + if (is_array($primaryKey)) { + $primaryKey = $primaryKey[0]; + } + + /** @phpstan-var \Cake\ORM\Query\SelectQuery<\Crustum\Notification\Model\Entity\Notification> */ + return $notificationsTable + ->find('forModel', model: $this->getAlias(), foreign_key: (string)$entity->get($primaryKey)) + ->find('unread'); + } + + /** + * Get read notifications query for this entity + * + * @param \Cake\Datasource\EntityInterface $entity The entity + * @return \Cake\ORM\Query\SelectQuery<\Crustum\Notification\Model\Entity\Notification> + * @phpstan-return \Cake\ORM\Query\SelectQuery<\Crustum\Notification\Model\Entity\Notification> + */ + public function readNotifications(EntityInterface $entity): SelectQuery + { + /** @var \Crustum\Notification\Model\Table\NotificationsTable $notificationsTable */ + $notificationsTable = $this->getAssociation('Notifications')->getTarget(); + $primaryKey = $this->getPrimaryKey(); + if (is_array($primaryKey)) { + $primaryKey = $primaryKey[0]; + } + + /** @phpstan-var \Cake\ORM\Query\SelectQuery<\Crustum\Notification\Model\Entity\Notification> */ + return $notificationsTable + ->find('forModel', model: $this->getAlias(), foreign_key: (string)$entity->get($primaryKey)) + ->find('read'); + } +} diff --git a/tests/TestApp/Controller/PostsController.php b/tests/TestApp/Controller/PostsController.php index dcb1bed..f140ab1 100644 --- a/tests/TestApp/Controller/PostsController.php +++ b/tests/TestApp/Controller/PostsController.php @@ -39,14 +39,16 @@ public function publish(?int $id = null) $postId = (int)($id ?? $this->request->getData('id')); $post = $this->Posts->get($postId, ['contain' => ['Users']]); $post->published = true; + /** @var \TestApp\Model\Table\UsersTable $UsersTable */ + $UsersTable = $this->Posts->Users; if ($this->Posts->save($post)) { $user = $post->user; - $this->Posts->Users->notify($user, new PostPublished($post->id, $post->title)); + $UsersTable->notify($user, new PostPublished($post->id, $post->title)); return $this->response ->withType('application/json') - ->withStringBody(json_encode([ + ->withStringBody((string)json_encode([ 'success' => true, 'post_id' => $post->id, ])); @@ -55,7 +57,7 @@ public function publish(?int $id = null) return $this->response ->withType('application/json') ->withStatus(400) - ->withStringBody(json_encode(['success' => false])); + ->withStringBody((string)json_encode(['success' => false])); } /** @@ -71,11 +73,14 @@ public function notify(): Response $postId = (int)$this->request->getData('post_id', 1); $title = $this->request->getData('title', 'Test Post'); - $user = $this->Posts->Users->get($userId); - $this->Posts->Users->notify($user, new PostPublished($postId, $title)); + /** @var \TestApp\Model\Table\UsersTable $UsersTable */ + $UsersTable = $this->Posts->Users; + + $user = $UsersTable->get($userId); + $UsersTable->notify($user, new PostPublished($postId, $title)); return $this->response ->withType('application/json') - ->withStringBody(json_encode(['success' => true])); + ->withStringBody((string)json_encode(['success' => true])); } } diff --git a/tests/TestApp/Model/Table/PostsTable.php b/tests/TestApp/Model/Table/PostsTable.php index 72fd2de..d7f816a 100644 --- a/tests/TestApp/Model/Table/PostsTable.php +++ b/tests/TestApp/Model/Table/PostsTable.php @@ -13,14 +13,14 @@ * * @property \Cake\ORM\Association\BelongsTo $Users * @method \TestApp\Model\Entity\Post newEmptyEntity() - * @method \TestApp\Model\Entity\Post newEntity(array $data, array $options = []) - * @method array<\TestApp\Model\Entity\Post> newEntities(array $data, array $options = []) - * @method \TestApp\Model\Entity\Post get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args) - * @method \TestApp\Model\Entity\Post findOrCreate($search, ?callable $callback = null, array $options = []) - * @method \TestApp\Model\Entity\Post patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) - * @method array<\TestApp\Model\Entity\Post> patchEntities(iterable $entities, array $data, array $options = []) - * @method \TestApp\Model\Entity\Post|false save(\Cake\Datasource\EntityInterface $entity, array $options = []) - * @method \TestApp\Model\Entity\Post saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = []) + * @method \TestApp\Model\Entity\Post newEntity(array $data, array $options = []) + * @method array<\TestApp\Model\Entity\Post> newEntities(array $data, array $options = []) + * @method \TestApp\Model\Entity\Post get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args) + * @method \TestApp\Model\Entity\Post findOrCreate($search, ?callable $callback = null, array $options = []) + * @method \TestApp\Model\Entity\Post patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) + * @method array<\TestApp\Model\Entity\Post> patchEntities(iterable<\Cake\Datasource\EntityInterface> $entities, array $data, array $options = []) + * @method \TestApp\Model\Entity\Post|false save(\Cake\Datasource\EntityInterface $entity, array $options = []) + * @method \TestApp\Model\Entity\Post saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = []) */ class PostsTable extends Table { @@ -43,7 +43,7 @@ public function initialize(array $config): void $this->belongsTo('Users', [ 'foreignKey' => 'user_id', 'joinType' => 'INNER', - 'className' => 'TestApp.Users', + 'className' => 'Users', ]); } @@ -91,10 +91,13 @@ public function validationDefault(Validator $validator): Validator */ public function afterSave(EventInterface $event, $entity, $options): void { + /** @var \TestApp\Model\Entity\Post $entity */ if ($entity->published && $entity->isDirty('published')) { - $user = $this->Users->get($entity->user_id); + /** @var \TestApp\Model\Table\UsersTable $usersTable */ + $usersTable = $this->Users->getTarget(); + $user = $usersTable->get($entity->user_id); - $this->Users->notify($user, new PostPublished($entity->id, $entity->title)); + $usersTable->notify($user, new PostPublished($entity->id, $entity->title)); } } } diff --git a/tests/TestApp/Model/Table/UsersTable.php b/tests/TestApp/Model/Table/UsersTable.php index ae8c092..fb98499 100644 --- a/tests/TestApp/Model/Table/UsersTable.php +++ b/tests/TestApp/Model/Table/UsersTable.php @@ -5,6 +5,7 @@ use Cake\ORM\Table; use Cake\Validation\Validator; +use Crustum\Notification\Model\Trait\NotifiableTrait; /** * Users Table @@ -12,17 +13,19 @@ * @property \Cake\ORM\Association\HasMany $Posts * @property \Cake\ORM\Association\HasMany $Notifications * @method \TestApp\Model\Entity\User newEmptyEntity() - * @method \TestApp\Model\Entity\User newEntity(array $data, array $options = []) - * @method array<\TestApp\Model\Entity\User> newEntities(array $data, array $options = []) - * @method \TestApp\Model\Entity\User get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args) - * @method \TestApp\Model\Entity\User findOrCreate($search, ?callable $callback = null, array $options = []) - * @method \TestApp\Model\Entity\User patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) - * @method array<\TestApp\Model\Entity\User> patchEntities(iterable $entities, array $data, array $options = []) - * @method \TestApp\Model\Entity\User|false save(\Cake\Datasource\EntityInterface $entity, array $options = []) - * @method \TestApp\Model\Entity\User saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = []) + * @method \TestApp\Model\Entity\User newEntity(array $data, array $options = []) + * @method array<\TestApp\Model\Entity\User> newEntities(array $data, array $options = []) + * @method \TestApp\Model\Entity\User get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args) + * @method \TestApp\Model\Entity\User findOrCreate($search, ?callable $callback = null, array $options = []) + * @method \TestApp\Model\Entity\User patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) + * @method array<\TestApp\Model\Entity\User> patchEntities(iterable<\Cake\Datasource\EntityInterface> $entities, array $data, array $options = []) + * @method \TestApp\Model\Entity\User|false save(\Cake\Datasource\EntityInterface $entity, array $options = []) + * @method \TestApp\Model\Entity\User saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = []) */ class UsersTable extends Table { + use NotifiableTrait; + /** * Initialize method * diff --git a/tests/TestCase/Channel/DatabaseChannelTest.php b/tests/TestCase/Channel/DatabaseChannelTest.php index d22c505..c89ec9e 100644 --- a/tests/TestCase/Channel/DatabaseChannelTest.php +++ b/tests/TestCase/Channel/DatabaseChannelTest.php @@ -37,6 +37,7 @@ public function testSendStoresNotification(): void $entity = new Entity(['id' => 99]); $entity->setSource('Users'); + /** @var \Crustum\Notification\Model\Entity\Notification $result */ $result = $channel->send($entity, $notification); $this->assertNotFalse($result); diff --git a/tests/TestCase/Channel/MailChannelTest.php b/tests/TestCase/Channel/MailChannelTest.php index b4aed9a..d81de8f 100644 --- a/tests/TestCase/Channel/MailChannelTest.php +++ b/tests/TestCase/Channel/MailChannelTest.php @@ -51,7 +51,6 @@ protected function setUp(): void */ protected function tearDown(): void { - unset($this->channel); TransportFactory::drop('test'); parent::tearDown(); diff --git a/tests/TestCase/Controller/NotificationIntegrationTest.php b/tests/TestCase/Controller/NotificationIntegrationTest.php index fe97070..c94537a 100644 --- a/tests/TestCase/Controller/NotificationIntegrationTest.php +++ b/tests/TestCase/Controller/NotificationIntegrationTest.php @@ -47,7 +47,7 @@ public function testControllerActionNotificationCaptured(): void $this->assertNotificationSent(PostPublished::class); - $usersTable = $this->getTableLocator()->get('TestApp.Users'); + $usersTable = $this->getTableLocator()->get('Users'); $user = $usersTable->get(1); $this->assertNotificationSentTo($user, PostPublished::class); diff --git a/tests/TestCase/Extension/ChannelProviderInterfaceTest.php b/tests/TestCase/Extension/ChannelProviderInterfaceTest.php index 9a0c607..ccb9169 100644 --- a/tests/TestCase/Extension/ChannelProviderInterfaceTest.php +++ b/tests/TestCase/Extension/ChannelProviderInterfaceTest.php @@ -24,8 +24,8 @@ public function testProviderImplementsInterface(): void $provider = new MockChannelProvider(); $this->assertInstanceOf(ChannelProviderInterface::class, $provider); - $this->assertIsArray($provider->provides()); - $this->assertIsArray($provider->getDefaultConfig()); + $this->assertArrayHasKey(0, $provider->provides()); + $this->assertArrayHasKey('timeout', $provider->getDefaultConfig()); } /** @@ -68,7 +68,6 @@ public function testProviderReturnsDefaultConfig(): void $config = $provider->getDefaultConfig(); - $this->assertIsArray($config); $this->assertArrayHasKey('timeout', $config); } } diff --git a/tests/TestCase/Extension/MockChannel.php b/tests/TestCase/Extension/MockChannel.php index 738b2f2..26e5f7c 100644 --- a/tests/TestCase/Extension/MockChannel.php +++ b/tests/TestCase/Extension/MockChannel.php @@ -12,10 +12,18 @@ class MockChannel implements ChannelInterface { /** - * @inheritDoc + * Channel configuration + * + * @var array + */ + protected array $config; + + /** + * @param array $config Channel configuration */ public function __construct(array $config = []) { + $this->config = $config; } /** diff --git a/tests/TestCase/Integration/PostNotificationTest.php b/tests/TestCase/Integration/PostNotificationTest.php index 818fee7..b096625 100644 --- a/tests/TestCase/Integration/PostNotificationTest.php +++ b/tests/TestCase/Integration/PostNotificationTest.php @@ -5,6 +5,8 @@ use Cake\TestSuite\TestCase; use Crustum\Notification\TestSuite\NotificationTrait; +use TestApp\Model\Table\PostsTable; +use TestApp\Model\Table\UsersTable; use TestApp\Notification\PostPublished; /** @@ -18,6 +20,10 @@ class PostNotificationTest extends TestCase { use NotificationTrait; + protected PostsTable $Posts; + + protected UsersTable $Users; + /** * Fixtures * @@ -37,6 +43,12 @@ class PostNotificationTest extends TestCase public function setUp(): void { parent::setUp(); + /** @var \TestApp\Model\Table\PostsTable $postsTable */ + $postsTable = $this->getTableLocator()->get('Posts'); + $this->Posts = $postsTable; + /** @var \TestApp\Model\Table\UsersTable $usersTable */ + $usersTable = $this->getTableLocator()->get('Users'); + $this->Users = $usersTable; } /** @@ -46,14 +58,11 @@ public function setUp(): void */ public function testNotificationSentWhenPostPublished(): void { - $postsTable = $this->getTableLocator()->get('TestApp.Posts'); - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - - $post = $postsTable->get(1); - $user = $usersTable->get($post->user_id); + $post = $this->Posts->get(1); + $user = $this->Users->get($post->user_id); $post->published = true; - $postsTable->save($post); + $this->Posts->save($post); $this->assertNotificationSentTo($user, PostPublished::class); $this->assertNotificationCount(1); @@ -66,11 +75,10 @@ public function testNotificationSentWhenPostPublished(): void */ public function testNotificationNotSentWhenPostNotPublished(): void { - $postsTable = $this->getTableLocator()->get('TestApp.Posts'); - $post = $postsTable->get(1); + $post = $this->Posts->get(1); $post->title = 'Updated Title'; - $postsTable->save($post); + $this->Posts->save($post); $this->assertNoNotificationsSent(); } @@ -82,11 +90,10 @@ public function testNotificationNotSentWhenPostNotPublished(): void */ public function testNotificationContainsPostData(): void { - $postsTable = $this->getTableLocator()->get('TestApp.Posts'); - $post = $postsTable->get(1); + $post = $this->Posts->get(1); $post->published = true; - $postsTable->save($post); + $this->Posts->save($post); $this->assertNotificationDataContains(PostPublished::class, 'post_id', 1); $this->assertNotificationDataContains(PostPublished::class, 'post_title', 'First Post'); @@ -99,15 +106,12 @@ public function testNotificationContainsPostData(): void */ public function testNotificationSentToPostOwner(): void { - $postsTable = $this->getTableLocator()->get('TestApp.Posts'); - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - - $post = $postsTable->get(1); - $owner = $usersTable->get($post->user_id); - $otherUser = $usersTable->get(2); + $post = $this->Posts->get(1); + $owner = $this->Users->get($post->user_id); + $otherUser = $this->Users->get(2); $post->published = true; - $postsTable->save($post); + $this->Posts->save($post); $this->assertNotificationSentTo($owner, PostPublished::class); $this->assertNotificationNotSentTo($otherUser, PostPublished::class); @@ -120,11 +124,10 @@ public function testNotificationSentToPostOwner(): void */ public function testNotificationSentThroughCorrectChannels(): void { - $postsTable = $this->getTableLocator()->get('TestApp.Posts'); - $post = $postsTable->get(1); + $post = $this->Posts->get(1); $post->published = true; - $postsTable->save($post); + $this->Posts->save($post); $this->assertNotificationSentToChannel('database', PostPublished::class); $this->assertNotificationSentToChannel('mail', PostPublished::class); @@ -137,15 +140,14 @@ public function testNotificationSentThroughCorrectChannels(): void */ public function testMultiplePostPublishSendsMultipleNotifications(): void { - $postsTable = $this->getTableLocator()->get('TestApp.Posts'); - $post1 = $postsTable->get(1); - $post3 = $postsTable->get(3); + $post1 = $this->Posts->get(1); + $post3 = $this->Posts->get(3); $post1->published = true; - $postsTable->save($post1); + $this->Posts->save($post1); $post3->published = true; - $postsTable->save($post3); + $this->Posts->save($post3); $this->assertNotificationSentTimes(PostPublished::class, 2); } diff --git a/tests/TestCase/Job/SendQueuedNotificationJobTest.php b/tests/TestCase/Job/SendQueuedNotificationJobTest.php index 55e6891..788b167 100644 --- a/tests/TestCase/Job/SendQueuedNotificationJobTest.php +++ b/tests/TestCase/Job/SendQueuedNotificationJobTest.php @@ -60,7 +60,6 @@ protected function setUp(): void */ protected function tearDown(): void { - unset($this->job); NotificationManager::drop('database'); TableRegistry::getTableLocator()->clear(); @@ -250,10 +249,7 @@ public function testComplexNotificationSerialization(): void */ protected function createMessage(array $arguments): Message { - $message = $this->getMockBuilder(Message::class) - ->disableOriginalConstructor() - ->onlyMethods(['getArgument']) - ->getMock(); + $message = $this->createStub(Message::class); $message->method('getArgument') ->willReturnCallback(function ($key) use ($arguments) { diff --git a/tests/TestCase/Model/Behavior/NotifiableBehaviorTest.php b/tests/TestCase/Model/Behavior/NotifiableBehaviorTest.php index a707bc7..9569513 100644 --- a/tests/TestCase/Model/Behavior/NotifiableBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/NotifiableBehaviorTest.php @@ -54,16 +54,17 @@ public function testAssociationHasModelCondition(): void $association = $table->getAssociation('Notifications'); $conditions = $association->getConditions(); + $this->assertIsArray($conditions); $this->assertArrayHasKey('Notifications.model', $conditions); $this->assertEquals('Users', $conditions['Notifications.model']); } /** - * Test that behavior implements required methods + * Test that behavior no longer implements methods (moved to trait) * * @return void */ - public function testImplementsRequiredMethods(): void + public function testBehaviorNoLongerImplementsMethods(): void { $table = TableRegistry::getTableLocator()->get('Users'); $table->addBehavior('Crustum/Notification.Notifiable'); @@ -71,9 +72,7 @@ public function testImplementsRequiredMethods(): void $behavior = $table->getBehavior('Notifiable'); $implementedMethods = $behavior->implementedMethods(); - $this->assertArrayHasKey('notify', $implementedMethods); - $this->assertArrayHasKey('notifyNow', $implementedMethods); - $this->assertArrayHasKey('routeNotificationFor', $implementedMethods); + $this->assertEmpty($implementedMethods); } /** @@ -83,6 +82,7 @@ public function testImplementsRequiredMethods(): void */ public function testRouteNotificationForDatabase(): void { + /** @var \TestApp\Model\Table\UsersTable $table */ $table = TableRegistry::getTableLocator()->get('Users'); $table->addBehavior('Crustum/Notification.Notifiable'); diff --git a/tests/TestCase/Model/Table/NotificationsTableTest.php b/tests/TestCase/Model/Table/NotificationsTableTest.php index 18d2583..7507734 100644 --- a/tests/TestCase/Model/Table/NotificationsTableTest.php +++ b/tests/TestCase/Model/Table/NotificationsTableTest.php @@ -39,7 +39,9 @@ public function setUp(): void { parent::setUp(); - $this->Notifications = TableRegistry::getTableLocator()->get('Crustum/Notification.Notifications'); + /** @var \Crustum\Notification\Model\Table\NotificationsTable $notificationsTable */ + $notificationsTable = TableRegistry::getTableLocator()->get('Crustum/Notification.Notifications'); + $this->Notifications = $notificationsTable; } /** @@ -49,7 +51,6 @@ public function setUp(): void */ public function tearDown(): void { - unset($this->Notifications); TableRegistry::getTableLocator()->clear(); parent::tearDown(); diff --git a/tests/TestCase/Registry/ChannelRegistryTest.php b/tests/TestCase/Registry/ChannelRegistryTest.php index b1340f3..284d7b0 100644 --- a/tests/TestCase/Registry/ChannelRegistryTest.php +++ b/tests/TestCase/Registry/ChannelRegistryTest.php @@ -68,6 +68,7 @@ public function testChannelsCanBeRegisteredViaDiscoveryEvent(): void EventManager::instance()->on( 'Notification.Registry.discover', function (Event $event): void { + /** @var \Crustum\Notification\Registry\ChannelRegistry $registry */ $registry = $event->getSubject(); $registry->load('test', [ 'className' => 'Crustum\Notification\Channel\DatabaseChannel', diff --git a/tests/TestCase/TestSuite/NotificationTraitTest.php b/tests/TestCase/TestSuite/NotificationTraitTest.php index c65c364..012fe35 100644 --- a/tests/TestCase/TestSuite/NotificationTraitTest.php +++ b/tests/TestCase/TestSuite/NotificationTraitTest.php @@ -43,7 +43,9 @@ class NotificationTraitTest extends TestCase public function setUp(): void { parent::setUp(); - $this->Users = $this->getTableLocator()->get('TestApp.Users'); + /** @var \TestApp\Model\Table\UsersTable $usersTable */ + $usersTable = $this->getTableLocator()->get('Users'); + $this->Users = $usersTable; } /** @@ -53,10 +55,9 @@ public function setUp(): void */ public function testAssertNotificationSent(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user = $usersTable->get(1); + $user = $this->Users->get(1); - $usersTable->notify($user, new PostPublished(1, 'Test Post')); + $this->Users->notify($user, new PostPublished(1, 'Test Post')); $this->assertNotificationSent(PostPublished::class); } @@ -68,10 +69,9 @@ public function testAssertNotificationSent(): void */ public function testAssertNotificationNotSent(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user = $usersTable->get(1); + $user = $this->Users->get(1); - $usersTable->notify($user, new PostPublished(1, 'Test Post')); + $this->Users->notify($user, new PostPublished(1, 'Test Post')); $this->assertNotificationNotSent(UserRegistered::class); } @@ -83,13 +83,12 @@ public function testAssertNotificationNotSent(): void */ public function testAssertNotificationCount(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user1 = $usersTable->get(1); - $user2 = $usersTable->get(2); + $user1 = $this->Users->get(1); + $user2 = $this->Users->get(2); - $usersTable->notify($user1, new PostPublished(1, 'Test Post')); - $usersTable->notify($user2, new PostPublished(2, 'Another Post')); - $usersTable->notify($user1, new AdminAlert('Alert Message')); + $this->Users->notify($user1, new PostPublished(1, 'Test Post')); + $this->Users->notify($user2, new PostPublished(2, 'Another Post')); + $this->Users->notify($user1, new AdminAlert('Alert Message')); $this->assertNotificationCount(3); } @@ -111,12 +110,11 @@ public function testAssertNoNotificationsSent(): void */ public function testAssertNotificationSentTo(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user1 = $usersTable->get(1); - $user2 = $usersTable->get(2); + $user1 = $this->Users->get(1); + $user2 = $this->Users->get(2); - $usersTable->notify($user1, new PostPublished(1, 'Test Post')); - $usersTable->notify($user2, new AdminAlert('Alert')); + $this->Users->notify($user1, new PostPublished(1, 'Test Post')); + $this->Users->notify($user2, new AdminAlert('Alert')); $this->assertNotificationSentTo($user1, PostPublished::class); $this->assertNotificationSentTo($user2, AdminAlert::class); @@ -129,11 +127,10 @@ public function testAssertNotificationSentTo(): void */ public function testAssertNotificationNotSentTo(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user1 = $usersTable->get(1); - $user2 = $usersTable->get(2); + $user1 = $this->Users->get(1); + $user2 = $this->Users->get(2); - $usersTable->notify($user1, new PostPublished(1, 'Test Post')); + $this->Users->notify($user1, new PostPublished(1, 'Test Post')); $this->assertNotificationNotSentTo($user2, PostPublished::class); $this->assertNotificationNotSentTo($user1, AdminAlert::class); @@ -146,10 +143,9 @@ public function testAssertNotificationNotSentTo(): void */ public function testAssertNotificationSentToChannel(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user = $usersTable->get(1); + $user = $this->Users->get(1); - $usersTable->notify($user, new PostPublished(1, 'Test Post')); + $this->Users->notify($user, new PostPublished(1, 'Test Post')); $this->assertNotificationSentToChannel('database', PostPublished::class); $this->assertNotificationSentToChannel('mail', PostPublished::class); @@ -177,13 +173,12 @@ public function testAssertOnDemandNotificationSent(): void */ public function testAssertNotificationSentTimes(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user1 = $usersTable->get(1); - $user2 = $usersTable->get(2); + $user1 = $this->Users->get(1); + $user2 = $this->Users->get(2); - $usersTable->notify($user1, new PostPublished(1, 'First Post')); - $usersTable->notify($user2, new PostPublished(2, 'Second Post')); - $usersTable->notify($user1, new PostPublished(3, 'Third Post')); + $this->Users->notify($user1, new PostPublished(1, 'First Post')); + $this->Users->notify($user2, new PostPublished(2, 'Second Post')); + $this->Users->notify($user1, new PostPublished(3, 'Third Post')); $this->assertNotificationSentTimes(PostPublished::class, 3); $this->assertNotificationSentTimes(AdminAlert::class, 0); @@ -196,12 +191,11 @@ public function testAssertNotificationSentTimes(): void */ public function testAssertNotificationSentToTimes(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user = $usersTable->get(1); + $user = $this->Users->get(1); - $usersTable->notify($user, new PostPublished(1, 'First Post')); - $usersTable->notify($user, new PostPublished(2, 'Second Post')); - $usersTable->notify($user, new AdminAlert('Alert')); + $this->Users->notify($user, new PostPublished(1, 'First Post')); + $this->Users->notify($user, new PostPublished(2, 'Second Post')); + $this->Users->notify($user, new AdminAlert('Alert')); $this->assertNotificationSentToTimes($user, PostPublished::class, 2); $this->assertNotificationSentToTimes($user, AdminAlert::class, 1); @@ -214,10 +208,9 @@ public function testAssertNotificationSentToTimes(): void */ public function testAssertNotificationDataContains(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user = $usersTable->get(1); + $user = $this->Users->get(1); - $usersTable->notify($user, new PostPublished(123, 'My Great Post')); + $this->Users->notify($user, new PostPublished(123, 'My Great Post')); $this->assertNotificationDataContains(PostPublished::class, 'post_id', 123); $this->assertNotificationDataContains(PostPublished::class, 'post_title', 'My Great Post'); @@ -230,11 +223,10 @@ public function testAssertNotificationDataContains(): void */ public function testGetNotifications(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user = $usersTable->get(1); + $user = $this->Users->get(1); - $usersTable->notify($user, new PostPublished(1, 'Test Post')); - $usersTable->notify($user, new AdminAlert('Alert')); + $this->Users->notify($user, new PostPublished(1, 'Test Post')); + $this->Users->notify($user, new AdminAlert('Alert')); $notifications = $this->getNotifications(); @@ -250,13 +242,12 @@ public function testGetNotifications(): void */ public function testGetNotificationsByClass(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user1 = $usersTable->get(1); - $user2 = $usersTable->get(2); + $user1 = $this->Users->get(1); + $user2 = $this->Users->get(2); - $usersTable->notify($user1, new PostPublished(1, 'First')); - $usersTable->notify($user2, new PostPublished(2, 'Second')); - $usersTable->notify($user1, new AdminAlert('Alert')); + $this->Users->notify($user1, new PostPublished(1, 'First')); + $this->Users->notify($user2, new PostPublished(2, 'Second')); + $this->Users->notify($user1, new AdminAlert('Alert')); $postNotifications = $this->getNotificationsByClass(PostPublished::class); $alertNotifications = $this->getNotificationsByClass(AdminAlert::class); @@ -272,13 +263,12 @@ public function testGetNotificationsByClass(): void */ public function testGetNotificationsFor(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user1 = $usersTable->get(1); - $user2 = $usersTable->get(2); + $user1 = $this->Users->get(1); + $user2 = $this->Users->get(2); - $usersTable->notify($user1, new PostPublished(1, 'First')); - $usersTable->notify($user1, new PostPublished(2, 'Second')); - $usersTable->notify($user2, new PostPublished(3, 'Third')); + $this->Users->notify($user1, new PostPublished(1, 'First')); + $this->Users->notify($user1, new PostPublished(2, 'Second')); + $this->Users->notify($user2, new PostPublished(3, 'Third')); $user1Notifications = $this->getNotificationsFor($user1, PostPublished::class); $user2Notifications = $this->getNotificationsFor($user2, PostPublished::class); @@ -294,11 +284,10 @@ public function testGetNotificationsFor(): void */ public function testGetNotificationsByChannel(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user = $usersTable->get(1); + $user = $this->Users->get(1); - $usersTable->notify($user, new PostPublished(1, 'Post')); - $usersTable->notify($user, new AdminAlert('Alert')); + $this->Users->notify($user, new PostPublished(1, 'Post')); + $this->Users->notify($user, new AdminAlert('Alert')); $databaseNotifications = $this->getNotificationsByChannel('database'); $mailNotifications = $this->getNotificationsByChannel('mail'); @@ -314,11 +303,10 @@ public function testGetNotificationsByChannel(): void */ public function testGetOnDemandNotifications(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user = $usersTable->get(1); + $user = $this->Users->get(1); $anonymous = NotificationManager::route('mail', 'admin@example.com'); - $usersTable->notify($user, new PostPublished(1, 'Post')); + $this->Users->notify($user, new PostPublished(1, 'Post')); $anonymous->notify(new AdminAlert('Server Down', 'critical')); $onDemandNotifications = $this->getOnDemandNotifications(); @@ -335,12 +323,11 @@ public function testGetOnDemandNotifications(): void */ public function testMultipleNotificationsToSameUser(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user = $usersTable->get(1); + $user = $this->Users->get(1); - $usersTable->notify($user, new PostPublished(1, 'First Post')); - $usersTable->notify($user, new PostPublished(2, 'Second Post')); - $usersTable->notify($user, new PostPublished(3, 'Third Post')); + $this->Users->notify($user, new PostPublished(1, 'First Post')); + $this->Users->notify($user, new PostPublished(2, 'Second Post')); + $this->Users->notify($user, new PostPublished(3, 'Third Post')); $this->assertNotificationSentToTimes($user, PostPublished::class, 3); $this->assertNotificationCount(3); @@ -353,14 +340,13 @@ public function testMultipleNotificationsToSameUser(): void */ public function testNotificationsToMultipleUsers(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user1 = $usersTable->get(1); - $user2 = $usersTable->get(2); - $user3 = $usersTable->get(3); + $user1 = $this->Users->get(1); + $user2 = $this->Users->get(2); + $user3 = $this->Users->get(3); - $usersTable->notify($user1, new PostPublished(1, 'Post')); - $usersTable->notify($user2, new PostPublished(2, 'Post')); - $usersTable->notify($user3, new PostPublished(3, 'Post')); + $this->Users->notify($user1, new PostPublished(1, 'Post')); + $this->Users->notify($user2, new PostPublished(2, 'Post')); + $this->Users->notify($user3, new PostPublished(3, 'Post')); $this->assertNotificationSentTo($user1, PostPublished::class); $this->assertNotificationSentTo($user2, PostPublished::class); @@ -375,8 +361,7 @@ public function testNotificationsToMultipleUsers(): void */ public function testNotificationWithSendNow(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user = $usersTable->get(1); + $user = $this->Users->get(1); NotificationManager::sendNow($user, new PostPublished(1, 'Test Post')); @@ -391,8 +376,7 @@ public function testNotificationWithSendNow(): void */ public function testNotificationWithSpecificChannels(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user = $usersTable->get(1); + $user = $this->Users->get(1); NotificationManager::sendNow($user, new PostPublished(1, 'Test Post'), ['database']); @@ -408,10 +392,9 @@ public function testNotificationWithSpecificChannels(): void */ public function testNotificationMetadata(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user = $usersTable->get(1); + $user = $this->Users->get(1); - $usersTable->notify($user, new PostPublished(1, 'Test Post')); + $this->Users->notify($user, new PostPublished(1, 'Test Post')); $notifications = $this->getNotifications(); $notification = $notifications[0]; diff --git a/tests/TestCase/TestSuite/TestNotificationSenderTest.php b/tests/TestCase/TestSuite/TestNotificationSenderTest.php index d3216fa..0a3f90d 100644 --- a/tests/TestCase/TestSuite/TestNotificationSenderTest.php +++ b/tests/TestCase/TestSuite/TestNotificationSenderTest.php @@ -6,6 +6,7 @@ use Cake\TestSuite\TestCase; use Crustum\Notification\NotificationManager; use Crustum\Notification\TestSuite\TestNotificationSender; +use TestApp\Model\Table\UsersTable; use TestApp\Notification\PostPublished; /** @@ -28,6 +29,8 @@ class TestNotificationSenderTest extends TestCase 'plugin.Crustum/Notification.Notifications', ]; + protected UsersTable $Users; + /** * Test setup * @@ -38,6 +41,9 @@ public function setUp(): void parent::setUp(); TestNotificationSender::replaceAllSenders(); TestNotificationSender::clearNotifications(); + /** @var \TestApp\Model\Table\UsersTable $usersTable */ + $usersTable = $this->getTableLocator()->get('Users'); + $this->Users = $usersTable; } /** @@ -72,10 +78,9 @@ public function testReplaceAllSenders(): void */ public function testSendCapturesNotification(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user = $usersTable->get(1); + $user = $this->Users->get(1); - $usersTable->notify($user, new PostPublished(1, 'Test')); + $this->Users->notify($user, new PostPublished(1, 'Test')); $notifications = TestNotificationSender::getNotifications(); @@ -90,8 +95,7 @@ public function testSendCapturesNotification(): void */ public function testSendNowCapturesNotification(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user = $usersTable->get(1); + $user = $this->Users->get(1); NotificationManager::sendNow($user, new PostPublished(1, 'Test')); @@ -108,10 +112,9 @@ public function testSendNowCapturesNotification(): void */ public function testClearNotifications(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user = $usersTable->get(1); + $user = $this->Users->get(1); - $usersTable->notify($user, new PostPublished(1, 'Test')); + $this->Users->notify($user, new PostPublished(1, 'Test')); $this->assertCount(1, TestNotificationSender::getNotifications()); @@ -127,12 +130,11 @@ public function testClearNotifications(): void */ public function testGetNotificationsFor(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user1 = $usersTable->get(1); - $user2 = $usersTable->get(2); + $user1 = $this->Users->get(1); + $user2 = $this->Users->get(2); - $usersTable->notify($user1, new PostPublished(1, 'Test')); - $usersTable->notify($user2, new PostPublished(2, 'Test')); + $this->Users->notify($user1, new PostPublished(1, 'Test')); + $this->Users->notify($user2, new PostPublished(2, 'Test')); $user1Notifications = TestNotificationSender::getNotificationsFor($user1, PostPublished::class); $user2Notifications = TestNotificationSender::getNotificationsFor($user2, PostPublished::class); @@ -148,10 +150,9 @@ public function testGetNotificationsFor(): void */ public function testGetNotificationsByChannel(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user = $usersTable->get(1); + $user = $this->Users->get(1); - $usersTable->notify($user, new PostPublished(1, 'Test')); + $this->Users->notify($user, new PostPublished(1, 'Test')); $databaseNotifications = TestNotificationSender::getNotificationsByChannel('database'); $mailNotifications = TestNotificationSender::getNotificationsByChannel('mail'); @@ -167,11 +168,10 @@ public function testGetNotificationsByChannel(): void */ public function testGetNotificationsByClass(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user = $usersTable->get(1); + $user = $this->Users->get(1); - $usersTable->notify($user, new PostPublished(1, 'Test')); - $usersTable->notify($user, new PostPublished(2, 'Test')); + $this->Users->notify($user, new PostPublished(1, 'Test')); + $this->Users->notify($user, new PostPublished(2, 'Test')); $notifications = TestNotificationSender::getNotificationsByClass(PostPublished::class); @@ -185,11 +185,10 @@ public function testGetNotificationsByClass(): void */ public function testGetOnDemandNotifications(): void { - $usersTable = $this->getTableLocator()->get('TestApp.Users'); - $user = $usersTable->get(1); + $user = $this->Users->get(1); $anonymous = NotificationManager::route('mail', 'admin@example.com'); - $usersTable->notify($user, new PostPublished(1, 'Test')); + $this->Users->notify($user, new PostPublished(1, 'Test')); $anonymous->notify(new PostPublished(2, 'Admin Post')); $onDemandNotifications = TestNotificationSender::getOnDemandNotifications(); diff --git a/tests/TestCase/Trait/SerializesNotificationTest.php b/tests/TestCase/Trait/SerializesNotificationTest.php index fc8e825..9973c4b 100644 --- a/tests/TestCase/Trait/SerializesNotificationTest.php +++ b/tests/TestCase/Trait/SerializesNotificationTest.php @@ -31,7 +31,6 @@ public function testSerializesScalarProperties(): void $data = $notification->__serialize(); - $this->assertIsArray($data); $this->assertArrayHasKey('__class__', $data); $this->assertStringContainsString('TestNotificationWithScalars', $data['__class__']); } @@ -64,7 +63,6 @@ public function testSerializesArrayProperties(): void $data = $notification->__serialize(); - $this->assertIsArray($data); $this->assertArrayHasKey('__class__', $data); } @@ -80,7 +78,6 @@ public function testSerializesDateTimeObjects(): void $data = $notification->__serialize(); - $this->assertIsArray($data); $this->assertArrayHasKey('__class__', $data); } @@ -98,7 +95,6 @@ public function testSerializesEntityObjects(): void $data = $notification->__serialize(); - $this->assertIsArray($data); $this->assertArrayHasKey('__class__', $data); } @@ -134,7 +130,6 @@ public function testSerializesNullProperties(): void $data = $notification->__serialize(); - $this->assertIsArray($data); $this->assertArrayHasKey('__class__', $data); }