microsoftgraph / microsoftgraph/msgraph-sdk-php

Need Advice on mocking the sdk for unit and integration tests

Open
#1,476 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
669
Forks
150
Avg merge
15h 21m
Merged PRs (30d)
3

Description

For unit testing a class in out application that uses the sdk there isn't an easy way to mock the sdk parts see code block below.
I had a dig aorund in the source and couldnt find anything helpful in regards to this. Though I did stumble onto somthing indicating it uses the guzzle client so I was wondering if there was a way to do this:
https://docs.guzzlephp.org/en/stable/testing.html#mock-handler
with the sdk?
Or if there was any other recomended approach?

I will also need to write integration tests and dont want to be letting it call out the the MS servers so the guzzle mock hadnler or something similar would be very useful.

<?php

use Http\Promise\Promise;
use Microsoft\Graph\Generated\Chats\Item\Messages\MessagesRequestBuilder;
use Microsoft\Graph\Generated\Users\Item\Messages\Item\MessageItemRequestBuilder;
use Microsoft\Graph\Generated\Users\Item\Messages\Item\Value\ContentRequestBuilder;
use Microsoft\Graph\Generated\Users\Item\UserItemRequestBuilder;
use Microsoft\Graph\Generated\Users\UsersRequestBuilder;
use Microsoft\Graph\GraphServiceClient;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\StreamInterface;

class SomeService {
    public function __construct(private GraphServiceClient $client)
    {
    }

    public function run(): void {
        $mimeEmail = $this->client->users()
            ->byUserId('testuser@testing.com')
            ->messages()
            ->byMessageId('SomeMessageId')
            ->content()
            ->get()
            ->wait();

        //do something with $mimeEmail;
    }
}

class SomeServiceTest extends TestCase
{

    public function testComponent(): void
    {
        $graphServiceClient = $this->createMock(GraphServiceClient::class);
        $mockUsersRequestBuilder = $this->createMock(UsersRequestBuilder::class);

        $graphServiceClient->expects(self::once())
            ->method('users')
            ->willReturn($mockUsersRequestBuilder)
        ;

        $userItemRequestBuilder = $this->createMock(UserItemRequestBuilder::class);
        $mockUsersRequestBuilder->expects(self::once())
            ->method('byUserId')
            ->with($this->email)
            ->willReturn($userItemRequestBuilder)
        ;

        $messagesRequestBuilder = $this->createMock(MessagesRequestBuilder::class);

        $userItemRequestBuilder->expects(self::once())
            ->method('messages')
            ->willReturn($messagesRequestBuilder);

        $messageItemRequestBuilder = $this->createMock(MessageItemRequestBuilder::class);

        $messagesRequestBuilder->expects(self::once())
            ->method('byMessageId')
            ->with('SomeMessageId')
            ->willReturn($messageItemRequestBuilder)
        ;

        $contentRequestBuilder = $this->createMock(ContentRequestBuilder::class);

        $messageItemRequestBuilder->expects(self::once())
            ->method('content')
            ->willReturn($contentRequestBuilder)
        ;

        $mimeMessagePromise = $this->createMock(Promise::class);

        $contentRequestBuilder->expects(self::once())
            ->method('get')
            ->willReturn($mimeMessagePromise)
        ;


        $stream = $this->createMock(StreamInterface::class);
        $mimeMessagePromise->expects(self::once())
            ->method('wait')
            ->willReturn($stream)
        ;

        $stream->expects(self::once())
            ->method('getContents')
            ->willReturn('Mime Contents')
        ;

        $service = new SomeService($graphServiceClient);

        $service->run();

        //Do assertions
    }

}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names GraphServiceClient, generated request builders, PHPUnit mocks, and Guzzle's MockHandler, but no repository files or test entry points. Start by tracing GraphServiceClient's request construction and comparing it with the linked Guzzle testing guidance; done would require a documented, supported mocking approach for both unit and integration tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
developer-experience, testing-qa
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.