Skip to content

Assert Dispatched #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 19, 2023
Merged

Conversation

Naugrimm
Copy link
Contributor

This PR allows your tests to assert that a certain workflow/activity was dispatched.

Motivation behind this PR:

Assume we have the following workflow structure:

  • Workflow: CreateAndLinkEntities
    • Activity: CreateEntity1 returns $entity1Id
    • Activity: CreateEntity2 returns $entity2Id
    • Activity: LinkEntity1WithEntity2 returns void
    • The workflow returns [$entity1Id, $entity2Id]

The latest release allows us to mock all three activities, but we cannot verify that the last activity has been dispatched.

With this PR you can now write:

WorkflowStub::fake();
WorkflowStub::mock(CreateEntity1::class, 1);
WorkflowStub::mock(CreateEntity2::class, 2);
WorkflowStub::mock(LinkEntity1WithEntity2::class);

$workflow = WorkflowStub::make(CreateAndLinkEntities::class);
$workflow->start();

WorkflowStub::assertDispatchedTimes(CreateEntity1::class);
WorkflowStub::assertDispatchedTimes(CreateEntity2::class);
WorkflowStub::assertDispatchedTimes(LinkEntity1WithEntity2::class);

This will also work to assert that a child workflow was dispatch.

It currently fails when we do not mock the child workflow itself, but we want to assert that the activities of the child workflow have been dispatched. We want to achieve some end-to-end-testing here.

It does not work because WithoutOverlappingMiddleware tries to get a lock on the parent workflow when trying to call the handle-Method on the child workflow.

@rmcdaniel So before finalizing this PR: What is your opinion on this issue?

  1. Should it be possible to only mock child-workflows? The child workflow's activities must then be tested separately.
  2. Should it be possible to not mock the child workflow and then to mock its activities?

@rmcdaniel
Copy link
Contributor

A child workflow behaves as just another activity to the parent. It should be mocked and tested separately as its own workflow.

@Naugrimm Naugrimm changed the title [WIP] allow to assert that certain workflows/activities were dispatched allow to assert that certain workflows/activities were dispatched Nov 14, 2023
@Naugrimm Naugrimm marked this pull request as ready for review November 14, 2023 08:00
@rmcdaniel
Copy link
Contributor

You can run composer ecs and composer stan locally and then commit the changes to fix the pipeline errors. I can also do it for you if you like. Let me know either way. Thanks for this!


use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use PHPUnit\Framework\Assert as PHPUnit;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be a problem with this because PHPUnit is usually require-dev not require and neither are actually listed in our composer.json. So we should only expect PHPUnit to be installed when running tests. I will do some more local testing to make sure.

@rmcdaniel rmcdaniel changed the title allow to assert that certain workflows/activities were dispatched Assert Dispatched Nov 19, 2023
@rmcdaniel
Copy link
Contributor

I have made a couple of changes. I have moved everything into a Fakes trait. I added the Macroable trait and now the test methods are only added via macros if you call WorkflowStub::fake(). The goal is to not add a lot of stuff to the production runtime that is only used for testing. There is an added bonus that now anyone can add extra methods to the WorkflowStub class.

@rmcdaniel rmcdaniel merged commit 48654ea into laravel-workflow:master Nov 19, 2023
@Naugrimm Naugrimm deleted the assert-dispatched branch November 19, 2023 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy