实体事件

Testing Is Documentation

tests/Database/Ddd/EntityEventTest.php实体事件 - 图1

实体在新增和更新时,预植了事件监听器,可以定义一些事件。

Uses

  1. <?php
  2. use Leevel\Database\Ddd\Entity;
  3. use Leevel\Di\Container;
  4. use Leevel\Event\Dispatch;
  5. use Tests\Database\DatabaseTestCase as TestCase;
  6. use Tests\Database\Ddd\Entity\DemoEventEntity;

事件基本使用方法

  1. public function testBaseUse(): void
  2. {
  3. $dispatch = new Dispatch(new Container());
  4. $this->assertNull(Entity::eventDispatch());
  5. Entity::withEventDispatch($dispatch);
  6. $this->assertInstanceof(Dispatch::class, Entity::eventDispatch());
  7. $test = new DemoEventEntity(['name' => 'foo']);
  8. DemoEventEntity::event(Entity::BEFORE_CREATE_EVENT, function () {
  9. $_SERVER['ENTITY.BEFORE_CREATE_EVENT'] = 'BEFORE_CREATE_EVENT';
  10. });
  11. DemoEventEntity::event(Entity::AFTER_CREATE_EVENT, function () {
  12. $_SERVER['ENTITY.AFTER_CREATE_EVENT'] = 'AFTER_CREATE_EVENT';
  13. });
  14. $this->assertFalse(isset($_SERVER['ENTITY.BEFORE_CREATE_EVENT']));
  15. $this->assertFalse(isset($_SERVER['ENTITY.AFTER_CREATE_EVENT']));
  16. $test->create()->flush();
  17. $this->assertTrue(isset($_SERVER['ENTITY.BEFORE_CREATE_EVENT']));
  18. $this->assertTrue(isset($_SERVER['ENTITY.AFTER_CREATE_EVENT']));
  19. $this->assertSame('BEFORE_CREATE_EVENT', $_SERVER['ENTITY.BEFORE_CREATE_EVENT']);
  20. $this->assertSame('AFTER_CREATE_EVENT', $_SERVER['ENTITY.AFTER_CREATE_EVENT']);
  21. unset($_SERVER['ENTITY.BEFORE_CREATE_EVENT'], $_SERVER['ENTITY.AFTER_CREATE_EVENT']);
  22. }

实体支持的事件

  1. # Tests\Database\Ddd\EntityEventTest::getSupportedEvent
  2. public function getSupportedEvent()
  3. {
  4. return [
  5. [Entity::BEFORE_SAVE_EVENT],
  6. [Entity::AFTER_SAVE_EVENT],
  7. [Entity::BEFORE_CREATE_EVENT],
  8. [Entity::AFTER_CREATE_EVENT],
  9. [Entity::BEFORE_UPDATE_EVENT],
  10. [Entity::AFTER_UPDATE_EVENT],
  11. [Entity::BEFORE_DELETE_EVENT],
  12. [Entity::AFTER_DELETE_EVENT],
  13. [Entity::BEFORE_SOFT_DELETE_EVENT],
  14. [Entity::AFTER_SOFT_DELETE_EVENT],
  15. [Entity::BEFORE_SOFT_RESTORE_EVENT],
  16. [Entity::AFTER_SOFT_RESTORE_EVENT],
  17. ];
  18. }
  1. public function testSupportEvent($event): void
  2. {
  3. $dispatch = new Dispatch(new Container());
  4. $this->assertNull(Entity::eventDispatch());
  5. Entity::withEventDispatch($dispatch);
  6. $this->assertInstanceof(Dispatch::class, Entity::eventDispatch());
  7. $supportEvent = DemoEventEntity::supportEvent();
  8. $this->assertTrue(in_array($event, $supportEvent, true));
  9. }

不受支持的事件

  1. public function testEventWasNotSupport(): void
  2. {
  3. $this->expectException(\InvalidArgumentException::class);
  4. $this->expectExceptionMessage('Event `not_support` do not support.');
  5. $dispatch = new Dispatch(new Container());
  6. $this->assertNull(Entity::eventDispatch());
  7. Entity::withEventDispatch($dispatch);
  8. $this->assertInstanceof(Dispatch::class, Entity::eventDispatch());
  9. DemoEventEntity::event('not_support', function () {
  10. });
  11. }