Skip to content

Commit 0c11259

Browse files
authored
Merge pull request #50 from samsonasik/support-recursive-collection-upload
support recursive collection upload included into mail attachment
2 parents dc21bf5 + 18801c6 commit 0c11259

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

spec/Handler/Writer/MailSpec.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,47 @@
7474

7575
expect($this->transport)->toReceive('send');
7676

77+
});
78+
79+
it('bring deeper multiple collection upload, then transport->send()', function () {
80+
81+
$writer = new Mail(
82+
$this->mailMessage,
83+
$this->transport,
84+
[
85+
'request_method' => 'POST',
86+
'query_data' => [],
87+
'body_data' => ['text' => 'test'],
88+
'raw_data' => [],
89+
'files_data' => [
90+
"file-collection" => [
91+
'file-collection-deeper' => [
92+
'file-collection-deeper-deep' => [
93+
[
94+
'name' => 'foo.html',
95+
'tmp_name' => __DIR__ . '/../../Fixture/data/foo.html',
96+
'error' => 0,
97+
'size' => 1,
98+
'type' => 'text/html'
99+
],
100+
],
101+
],
102+
],
103+
],
104+
'cookie_data' => [],
105+
]
106+
);
107+
108+
$r = new ReflectionProperty($this->writer, 'eventsToMail');
109+
$r->setAccessible(true);
110+
$r->setValue($writer, ["timestamp" => "2017-02-25T02:08:46+07:00"]);
111+
112+
allow($this->transport)->toReceive('send');
113+
114+
$writer->shutdown();
115+
116+
expect($this->transport)->toReceive('send');
117+
77118
});
78119

79120
it('transport->send() trigger error', function () {

src/Handler/Writer/Mail.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,12 @@ public function shutdown() : void
5757
foreach ($this->requestData['files_data'] as $key => $row) {
5858
if (\key($row) === 'name') {
5959
// single upload
60-
$body = $this->bodyAddPart($body, $row);
60+
$body = $this->singleBodyAddPart($body, $row);
6161
continue;
6262
}
6363

6464
// collection upload
65-
foreach ($row as $multiple => $upload) {
66-
$body = $this->bodyAddPart($body, $upload);
67-
}
65+
$body = $this->collectionBodyAddPart($body, $row);
6866
}
6967

7068
$this->mail->setBody($body);
@@ -91,7 +89,7 @@ public function shutdown() : void
9189
}
9290
}
9391

94-
private function bodyAddPart(MimeMessage $body, array $data) : MimeMessage
92+
private function singleBodyAddPart(MimeMessage $body, array $data) : MimeMessage
9593
{
9694
$mimePart = new MimePart(\fopen($data['tmp_name'], 'r'));
9795
$mimePart->type = $data['type'];
@@ -101,4 +99,18 @@ private function bodyAddPart(MimeMessage $body, array $data) : MimeMessage
10199

102100
return $body->addPart($mimePart);
103101
}
102+
103+
private function collectionBodyAddPart(MimeMessage $body, array $data) : MimeMessage
104+
{
105+
foreach ($data as $multiple => $upload) {
106+
if (\key($upload) === 'name') {
107+
$body = $this->singleBodyAddPart($body, $upload);
108+
continue;
109+
}
110+
111+
$body = $this->collectionBodyAddPart($body, $upload);
112+
}
113+
114+
return $body;
115+
}
104116
}

0 commit comments

Comments
 (0)