Skip to content

[Bug]: #On(Phase::handle) turns Event::commit return value into collection #213

@tsterker

Description

@tsterker

What happened?

When registering an external event listener with #On(Phase::Handle) the respective Event's commit calls now return the original return value wrapped in a Collection.

How to reproduce the bug

See the example test below, which showcases how we don't get the expected Event::commit return value once we registered our listener. Also note the stray null element, which might somehow be the root cause of implicit wrapping into a collection?

Image

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
use Thunk\Verbs\Attributes\Hooks\On;
use Thunk\Verbs\Event;
use Thunk\Verbs\Facades\Verbs;
use Thunk\Verbs\Lifecycle\Phase;

class ExampleVerbsListenerTest extends TestCase
{
    use RefreshDatabase;

    #[Test]
    public function handle_hook_turns_commit_response_into_collection(): void
    {
        // Get the commit return value before the event listener was registered
        $result = MyEvent::commit();
        $this->assertSame(MyEvent::COMMIT_RETURN_VALUE, $result);

        // Register the event listener
        Verbs::listen(ExampleVerbsListener::class);

        // 💥 See our assetion fail
        $result = MyEvent::commit();
        $this->assertSame(MyEvent::COMMIT_RETURN_VALUE, $result);
    }
}

class MyEvent extends Event
{
    public const COMMIT_RETURN_VALUE = 'Hello!';

    public function handle(): string
    {
        return self::COMMIT_RETURN_VALUE;
    }
}

class ExampleVerbsListener
{
    #[On(Phase::Handle)]
    public function handle(Event $event): void {}
}

Package Version

0.7.0

PHP Version

8.2.25

Laravel Version

10.48.29

Which operating systems does with happen with?

macOS

Notes

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions