Need Advice on mocking the sdk for unit and integration tests

Đang mở
#1,476 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Tài liệu
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
php

Hướng nghiên cứu

Issue đề cập đến GraphServiceClient, các request builder được tạo tự động, PHPUnit mock và MockHandler của Guzzle, nhưng không có tệp nào trong repository hoặc điểm vào của test. Hãy bắt đầu bằng cách lần theo quá trình xây dựng request của GraphServiceClient và so sánh với hướng dẫn kiểm thử Guzzle được liên kết; để được xem là hoàn thành, cần có một phương pháp mocking được tài liệu hóa và được hỗ trợ cho cả test đơn vị và test tích hợp.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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
    }

}
Ngôn ngữ chính
PHP
Star
669
Fork
150
Merge trung bình
15 giờ 21 phút
Pull request đã merge (30 ngày)
3

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của microsoftgraph/msgraph-sdk-php

Tất cả issue của microsoftgraph/msgraph-sdk-php

Issue tương tự

Thêm issue về PHP

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.