Skip to content

Commit

Permalink
Merge pull request #20172 from nextcloud/techdebt/phpunit-short-return
Browse files Browse the repository at this point in the history
Use the shorter phpunit syntax for mocked return values
  • Loading branch information
ChristophWurst authored Mar 26, 2020
2 parents b3af054 + 2ee65f1 commit 25ab122
Show file tree
Hide file tree
Showing 230 changed files with 3,356 additions and 3,356 deletions.
36 changes: 18 additions & 18 deletions apps/comments/tests/Unit/Notification/ListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public function testEvaluate($eventType, $notificationMethod) {
$comment = $this->getMockBuilder(IComment::class)->getMock();
$comment->expects($this->any())
->method('getObjectType')
->will($this->returnValue('files'));
->willReturn('files');
$comment->expects($this->any())
->method('getCreationDateTime')
->will($this->returnValue(new \DateTime()));
->willReturn(new \DateTime());
$comment->expects($this->once())
->method('getMentions')
->willReturn([
Expand All @@ -102,22 +102,22 @@ public function testEvaluate($eventType, $notificationMethod) {
->getMock();
$event->expects($this->once())
->method('getComment')
->will($this->returnValue($comment));
->willReturn($comment);
$event->expects(($this->any()))
->method(('getEvent'))
->will($this->returnValue($eventType));
->willReturn($eventType);

/** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
$notification = $this->getMockBuilder(INotification::class)->getMock();
$notification->expects($this->any())
->method($this->anything())
->will($this->returnValue($notification));
->willReturn($notification);
$notification->expects($this->exactly(6))
->method('setUser');

$this->notificationManager->expects($this->once())
->method('createNotification')
->will($this->returnValue($notification));
->willReturn($notification);
$this->notificationManager->expects($this->exactly(6))
->method($notificationMethod)
->with($this->isInstanceOf('\OCP\Notification\INotification'));
Expand All @@ -132,7 +132,7 @@ public function testEvaluate($eventType, $notificationMethod) {
['23452-4333-54353-2342'],
['yolo']
)
->will($this->returnValue(true));
->willReturn(true);

$this->listener->evaluate($event);
}
Expand All @@ -146,10 +146,10 @@ public function testEvaluateNoMentions($eventType) {
$comment = $this->getMockBuilder(IComment::class)->getMock();
$comment->expects($this->any())
->method('getObjectType')
->will($this->returnValue('files'));
->willReturn('files');
$comment->expects($this->any())
->method('getCreationDateTime')
->will($this->returnValue(new \DateTime()));
->willReturn(new \DateTime());
$comment->expects($this->once())
->method('getMentions')
->willReturn([]);
Expand All @@ -160,10 +160,10 @@ public function testEvaluateNoMentions($eventType) {
->getMock();
$event->expects($this->once())
->method('getComment')
->will($this->returnValue($comment));
->willReturn($comment);
$event->expects(($this->any()))
->method(('getEvent'))
->will($this->returnValue($eventType));
->willReturn($eventType);

$this->notificationManager->expects($this->never())
->method('createNotification');
Expand All @@ -183,10 +183,10 @@ public function testEvaluateUserDoesNotExist() {
$comment = $this->getMockBuilder(IComment::class)->getMock();
$comment->expects($this->any())
->method('getObjectType')
->will($this->returnValue('files'));
->willReturn('files');
$comment->expects($this->any())
->method('getCreationDateTime')
->will($this->returnValue(new \DateTime()));
->willReturn(new \DateTime());
$comment->expects($this->once())
->method('getMentions')
->willReturn([[ 'type' => 'user', 'id' => 'foobar']]);
Expand All @@ -200,22 +200,22 @@ public function testEvaluateUserDoesNotExist() {
->getMock();
$event->expects($this->once())
->method('getComment')
->will($this->returnValue($comment));
->willReturn($comment);
$event->expects(($this->any()))
->method(('getEvent'))
->will($this->returnValue(CommentsEvent::EVENT_ADD));
->willReturn(CommentsEvent::EVENT_ADD);

/** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
$notification = $this->getMockBuilder(INotification::class)->getMock();
$notification->expects($this->any())
->method($this->anything())
->will($this->returnValue($notification));
->willReturn($notification);
$notification->expects($this->never())
->method('setUser');

$this->notificationManager->expects($this->once())
->method('createNotification')
->will($this->returnValue($notification));
->willReturn($notification);
$this->notificationManager->expects($this->never())
->method('notify');

Expand All @@ -224,7 +224,7 @@ public function testEvaluateUserDoesNotExist() {
->withConsecutive(
['foobar']
)
->will($this->returnValue(false));
->willReturn(false);

$this->listener->evaluate($event);
}
Expand Down
4 changes: 2 additions & 2 deletions apps/comments/tests/Unit/Notification/NotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ protected function setUp(): void {
$this->l = $this->createMock(IL10N::class);
$this->l->expects($this->any())
->method('t')
->will($this->returnCallback(function ($text, $parameters = []) {
->willReturnCallback(function ($text, $parameters = []) {
return vsprintf($text, $parameters);
}));
});

$this->notification = $this->createMock(INotification::class);
$this->comment = $this->createMock(IComment::class);
Expand Down
8 changes: 4 additions & 4 deletions apps/dav/tests/unit/AppInfo/PluginManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ public function test() {
];

$appManager->method('getAppInfo')
->will($this->returnValueMap([
->willReturnMap([
['adavapp', false, null, $appInfo1],
['adavapp2', false, null, $appInfo2],
]));
]);

$pluginManager = new PluginManager($server, $appManager);

Expand All @@ -96,7 +96,7 @@ public function test() {
$calendarPlugin3 = $this->createMock(ICalendarProvider::class);

$server->method('query')
->will($this->returnValueMap([
->willReturnMap([
['\OCA\DAV\ADavApp\PluginOne', true, 'dummyplugin1'],
['\OCA\DAV\ADavApp\PluginTwo', true, 'dummyplugin2'],
['\OCA\DAV\ADavApp\CalendarPluginOne', true, $calendarPlugin1],
Expand All @@ -106,7 +106,7 @@ public function test() {
['\OCA\DAV\ADavApp2\PluginOne', true, 'dummy2plugin1'],
['\OCA\DAV\ADavApp2\CalendarPluginOne', true, $calendarPlugin3],
['\OCA\DAV\ADavApp2\CollectionOne', true, 'dummy2collection1'],
]));
]);

$expectedPlugins = [
'dummyplugin1',
Expand Down
20 changes: 10 additions & 10 deletions apps/dav/tests/unit/BackgroundJob/CleanupInvitationTokenJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function testRun() {
$this->timeFactory->expects($this->once())
->method('getTime')
->with()
->will($this->returnValue(1337));
->willReturn(1337);

$queryBuilder = $this->createMock(IQueryBuilder::class);
$expr = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class);
Expand All @@ -68,36 +68,36 @@ public function testRun() {
$this->dbConnection->expects($this->once())
->method('getQueryBuilder')
->with()
->will($this->returnValue($queryBuilder));
->willReturn($queryBuilder);
$queryBuilder->method('expr')
->will($this->returnValue($expr));
->willReturn($expr);
$queryBuilder->method('createNamedParameter')
->will($this->returnValueMap([
->willReturnMap([
[1337, \PDO::PARAM_STR, null, 'namedParameter1337']
]));
]);

$expr->expects($this->once())
->method('lt')
->with('expiration', 'namedParameter1337')
->will($this->returnValue('LT STATEMENT'));
->willReturn('LT STATEMENT');

$this->dbConnection->expects($this->once())
->method('getQueryBuilder')
->with()
->will($this->returnValue($queryBuilder));
->willReturn($queryBuilder);

$queryBuilder->expects($this->at(0))
->method('delete')
->with('calendar_invitations')
->will($this->returnValue($queryBuilder));
->willReturn($queryBuilder);
$queryBuilder->expects($this->at(3))
->method('where')
->with('LT STATEMENT')
->will($this->returnValue($queryBuilder));
->willReturn($queryBuilder);
$queryBuilder->expects($this->at(4))
->method('execute')
->with()
->will($this->returnValue($stmt));
->willReturn($stmt);

$this->backgroundJob->run([]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ public function testRun() {
$this->config->expects($this->once())
->method('getAppValue')
->with('dav', 'generateBirthdayCalendar', 'yes')
->will($this->returnValue('yes'));
->willReturn('yes');

$this->config->expects($this->once())
->method('getUserValue')
->with('user123', 'dav', 'generateBirthdayCalendar', 'yes')
->will($this->returnValue('yes'));
->willReturn('yes');

$this->birthdayService->expects($this->never())
->method('resetForUser')
Expand All @@ -76,12 +76,12 @@ public function testRunAndReset() {
$this->config->expects($this->once())
->method('getAppValue')
->with('dav', 'generateBirthdayCalendar', 'yes')
->will($this->returnValue('yes'));
->willReturn('yes');

$this->config->expects($this->once())
->method('getUserValue')
->with('user123', 'dav', 'generateBirthdayCalendar', 'yes')
->will($this->returnValue('yes'));
->willReturn('yes');

$this->birthdayService->expects($this->once())
->method('resetForUser')
Expand All @@ -98,7 +98,7 @@ public function testRunGloballyDisabled() {
$this->config->expects($this->once())
->method('getAppValue')
->with('dav', 'generateBirthdayCalendar', 'yes')
->will($this->returnValue('no'));
->willReturn('no');

$this->config->expects($this->never())
->method('getUserValue');
Expand All @@ -113,12 +113,12 @@ public function testRunUserDisabled() {
$this->config->expects($this->once())
->method('getAppValue')
->with('dav', 'generateBirthdayCalendar', 'yes')
->will($this->returnValue('yes'));
->willReturn('yes');

$this->config->expects($this->once())
->method('getUserValue')
->with('user123', 'dav', 'generateBirthdayCalendar', 'yes')
->will($this->returnValue('no'));
->willReturn('no');

$this->birthdayService->expects($this->never())
->method('syncUser');
Expand Down
2 changes: 1 addition & 1 deletion apps/dav/tests/unit/BackgroundJob/RefreshWebcalJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function testRun(int $lastRun, int $time, bool $process) {
$this->config->expects($this->once())
->method('getAppValue')
->with('dav', 'calendarSubscriptionRefreshRate', 'P1W')
->will($this->returnValue('P1W'));
->willReturn('P1W');

$this->timeFactory->expects($this->once())
->method('getTime')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@ protected function setUp(): void {
public function testRun() {
$this->userManager->expects($this->once())
->method('callForSeenUsers')
->will($this->returnCallback(function($closure) {
->willReturnCallback(function($closure) {
$user1 = $this->createMock(IUser::class);
$user1->method('getUID')->will($this->returnValue('uid1'));
$user1->method('getUID')->willReturn('uid1');
$user2 = $this->createMock(IUser::class);
$user2->method('getUID')->will($this->returnValue('uid2'));
$user2->method('getUID')->willReturn('uid2');
$user3 = $this->createMock(IUser::class);
$user3->method('getUID')->will($this->returnValue('uid3'));
$user3->method('getUID')->willReturn('uid3');

$closure($user1);
$closure($user2);
$closure($user3);
}));
});

$this->jobList->expects($this->at(0))
->method('add')
Expand Down
Loading

0 comments on commit 25ab122

Please sign in to comment.