Add strict type on Notifications tests (#15935)

Add strict type on Notifications tests
This commit is contained in:
John Molakvoæ 2019-06-14 14:12:50 +02:00 committed by GitHub
commit bf18f1ee93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 91 additions and 39 deletions

View File

@ -125,6 +125,7 @@ class NotificationsTest extends TestCase {
$file = $this->createMock(Node::class); $file = $this->createMock(Node::class);
$folder = $this->createMock(Folder::class); $folder = $this->createMock(Folder::class);
$user = $this->createMock(IUser::class);
$this->rootFolder->expects($this->once()) $this->rootFolder->expects($this->once())
->method('getUserFolder') ->method('getUserFolder')
@ -136,7 +137,11 @@ class NotificationsTest extends TestCase {
$this->session->expects($this->once()) $this->session->expects($this->once())
->method('getUser') ->method('getUser')
->willReturn($this->createMock(IUser::class)); ->willReturn($user);
$user->expects($this->any())
->method('getUID')
->willReturn('user');
$notification = $this->createMock(INotification::class); $notification = $this->createMock(INotification::class);
$notification->expects($this->any()) $notification->expects($this->any())
@ -163,9 +168,15 @@ class NotificationsTest extends TestCase {
$this->rootFolder->expects($this->never()) $this->rootFolder->expects($this->never())
->method('getUserFolder'); ->method('getUserFolder');
$user = $this->createMock(IUser::class);
$this->session->expects($this->once()) $this->session->expects($this->once())
->method('getUser') ->method('getUser')
->willReturn($this->createMock(IUser::class)); ->willReturn($user);
$user->expects($this->any())
->method('getUID')
->willReturn('user');
$this->notificationManager->expects($this->never()) $this->notificationManager->expects($this->never())
->method('createNotification'); ->method('createNotification');
@ -197,9 +208,15 @@ class NotificationsTest extends TestCase {
->method('getById') ->method('getById')
->willReturn([]); ->willReturn([]);
$user = $this->createMock(IUser::class);
$this->session->expects($this->once()) $this->session->expects($this->once())
->method('getUser') ->method('getUser')
->willReturn($this->createMock(IUser::class)); ->willReturn($user);
$user->expects($this->any())
->method('getUID')
->willReturn('user');
$notification = $this->createMock(INotification::class); $notification = $this->createMock(INotification::class);
$notification->expects($this->any()) $notification->expects($this->any())

View File

@ -135,7 +135,7 @@ class Notification implements INotification {
* @since 8.2.0 * @since 8.2.0
*/ */
public function setApp(string $app) { public function setApp(string $app) {
if (!is_string($app) || $app === '' || isset($app[32])) { if (trim($app) === '' || isset($app[32])) {
throw new \InvalidArgumentException('The given app name is invalid'); throw new \InvalidArgumentException('The given app name is invalid');
} }
$this->app = $app; $this->app = $app;
@ -157,7 +157,7 @@ class Notification implements INotification {
* @since 8.2.0 * @since 8.2.0
*/ */
public function setUser(string $user) { public function setUser(string $user) {
if (!is_string($user) || $user === '' || isset($user[64])) { if (trim($user) === '' || isset($user[64])) {
throw new \InvalidArgumentException('The given user id is invalid'); throw new \InvalidArgumentException('The given user id is invalid');
} }
$this->user = $user; $this->user = $user;
@ -201,8 +201,8 @@ class Notification implements INotification {
* @throws \InvalidArgumentException if the object type or id is invalid * @throws \InvalidArgumentException if the object type or id is invalid
* @since 8.2.0 - 9.0.0: Type of $id changed to string * @since 8.2.0 - 9.0.0: Type of $id changed to string
*/ */
public function setObject($type, $id) { public function setObject(string $type, $id) {
if (!is_string($type) || $type === '' || isset($type[64])) { if (trim($type) === '' || isset($type[64])) {
throw new \InvalidArgumentException('The given object type is invalid'); throw new \InvalidArgumentException('The given object type is invalid');
} }
$this->objectType = $type; $this->objectType = $type;
@ -237,8 +237,8 @@ class Notification implements INotification {
* @throws \InvalidArgumentException if the subject or parameters are invalid * @throws \InvalidArgumentException if the subject or parameters are invalid
* @since 8.2.0 * @since 8.2.0
*/ */
public function setSubject($subject, array $parameters = []) { public function setSubject(string $subject, array $parameters = []) {
if (!is_string($subject) || $subject === '' || isset($subject[64])) { if (trim($subject) === '' || isset($subject[64])) {
throw new \InvalidArgumentException('The given subject is invalid'); throw new \InvalidArgumentException('The given subject is invalid');
} }
@ -270,8 +270,8 @@ class Notification implements INotification {
* @throws \InvalidArgumentException if the subject is invalid * @throws \InvalidArgumentException if the subject is invalid
* @since 8.2.0 * @since 8.2.0
*/ */
public function setParsedSubject($subject) { public function setParsedSubject(string $subject) {
if (!is_string($subject) || $subject === '') { if (trim($subject) === '') {
throw new \InvalidArgumentException('The given parsed subject is invalid'); throw new \InvalidArgumentException('The given parsed subject is invalid');
} }
$this->subjectParsed = $subject; $this->subjectParsed = $subject;
@ -293,8 +293,8 @@ class Notification implements INotification {
* @throws \InvalidArgumentException if the subject or parameters are invalid * @throws \InvalidArgumentException if the subject or parameters are invalid
* @since 11.0.0 * @since 11.0.0
*/ */
public function setRichSubject($subject, array $parameters = []) { public function setRichSubject(string $subject, array $parameters = []) {
if (!is_string($subject) || $subject === '') { if (trim($subject) === '') {
throw new \InvalidArgumentException('The given parsed subject is invalid'); throw new \InvalidArgumentException('The given parsed subject is invalid');
} }
@ -327,8 +327,8 @@ class Notification implements INotification {
* @throws \InvalidArgumentException if the message or parameters are invalid * @throws \InvalidArgumentException if the message or parameters are invalid
* @since 8.2.0 * @since 8.2.0
*/ */
public function setMessage($message, array $parameters = []) { public function setMessage(string $message, array $parameters = []) {
if (!is_string($message) || $message === '' || isset($message[64])) { if (trim($message) === '' || isset($message[64])) {
throw new \InvalidArgumentException('The given message is invalid'); throw new \InvalidArgumentException('The given message is invalid');
} }
@ -360,8 +360,8 @@ class Notification implements INotification {
* @throws \InvalidArgumentException if the message is invalid * @throws \InvalidArgumentException if the message is invalid
* @since 8.2.0 * @since 8.2.0
*/ */
public function setParsedMessage($message) { public function setParsedMessage(string $message) {
if (!is_string($message) || $message === '') { if (trim($message) === '') {
throw new \InvalidArgumentException('The given parsed message is invalid'); throw new \InvalidArgumentException('The given parsed message is invalid');
} }
$this->messageParsed = $message; $this->messageParsed = $message;
@ -383,8 +383,8 @@ class Notification implements INotification {
* @throws \InvalidArgumentException if the message or parameters are invalid * @throws \InvalidArgumentException if the message or parameters are invalid
* @since 11.0.0 * @since 11.0.0
*/ */
public function setRichMessage($message, array $parameters = []) { public function setRichMessage(string $message, array $parameters = []) {
if (!is_string($message) || $message === '') { if (trim($message) === '') {
throw new \InvalidArgumentException('The given parsed message is invalid'); throw new \InvalidArgumentException('The given parsed message is invalid');
} }
@ -416,8 +416,8 @@ class Notification implements INotification {
* @throws \InvalidArgumentException if the link is invalid * @throws \InvalidArgumentException if the link is invalid
* @since 8.2.0 * @since 8.2.0
*/ */
public function setLink($link) { public function setLink(string $link) {
if (!is_string($link) || $link === '' || isset($link[4000])) { if (trim($link) === '' || isset($link[4000])) {
throw new \InvalidArgumentException('The given link is invalid'); throw new \InvalidArgumentException('The given link is invalid');
} }
$this->link = $link; $this->link = $link;
@ -438,8 +438,8 @@ class Notification implements INotification {
* @throws \InvalidArgumentException if the icon is invalid * @throws \InvalidArgumentException if the icon is invalid
* @since 11.0.0 * @since 11.0.0
*/ */
public function setIcon($icon) { public function setIcon(string $icon) {
if (!is_string($icon) || $icon === '' || isset($icon[4000])) { if (trim($icon) === '' || isset($icon[4000])) {
throw new \InvalidArgumentException('The given icon is invalid'); throw new \InvalidArgumentException('The given icon is invalid');
} }
$this->icon = $icon; $this->icon = $icon;

View File

@ -80,7 +80,7 @@ interface INotification {
* @throws \InvalidArgumentException if the object type or id is invalid * @throws \InvalidArgumentException if the object type or id is invalid
* @since 9.0.0 * @since 9.0.0
*/ */
public function setObject($type, $id); public function setObject(string $type, $id);
/** /**
* @return string * @return string
@ -101,7 +101,7 @@ interface INotification {
* @throws \InvalidArgumentException if the subject or parameters are invalid * @throws \InvalidArgumentException if the subject or parameters are invalid
* @since 9.0.0 * @since 9.0.0
*/ */
public function setSubject($subject, array $parameters = []); public function setSubject(string $subject, array $parameters = []);
/** /**
* @return string * @return string
@ -132,7 +132,7 @@ interface INotification {
* @throws \InvalidArgumentException if the subject is invalid * @throws \InvalidArgumentException if the subject is invalid
* @since 9.0.0 * @since 9.0.0
*/ */
public function setParsedSubject($subject); public function setParsedSubject(string $subject);
/** /**
* @return string * @return string
@ -157,7 +157,7 @@ interface INotification {
* @throws \InvalidArgumentException if the subject or parameters are invalid * @throws \InvalidArgumentException if the subject or parameters are invalid
* @since 11.0.0 * @since 11.0.0
*/ */
public function setRichSubject($subject, array $parameters = []); public function setRichSubject(string $subject, array $parameters = []);
/** /**
* @return string * @return string
@ -178,7 +178,7 @@ interface INotification {
* @throws \InvalidArgumentException if the message or parameters are invalid * @throws \InvalidArgumentException if the message or parameters are invalid
* @since 9.0.0 * @since 9.0.0
*/ */
public function setMessage($message, array $parameters = []); public function setMessage(string $message, array $parameters = []);
/** /**
* @return string * @return string
@ -209,7 +209,7 @@ interface INotification {
* @throws \InvalidArgumentException if the message is invalid * @throws \InvalidArgumentException if the message is invalid
* @since 9.0.0 * @since 9.0.0
*/ */
public function setParsedMessage($message); public function setParsedMessage(string $message);
/** /**
* @return string * @return string
@ -234,7 +234,7 @@ interface INotification {
* @throws \InvalidArgumentException if the message or parameters are invalid * @throws \InvalidArgumentException if the message or parameters are invalid
* @since 11.0.0 * @since 11.0.0
*/ */
public function setRichMessage($message, array $parameters = []); public function setRichMessage(string $message, array $parameters = []);
/** /**
* @return string * @return string
@ -254,7 +254,7 @@ interface INotification {
* @throws \InvalidArgumentException if the link is invalid * @throws \InvalidArgumentException if the link is invalid
* @since 9.0.0 * @since 9.0.0
*/ */
public function setLink($link); public function setLink(string $link);
/** /**
* @return string * @return string
@ -268,7 +268,7 @@ interface INotification {
* @throws \InvalidArgumentException if the icon is invalid * @throws \InvalidArgumentException if the icon is invalid
* @since 11.0.0 * @since 11.0.0
*/ */
public function setIcon($icon); public function setIcon(string $icon);
/** /**
* @return string * @return string

View File

@ -1,4 +1,5 @@
<?php <?php
declare (strict_types = 1);
/** /**
* @author Joas Schilling <nickvergessen@owncloud.com> * @author Joas Schilling <nickvergessen@owncloud.com>
* *
@ -43,6 +44,7 @@ class NotificationTest extends TestCase {
protected function dataValidString($maxLength) { protected function dataValidString($maxLength) {
$dataSets = [ $dataSets = [
['test1'], ['test1'],
['1564'],
[str_repeat('a', 1)], [str_repeat('a', 1)],
]; ];
if ($maxLength !== false) { if ($maxLength !== false) {
@ -53,20 +55,24 @@ class NotificationTest extends TestCase {
protected function dataInvalidString($maxLength) { protected function dataInvalidString($maxLength) {
$dataSets = [ $dataSets = [
[true], ['']
[false],
[0],
[1],
[''],
[[]],
]; ];
if ($maxLength !== false) { if ($maxLength !== false) {
$dataSets[] = [str_repeat('a', $maxLength + 1)]; $dataSets[] = [str_repeat('a', $maxLength + 1)];
$dataSets[] = [[str_repeat('a', $maxLength + 1)]];
} }
return $dataSets; return $dataSets;
} }
protected function dataInvalidStringType() {
return [
[true],
[false],
[16412],
[[]],
[null],
];
}
protected function dataInvalidInt() { protected function dataInvalidInt() {
return [ return [
[true], [true],
@ -98,6 +104,10 @@ class NotificationTest extends TestCase {
return $this->dataInvalidString(32); return $this->dataInvalidString(32);
} }
public function dataSetAppInvalidType() {
return $this->dataInvalidStringType();
}
/** /**
* @dataProvider dataSetAppInvalid * @dataProvider dataSetAppInvalid
* @param mixed $app * @param mixed $app
@ -108,6 +118,17 @@ class NotificationTest extends TestCase {
$this->notification->setApp($app); $this->notification->setApp($app);
} }
/**
* @dataProvider dataSetAppInvalidType
* @param mixed $app
*
* @expectedException \TypeError
*/
public function testSetAppInvalidType($app) {
$this->notification->setApp($app);
}
public function dataSetUser() { public function dataSetUser() {
return $this->dataValidString(64); return $this->dataValidString(64);
} }
@ -126,6 +147,10 @@ class NotificationTest extends TestCase {
return $this->dataInvalidString(64); return $this->dataInvalidString(64);
} }
public function dataSetUserInvalidType() {
return $this->dataInvalidStringType();
}
/** /**
* @dataProvider dataSetUserInvalid * @dataProvider dataSetUserInvalid
* @param mixed $user * @param mixed $user
@ -136,6 +161,16 @@ class NotificationTest extends TestCase {
$this->notification->setUser($user); $this->notification->setUser($user);
} }
/**
* @dataProvider dataSetUserInvalidType
* @param mixed $user
*
* @expectedException \TypeError
*/
public function testSetUserInvalidType($user) {
$this->notification->setUser($user);
}
public function dataSetDateTime() { public function dataSetDateTime() {
$past = new \DateTime(); $past = new \DateTime();
$past->sub(new \DateInterval('P1Y')); $past->sub(new \DateInterval('P1Y'));