Skip to content
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

Multiple matches with different method arguments broken #1171

Open
emulgeator opened this issue Mar 22, 2022 · 0 comments
Open

Multiple matches with different method arguments broken #1171

emulgeator opened this issue Mar 22, 2022 · 0 comments

Comments

@emulgeator
Copy link

The following simplified test is unable to match properly the expectations. Though the code is fine the test is still failing:

class Command
{
    public function run(): void
    {
        foreach ($this->getAvailableEntityTypes() as $entityType) {
            $requestId = $this->uuidFactory->uuid4();
            codecept_debug('----- ACTUAL -----' . $entityType . ' ' . $requestId);
            $this->queueService->add($entityType, $requestId);
        }
    }

    /**
     * @return EntityType[]
     */
    private function getAvailableEntityTypes(): array
    {
        return [
            EntityType::user(),
            EntityType::admin(),
            EntityType::guest(),
        ];
    }
}
class CommandTest extends TestCaseAbstract
{
    private QueueService         $queueService;
    private UuidFactoryInterface $uuidFactory;

    public function setUp(): void
    {
        parent::setUp();

        $this->queueService = Mockery::mock(QueueService::class);
        $this->uuidFactory  = Mockery::mock(UuidFactoryInterface::class);
    }

    public function testRun_shouldScheduleTasksForAllEntities()
    {
        $sut = new ScheduleDocumentationGatherCommand($this->queueService, $this->uuidFactory);

        $userRequestId    = Uuid::uuid4();
        $adminRequestId = Uuid::uuid4();
        $guestRequestId = Uuid::uuid4();

        $this->expectRequestIdGenerated($userRequestId, $adminRequestId, $userRequestId);
        $this->expectTaskQueued(EntityType::user(), $userRequestId);
        $this->expectTaskQueued(EntityType::admin(), $adminRequestId);
        $this->expectTaskQueued(EntityType::guest(), $guestRequestId);

        $sut->run();
    }

    private function expectRequestIdGenerated(UuidInterface ...$requestIds): void
    {
        $this->uuidFactory
            ->shouldReceive('uuid4')
            ->andReturn(...$requestIds);
    }

    private function expectTaskQueued(EntityType $expectedEntityType, UuidInterface $expectedRequestId): void
    {
        $this->queueService
            ->shouldReceive('addDocumentationGatherTasksToQueue')
            ->withArgs(
                function (EntityType $entityType, UuidInterface $requestId) use ($expectedEntityType, $expectedRequestId) {
                    codecept_debug([
                                       (string)$entityType,
                                       (string)$expectedEntityType,
                                       (string)$requestId,
                                       (string)$expectedRequestId,
                                   ]);
                    return (string)$entityType === (string)$expectedEntityType
                        && $requestId->toString() === $expectedRequestId->toString();
                }
            );
    }
}

I added some debugging as well to demonstrate what's happening exactly:

  ----- ACTUAL -----user 97fa864f-66dc-4777-94ef-cad5fb73c54a
  Array
  (
      [receivedEntity] => user
      [expectedEntity] => user
      [receivedRequest] => 97fa864f-66dc-4777-94ef-cad5fb73c54a
      [expectedRequest] => 97fa864f-66dc-4777-94ef-cad5fb73c54a
  )

  ----- ACTUAL -----admin e6c0bea7-b6f7-41b7-b69a-75db9668e9a3
  Array
  (
      [receivedEntity] => admin
      [expectedEntity] => user
      [receivedRequest] => e6c0bea7-b6f7-41b7-b69a-75db9668e9a3
      [expectedRequest] => 97fa864f-66dc-4777-94ef-cad5fb73c54a
  )

  Array
  (
      [receivedEntity] => admin
      [expectedEntity] => admin
      [receivedRequest] => e6c0bea7-b6f7-41b7-b69a-75db9668e9a3
      [expectedRequest] => e6c0bea7-b6f7-41b7-b69a-75db9668e9a3
  )

  ----- ACTUAL -----guest 97fa864f-66dc-4777-94ef-cad5fb73c54a
  Array
  (
      [receivedEntity] => guest
      [expectedEntity] => user
      [receivedRequest] => 97fa864f-66dc-4777-94ef-cad5fb73c54a
      [expectedRequest] => 97fa864f-66dc-4777-94ef-cad5fb73c54a
  )

  Array
  (
      [receivedEntity] => guest
      [expectedEntity] => admin
      [receivedRequest] => 97fa864f-66dc-4777-94ef-cad5fb73c54a
      [expectedRequest] => e6c0bea7-b6f7-41b7-b69a-75db9668e9a3
  )

  Array
  (
      [receivedEntity] => guest
      [expectedEntity] => guest
      [receivedRequest] => 97fa864f-66dc-4777-94ef-cad5fb73c54a
      [expectedRequest] => 8505e13f-5a3e-4c1f-a2cf-94823d418409
  )

  Array
  (
      [receivedEntity] => guest
      [expectedEntity] => user
      [receivedRequest] => 97fa864f-66dc-4777-94ef-cad5fb73c54a
      [expectedRequest] => 97fa864f-66dc-4777-94ef-cad5fb73c54a
  )

  Array
  (
      [receivedEntity] => guest
      [expectedEntity] => admin
      [receivedRequest] => 97fa864f-66dc-4777-94ef-cad5fb73c54a
      [expectedRequest] => e6c0bea7-b6f7-41b7-b69a-75db9668e9a3
  )

  Array
  (
      [receivedEntity] => guest
      [expectedEntity] => guest
      [receivedRequest] => 97fa864f-66dc-4777-94ef-cad5fb73c54a
      [expectedRequest] => 8505e13f-5a3e-4c1f-a2cf-94823d418409
  )

As can be seen here the first two iteration is matched, but the third one fails.
At the third call, first it tries to match with the wrong entityType, but the right requestId. And when it gets to the right entityType it wont try to match with the right requestId any more

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

No branches or pull requests

1 participant