Skip to content

Commit 1b79c41

Browse files
authored
Merge pull request #303 from dacastro4/xfiov7-codex/add-tests-for-token-generation-and-api-handling
Add Gmail service tests for tokens, messages, and attachments
2 parents 8dd1b75 + 32880c0 commit 1b79c41

File tree

2 files changed

+181
-1
lines changed

2 files changed

+181
-1
lines changed

tests/GmailServiceTest.php

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?php
2+
3+
use Dacastro4\LaravelGmail\Exceptions\AuthException;
4+
use Dacastro4\LaravelGmail\GmailConnection;
5+
use Dacastro4\LaravelGmail\LaravelGmailClass;
6+
use Dacastro4\LaravelGmail\Services\Message;
7+
use Dacastro4\LaravelGmail\Services\Message\Attachment;
8+
use Illuminate\Support\Facades\Storage;
9+
use Tests\TestCase;
10+
11+
class GmailServiceTest extends TestCase
12+
{
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
17+
config([
18+
'gmail.client_secret' => 'secret',
19+
'gmail.client_id' => 'client',
20+
'gmail.redirect_url' => '/',
21+
'gmail.credentials_file_name' => 'test-token',
22+
'gmail.allow_json_encrypt' => false,
23+
'gmail.allow_multiple_credentials' => false,
24+
]);
25+
}
26+
27+
/** @test */
28+
public function test_token_generation_saves_token_file()
29+
{
30+
Storage::fake('local');
31+
32+
$connection = new GmailConnection($this->app['config']);
33+
$token = ['access_token' => 'token', 'refresh_token' => 'refresh'];
34+
$connection->saveAccessToken($token);
35+
36+
Storage::disk('local')->assertExists('gmail/tokens/test-token.json');
37+
$this->assertTrue($connection->checkPreviouslyLoggedIn());
38+
}
39+
40+
/** @test */
41+
public function test_check_previously_logged_in_returns_false_when_no_token()
42+
{
43+
Storage::fake('local');
44+
45+
$connection = new GmailConnection($this->app['config']);
46+
47+
$this->assertFalse($connection->checkPreviouslyLoggedIn());
48+
}
49+
50+
/** @test */
51+
public function test_message_retrieval_returns_collection()
52+
{
53+
Storage::fake('local');
54+
$client = new LaravelGmailClass($this->app['config']);
55+
$service = new \stdClass();
56+
57+
$googleMessage = new \Google_Service_Gmail_Message();
58+
$googleMessage->setId('msg1');
59+
60+
$response = Mockery::mock();
61+
$response->shouldReceive('getMessages')->andReturn([$googleMessage]);
62+
$response->shouldReceive('getNextPageToken')->andReturn(null);
63+
64+
$messagesResource = Mockery::mock();
65+
$messagesResource->shouldReceive('listUsersMessages')->with('me', [])->andReturn($response);
66+
67+
$service->users_messages = $messagesResource;
68+
69+
$messageService = new Message($client);
70+
$messageService->service = $service;
71+
72+
$result = $messageService->all();
73+
74+
$this->assertCount(1, $result);
75+
}
76+
77+
/** @test */
78+
public function test_message_retrieval_handles_empty_response()
79+
{
80+
Storage::fake('local');
81+
$client = new LaravelGmailClass($this->app['config']);
82+
$service = new \stdClass();
83+
84+
$response = Mockery::mock();
85+
$response->shouldReceive('getMessages')->andReturn([]);
86+
$response->shouldReceive('getNextPageToken')->andReturn(null);
87+
88+
$messagesResource = Mockery::mock();
89+
$messagesResource->shouldReceive('listUsersMessages')->andReturn($response);
90+
91+
$service->users_messages = $messagesResource;
92+
93+
$messageService = new Message($client);
94+
$messageService->service = $service;
95+
96+
$result = $messageService->all();
97+
98+
$this->assertCount(0, $result);
99+
}
100+
101+
/** @test */
102+
public function test_attachment_can_be_saved()
103+
{
104+
Storage::fake('local');
105+
106+
$body = Mockery::mock();
107+
$body->shouldReceive('getAttachmentId')->andReturn('attach');
108+
$body->shouldReceive('getSize')->andReturn(10);
109+
110+
$part = Mockery::mock(\Google_Service_Gmail_MessagePart::class);
111+
$part->shouldReceive('getBody')->andReturn($body);
112+
$part->shouldReceive('getFilename')->andReturn('file.txt');
113+
$part->shouldReceive('getMimeType')->andReturn('text/plain');
114+
$part->shouldReceive('getHeaders')->andReturn([]);
115+
116+
$attachment = new Attachment('msg', $part);
117+
118+
$attachmentsResource = Mockery::mock();
119+
$attachmentData = Mockery::mock();
120+
$attachmentData->shouldReceive('getData')->andReturn(base64_encode('content'));
121+
$attachmentsResource->shouldReceive('get')->with('me', 'msg', 'attach')->andReturn($attachmentData);
122+
123+
$service = new \stdClass();
124+
$service->users_messages_attachments = $attachmentsResource;
125+
$ref = new ReflectionProperty(Attachment::class, 'service');
126+
$ref->setAccessible(true);
127+
$ref->setValue($attachment, $service);
128+
129+
$path = $attachment->saveAttachmentTo('attachments', 'file.txt', 'local');
130+
131+
Storage::disk('local')->assertExists('attachments/file.txt');
132+
$this->assertEquals('attachments/file.txt', $path);
133+
}
134+
135+
/** @test */
136+
public function test_attachment_save_throws_exception_when_no_data()
137+
{
138+
Storage::fake('local');
139+
140+
$body = Mockery::mock();
141+
$body->shouldReceive('getAttachmentId')->andReturn('attach');
142+
$body->shouldReceive('getSize')->andReturn(10);
143+
144+
$part = Mockery::mock(\Google_Service_Gmail_MessagePart::class);
145+
$part->shouldReceive('getBody')->andReturn($body);
146+
$part->shouldReceive('getFilename')->andReturn('file.txt');
147+
$part->shouldReceive('getMimeType')->andReturn('text/plain');
148+
$part->shouldReceive('getHeaders')->andReturn([]);
149+
150+
$attachment = new Attachment('msg', $part);
151+
152+
$attachmentsResource = Mockery::mock();
153+
$attachmentData = Mockery::mock();
154+
$attachmentData->shouldReceive('getData')->andReturn(null);
155+
$attachmentsResource->shouldReceive('get')->andReturn($attachmentData);
156+
157+
$service = new \stdClass();
158+
$service->users_messages_attachments = $attachmentsResource;
159+
$ref = new ReflectionProperty(Attachment::class, 'service');
160+
$ref->setAccessible(true);
161+
$ref->setValue($attachment, $service);
162+
163+
$this->expectException(\Exception::class);
164+
$attachment->saveAttachmentTo('attachments', 'file.txt', 'local');
165+
}
166+
167+
/** @test */
168+
public function test_message_method_throws_exception_without_credentials()
169+
{
170+
Storage::fake('local');
171+
$client = new LaravelGmailClass($this->app['config']);
172+
173+
$this->expectException(AuthException::class);
174+
$client->message();
175+
}
176+
}

tests/TestCase.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace Tests;
44

5+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
56
use Orchestra\Testbench\TestCase as TC;
67

7-
class TestCase extends TC {}
8+
class TestCase extends TC
9+
{
10+
use MockeryPHPUnitIntegration;
11+
}

0 commit comments

Comments
 (0)