-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathGenerateTest.php
More file actions
151 lines (136 loc) Β· 4.62 KB
/
Copy pathGenerateTest.php
File metadata and controls
151 lines (136 loc) Β· 4.62 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Notifications\Tests\Unit\Command;
use OCA\Notifications\App;
use OCA\Notifications\Command\Generate;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Notification\IManager;
use OCP\Notification\INotification;
use OCP\RichObjectStrings\IValidator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Test\TestCase;
#[Group(name: 'DB')]
class GenerateTest extends TestCase {
protected ITimeFactory&MockObject $timeFactory;
protected IUserManager&MockObject $userManager;
protected IManager&MockObject $notificationManager;
protected IValidator&MockObject $richValidator;
protected App&MockObject $notificationApp;
protected Generate $command;
protected function setUp(): void {
parent::setUp();
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->notificationManager = $this->createMock(IManager::class);
$this->richValidator = $this->createMock(IValidator::class);
$this->notificationApp = $this->createMock(App::class);
$this->command = new Generate(
$this->timeFactory,
$this->userManager,
$this->notificationManager,
$this->richValidator,
$this->notificationApp,
);
}
public static function dataExecute(): array {
return [
['user', '', '', false, null, false, null, false, 1],
['user', '', '', false, null, false, 'user', false, 1],
['user', str_repeat('a', 256), '', false, null, false, 'user', false, 1],
['user', 'short', '', true, false, false, 'user', true, 0],
['user', 'short', str_repeat('a', 4001), false, null, false, 'user', false, 1],
['user', 'short', str_repeat('a', 4000), true, false, true, 'user', true, 0],
['user', 'short', 'long', true, true, true, 'user', true, 1],
];
}
#[DataProvider(methodName: 'dataExecute')]
public function testExecute(string $userId, string $short, string $long, bool $createNotification, ?bool $notifyThrows, bool $validLong, ?string $user, bool $isCreated, int $exitCode): void {
if ($user !== null) {
$u = $this->createMock(IUser::class);
$u->expects($createNotification ? $this->once() : $this->never())
->method('getUID')
->willReturn($user);
} else {
$u = null;
}
$this->userManager->expects($this->any())
->method('get')
->with($userId)
->willReturn($u);
$dateTime = new \DateTime();
$this->timeFactory->expects(!$isCreated ? $this->never() : $this->once())
->method('getDateTime')
->willReturn($dateTime);
if ($createNotification) {
$n = $this->createMock(INotification::class);
$n->expects($this->once())
->method('setApp')
->with('admin_notifications')
->willReturnSelf();
$n->expects($this->once())
->method('setUser')
->with($user)
->willReturnSelf();
$n->expects($this->once())
->method('setDateTime')
->with($dateTime)
->willReturnSelf();
$n->expects($this->once())
->method('setObject')
->with('admin_notifications', dechex($dateTime->getTimestamp()))
->willReturnSelf();
$n->expects($this->once())
->method('setSubject')
->with('cli', [$short])
->willReturnSelf();
if ($validLong) {
$n->expects($this->once())
->method('setMessage')
->with('cli', [$long])
->willReturnSelf();
} else {
$n->expects($this->never())
->method('setMessage');
}
$this->notificationManager->expects($this->once())
->method('createNotification')
->willReturn($n);
if ($notifyThrows === null) {
$this->notificationManager->expects($this->never())
->method('notify');
} elseif ($notifyThrows === false) {
$this->notificationManager->expects($this->once())
->method('notify')
->with($n);
} elseif ($notifyThrows === true) {
$this->notificationManager->expects($this->once())
->method('notify')
->willThrowException(new \InvalidArgumentException());
}
}
$input = $this->createMock(InputInterface::class);
$input->method('getArgument')
->willReturnMap([
['user-id', $userId],
['short-message', $short],
]);
$input->method('getOption')
->willReturnMap([
['long-message', $long],
['dummy', false],
]);
$output = $this->createMock(OutputInterface::class);
$return = self::invokePrivate($this->command, 'execute', [$input, $output]);
$this->assertSame($exitCode, $return);
}
}