Need Advice on mocking the sdk for unit and integration tests
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
- Lĩnh vực
- developer-experience, testing-qa
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
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của microsoftgraph/msgraph-sdk-php
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
microsoftgraph/msgraph-sdk-php#1794 · 2 bình luận ·
-
bug: metadata type:bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
microsoftgraph/msgraph-sdk-php#1739 · 6 bình luận ·
-
status:waiting-for-triage type:bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 68/100
microsoftgraph/msgraph-sdk-php#1643 · 8 bình luận · 4 reaction ·
-
status:waiting-for-triage
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
microsoftgraph/msgraph-sdk-php#1800 · 1 reaction ·
-
Now Đang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 1/100
microsoftgraph/msgraph-sdk-php#1799 ·
Tất cả issue của microsoftgraph/msgraph-sdk-php
Issue tương tự
-
priority: p3
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
googleapis/librarian#7636 ·
-
0. Needs triage bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
nextcloud/fulltextsearch#1011 ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
coollabsio/coolify#11927 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100