wpSegment = $this->diContainer->get(WP::class); $this->segmentRepository = $this->diContainer->get(SegmentsRepository::class); $this->subscribersRepository = $this->diContainer->get(SubscribersRepository::class); if (!is_numeric($this->userId)) { $userId = wp_insert_user([ 'user_login' => self::USER_NAME, 'user_pass' => 'abc', 'user_email' => self::USER_EMAIL, 'role' => self::USER_ROLE, ]); assert(is_numeric($userId)); $this->userId = $userId; $this->wpSegment->synchronizeUsers(); } } public function testCanHandleRegistration() { $wpMock = $this->createMock(Functions::class); $testee = new UserRegistrationTrigger( $this->segmentRepository, $wpMock ); $subscriber = $this->subscribersRepository->findOneBy(['wpUserId' => $this->userId]); assert($subscriber instanceof SubscriberEntity); $wpMock->expects($this->once())->method( 'doAction' )->willReturnCallback(function($hook, $trigger, array $subjects) use ($testee, $subscriber) { $this->assertSame(Hooks::TRIGGER, $hook); $this->assertSame($trigger, $testee); /** @var Subject[] $subjects */ $this->assertSame(SegmentSubject::KEY, $subjects[0]->getKey()); $this->assertSame(SubscriberSubject::KEY, $subjects[1]->getKey()); $wpUserSegment = $this->segmentRepository->getWPUsersSegment(); assert($wpUserSegment instanceof SegmentEntity); $this->assertSame($wpUserSegment->getId(), $subjects[0]->getArgs()['segment_id']); $this->assertSame($subscriber->getId(), $subjects[1]->getArgs()['subscriber_id']); }); $testee->handleSubscription($subscriber); } /** * @dataProvider dataForTestTriggeredByWorkflowRun */ public function testTriggeredByWorkflowRun(array $roleSetting, bool $expectation) { $testee = $this->diContainer->get(UserRegistrationTrigger::class); $subscriber = $this->subscribersRepository->findOneBy(['email' => self::USER_EMAIL]); $this->assertInstanceOf(SubscriberEntity::class, $subscriber); $segment = $this->segmentRepository->getWPUsersSegment(); $this->assertInstanceOf(SegmentEntity::class, $segment); $stepRunArgs = new StepRunArgs( $this->make(Workflow::class), $this->make(WorkflowRun::class), new Step('test-id', 'trigger', 'test:trigger', ['roles' => $roleSetting], []), [ new SubjectEntry( $this->diContainer->get(SegmentSubject::class), new Subject('mailpoet:segment', ['segment_id' => $segment->getId()]) ), new SubjectEntry( $this->diContainer->get(SubscriberSubject::class), new Subject('mailpoet:subscriber', ['subscriber_id' => $subscriber->getId()]) ), ] ); $this->assertSame($expectation, $testee->isTriggeredBy($stepRunArgs)); } public function dataForTestTriggeredByWorkflowRun() : array { return [ 'any_role' => [ [], // any list true, ], 'list_match' => [ [self::USER_ROLE], true, ], 'list_mismatch' => [ ['editor'], false, ], ]; } public function _after() { if (!$this->userId) { return; } is_multisite() ? wpmu_delete_user($this->userId) : wp_delete_user($this->userId); $this->userId = null; $this->wpSegment->synchronizeUsers(); } }