Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AttachmentServiceTest.php « Attachment « Service « Unit « tests - github.com/nextcloud/mail.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f2d9483ccf548133d8145e3745c26b1aa103df8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php

/**
 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
 *
 * Mail
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

namespace OCA\Mail\Tests\Unit\Service\Attachment;

use ChristophWurst\Nextcloud\Testing\TestCase;
use OCA\Mail\Db\LocalAttachment;
use OCA\Mail\Db\LocalAttachmentMapper;
use OCA\Mail\Exception\UploadException;
use OCA\Mail\Service\Attachment\AttachmentService;
use OCA\Mail\Service\Attachment\AttachmentStorage;
use OCA\Mail\Service\Attachment\UploadedFile;
use PHPUnit_Framework_MockObject_MockObject;

class AttachmentServiceTest extends TestCase {

	/** @var LocalAttachmentMapper|PHPUnit_Framework_MockObject_MockObject */
	private $mapper;

	/** @var AttachmentStorage|PHPUnit_Framework_MockObject_MockObject */
	private $storage;

	/** @var AttachmentService */
	private $service;

	protected function setUp() {
		parent::setUp();

		$this->mapper = $this->createMock(LocalAttachmentMapper::class);
		$this->storage = $this->createMock(AttachmentStorage::class);

		$this->service = new AttachmentService($this->mapper, $this->storage);
	}

	public function testAddFileWithUploadException() {
		$userId = 'jan';
		$uploadedFile = $this->createMock(UploadedFile::class);
		$uploadedFile->expects($this->once())
			->method('getFileName')
			->willReturn('cat.jpg');
		$attachment = LocalAttachment::fromParams([
				'userId' => $userId,
				'fileName' => 'cat.jpg',
		]);
		$persistedAttachment = LocalAttachment::fromParams([
				'id' => 123,
				'userId' => $userId,
				'fileName' => 'cat.jpg',
		]);
		$this->mapper->expects($this->once())
			->method('insert')
			->with($this->equalTo($attachment))
			->willReturn($persistedAttachment);
		$this->storage->expects($this->once())
			->method('save')
			->with($this->equalTo($userId), $this->equalTo(123), $this->equalTo($uploadedFile))
			->willThrowException(new UploadException());
		$this->mapper->expects($this->once())
			->method('delete')
			->with($this->equalTo($persistedAttachment));
		$this->expectException(UploadException::class);

		$this->service->addFile($userId, $uploadedFile);
	}

	public function testAddFile() {
		$userId = 'jan';
		$uploadedFile = $this->createMock(UploadedFile::class);
		$uploadedFile->expects($this->once())
			->method('getFileName')
			->willReturn('cat.jpg');
		$attachment = LocalAttachment::fromParams([
				'userId' => $userId,
				'fileName' => 'cat.jpg',
		]);
		$persistedAttachment = LocalAttachment::fromParams([
				'id' => 123,
				'userId' => $userId,
				'fileName' => 'cat.jpg',
		]);
		$this->mapper->expects($this->once())
			->method('insert')
			->with($this->equalTo($attachment))
			->willReturn($persistedAttachment);
		$this->storage->expects($this->once())
			->method('save')
			->with($this->equalTo($userId), $this->equalTo(123), $this->equalTo($uploadedFile));

		$this->service->addFile($userId, $uploadedFile);
	}

}