summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Molakvoæ <skjnldsv@users.noreply.github.com>2019-06-14 14:12:50 +0200
committerGitHub <noreply@github.com>2019-06-14 14:12:50 +0200
commitbf18f1ee934b71e21b060450a30e321d98ab2da2 (patch)
tree0c085b17e659c1b8298996689912746636187459
parent85767660baa8b9e686303b091da8c96b13345f86 (diff)
parente9351ef779ea8e462062eaeb3e1824fe66829f7d (diff)
downloadnextcloud-server-bf18f1ee934b71e21b060450a30e321d98ab2da2.tar.gz
nextcloud-server-bf18f1ee934b71e21b060450a30e321d98ab2da2.zip
Add strict type on Notifications tests (#15935)
Add strict type on Notifications tests
-rw-r--r--apps/comments/tests/Unit/Controller/NotificationsTest.php23
-rw-r--r--lib/private/Notification/Notification.php40
-rw-r--r--lib/public/Notification/INotification.php18
-rw-r--r--tests/lib/Notification/NotificationTest.php49
4 files changed, 91 insertions, 39 deletions
diff --git a/apps/comments/tests/Unit/Controller/NotificationsTest.php b/apps/comments/tests/Unit/Controller/NotificationsTest.php
index 9897027b824..c5209b9f9d5 100644
--- a/apps/comments/tests/Unit/Controller/NotificationsTest.php
+++ b/apps/comments/tests/Unit/Controller/NotificationsTest.php
@@ -125,6 +125,7 @@ class NotificationsTest extends TestCase {
$file = $this->createMock(Node::class);
$folder = $this->createMock(Folder::class);
+ $user = $this->createMock(IUser::class);
$this->rootFolder->expects($this->once())
->method('getUserFolder')
@@ -136,7 +137,11 @@ class NotificationsTest extends TestCase {
$this->session->expects($this->once())
->method('getUser')
- ->willReturn($this->createMock(IUser::class));
+ ->willReturn($user);
+
+ $user->expects($this->any())
+ ->method('getUID')
+ ->willReturn('user');
$notification = $this->createMock(INotification::class);
$notification->expects($this->any())
@@ -163,9 +168,15 @@ class NotificationsTest extends TestCase {
$this->rootFolder->expects($this->never())
->method('getUserFolder');
+ $user = $this->createMock(IUser::class);
+
$this->session->expects($this->once())
->method('getUser')
- ->willReturn($this->createMock(IUser::class));
+ ->willReturn($user);
+
+ $user->expects($this->any())
+ ->method('getUID')
+ ->willReturn('user');
$this->notificationManager->expects($this->never())
->method('createNotification');
@@ -197,9 +208,15 @@ class NotificationsTest extends TestCase {
->method('getById')
->willReturn([]);
+ $user = $this->createMock(IUser::class);
+
$this->session->expects($this->once())
->method('getUser')
- ->willReturn($this->createMock(IUser::class));
+ ->willReturn($user);
+
+ $user->expects($this->any())
+ ->method('getUID')
+ ->willReturn('user');
$notification = $this->createMock(INotification::class);
$notification->expects($this->any())
diff --git a/lib/private/Notification/Notification.php b/lib/private/Notification/Notification.php
index ff3826047dd..47f415d15d3 100644
--- a/lib/private/Notification/Notification.php
+++ b/lib/private/Notification/Notification.php
@@ -135,7 +135,7 @@ class Notification implements INotification {
* @since 8.2.0
*/
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');
}
$this->app = $app;
@@ -157,7 +157,7 @@ class Notification implements INotification {
* @since 8.2.0
*/
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');
}
$this->user = $user;
@@ -201,8 +201,8 @@ class Notification implements INotification {
* @throws \InvalidArgumentException if the object type or id is invalid
* @since 8.2.0 - 9.0.0: Type of $id changed to string
*/
- public function setObject($type, $id) {
- if (!is_string($type) || $type === '' || isset($type[64])) {
+ public function setObject(string $type, $id) {
+ if (trim($type) === '' || isset($type[64])) {
throw new \InvalidArgumentException('The given object type is invalid');
}
$this->objectType = $type;
@@ -237,8 +237,8 @@ class Notification implements INotification {
* @throws \InvalidArgumentException if the subject or parameters are invalid
* @since 8.2.0
*/
- public function setSubject($subject, array $parameters = []) {
- if (!is_string($subject) || $subject === '' || isset($subject[64])) {
+ public function setSubject(string $subject, array $parameters = []) {
+ if (trim($subject) === '' || isset($subject[64])) {
throw new \InvalidArgumentException('The given subject is invalid');
}
@@ -270,8 +270,8 @@ class Notification implements INotification {
* @throws \InvalidArgumentException if the subject is invalid
* @since 8.2.0
*/
- public function setParsedSubject($subject) {
- if (!is_string($subject) || $subject === '') {
+ public function setParsedSubject(string $subject) {
+ if (trim($subject) === '') {
throw new \InvalidArgumentException('The given parsed subject is invalid');
}
$this->subjectParsed = $subject;
@@ -293,8 +293,8 @@ class Notification implements INotification {
* @throws \InvalidArgumentException if the subject or parameters are invalid
* @since 11.0.0
*/
- public function setRichSubject($subject, array $parameters = []) {
- if (!is_string($subject) || $subject === '') {
+ public function setRichSubject(string $subject, array $parameters = []) {
+ if (trim($subject) === '') {
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
* @since 8.2.0
*/
- public function setMessage($message, array $parameters = []) {
- if (!is_string($message) || $message === '' || isset($message[64])) {
+ public function setMessage(string $message, array $parameters = []) {
+ if (trim($message) === '' || isset($message[64])) {
throw new \InvalidArgumentException('The given message is invalid');
}
@@ -360,8 +360,8 @@ class Notification implements INotification {
* @throws \InvalidArgumentException if the message is invalid
* @since 8.2.0
*/
- public function setParsedMessage($message) {
- if (!is_string($message) || $message === '') {
+ public function setParsedMessage(string $message) {
+ if (trim($message) === '') {
throw new \InvalidArgumentException('The given parsed message is invalid');
}
$this->messageParsed = $message;
@@ -383,8 +383,8 @@ class Notification implements INotification {
* @throws \InvalidArgumentException if the message or parameters are invalid
* @since 11.0.0
*/
- public function setRichMessage($message, array $parameters = []) {
- if (!is_string($message) || $message === '') {
+ public function setRichMessage(string $message, array $parameters = []) {
+ if (trim($message) === '') {
throw new \InvalidArgumentException('The given parsed message is invalid');
}
@@ -416,8 +416,8 @@ class Notification implements INotification {
* @throws \InvalidArgumentException if the link is invalid
* @since 8.2.0
*/
- public function setLink($link) {
- if (!is_string($link) || $link === '' || isset($link[4000])) {
+ public function setLink(string $link) {
+ if (trim($link) === '' || isset($link[4000])) {
throw new \InvalidArgumentException('The given link is invalid');
}
$this->link = $link;
@@ -438,8 +438,8 @@ class Notification implements INotification {
* @throws \InvalidArgumentException if the icon is invalid
* @since 11.0.0
*/
- public function setIcon($icon) {
- if (!is_string($icon) || $icon === '' || isset($icon[4000])) {
+ public function setIcon(string $icon) {
+ if (trim($icon) === '' || isset($icon[4000])) {
throw new \InvalidArgumentException('The given icon is invalid');
}
$this->icon = $icon;
diff --git a/lib/public/Notification/INotification.php b/lib/public/Notification/INotification.php
index f71d0c53cc8..f7400e4778b 100644
--- a/lib/public/Notification/INotification.php
+++ b/lib/public/Notification/INotification.php
@@ -80,7 +80,7 @@ interface INotification {
* @throws \InvalidArgumentException if the object type or id is invalid
* @since 9.0.0
*/
- public function setObject($type, $id);
+ public function setObject(string $type, $id);
/**
* @return string
@@ -101,7 +101,7 @@ interface INotification {
* @throws \InvalidArgumentException if the subject or parameters are invalid
* @since 9.0.0
*/
- public function setSubject($subject, array $parameters = []);
+ public function setSubject(string $subject, array $parameters = []);
/**
* @return string
@@ -132,7 +132,7 @@ interface INotification {
* @throws \InvalidArgumentException if the subject is invalid
* @since 9.0.0
*/
- public function setParsedSubject($subject);
+ public function setParsedSubject(string $subject);
/**
* @return string
@@ -157,7 +157,7 @@ interface INotification {
* @throws \InvalidArgumentException if the subject or parameters are invalid
* @since 11.0.0
*/
- public function setRichSubject($subject, array $parameters = []);
+ public function setRichSubject(string $subject, array $parameters = []);
/**
* @return string
@@ -178,7 +178,7 @@ interface INotification {
* @throws \InvalidArgumentException if the message or parameters are invalid
* @since 9.0.0
*/
- public function setMessage($message, array $parameters = []);
+ public function setMessage(string $message, array $parameters = []);
/**
* @return string
@@ -209,7 +209,7 @@ interface INotification {
* @throws \InvalidArgumentException if the message is invalid
* @since 9.0.0
*/
- public function setParsedMessage($message);
+ public function setParsedMessage(string $message);
/**
* @return string
@@ -234,7 +234,7 @@ interface INotification {
* @throws \InvalidArgumentException if the message or parameters are invalid
* @since 11.0.0
*/
- public function setRichMessage($message, array $parameters = []);
+ public function setRichMessage(string $message, array $parameters = []);
/**
* @return string
@@ -254,7 +254,7 @@ interface INotification {
* @throws \InvalidArgumentException if the link is invalid
* @since 9.0.0
*/
- public function setLink($link);
+ public function setLink(string $link);
/**
* @return string
@@ -268,7 +268,7 @@ interface INotification {
* @throws \InvalidArgumentException if the icon is invalid
* @since 11.0.0
*/
- public function setIcon($icon);
+ public function setIcon(string $icon);
/**
* @return string
diff --git a/tests/lib/Notification/NotificationTest.php b/tests/lib/Notification/NotificationTest.php
index 5aa24fc762a..7517be715ee 100644
--- a/tests/lib/Notification/NotificationTest.php
+++ b/tests/lib/Notification/NotificationTest.php
@@ -1,4 +1,5 @@
<?php
+declare (strict_types = 1);
/**
* @author Joas Schilling <nickvergessen@owncloud.com>
*
@@ -43,6 +44,7 @@ class NotificationTest extends TestCase {
protected function dataValidString($maxLength) {
$dataSets = [
['test1'],
+ ['1564'],
[str_repeat('a', 1)],
];
if ($maxLength !== false) {
@@ -53,20 +55,24 @@ class NotificationTest extends TestCase {
protected function dataInvalidString($maxLength) {
$dataSets = [
- [true],
- [false],
- [0],
- [1],
- [''],
- [[]],
+ ['']
];
if ($maxLength !== false) {
$dataSets[] = [str_repeat('a', $maxLength + 1)];
- $dataSets[] = [[str_repeat('a', $maxLength + 1)]];
}
return $dataSets;
}
+ protected function dataInvalidStringType() {
+ return [
+ [true],
+ [false],
+ [16412],
+ [[]],
+ [null],
+ ];
+ }
+
protected function dataInvalidInt() {
return [
[true],
@@ -98,6 +104,10 @@ class NotificationTest extends TestCase {
return $this->dataInvalidString(32);
}
+ public function dataSetAppInvalidType() {
+ return $this->dataInvalidStringType();
+ }
+
/**
* @dataProvider dataSetAppInvalid
* @param mixed $app
@@ -108,6 +118,17 @@ class NotificationTest extends TestCase {
$this->notification->setApp($app);
}
+ /**
+ * @dataProvider dataSetAppInvalidType
+ * @param mixed $app
+ *
+ * @expectedException \TypeError
+ */
+ public function testSetAppInvalidType($app) {
+ $this->notification->setApp($app);
+ }
+
+
public function dataSetUser() {
return $this->dataValidString(64);
}
@@ -126,6 +147,10 @@ class NotificationTest extends TestCase {
return $this->dataInvalidString(64);
}
+ public function dataSetUserInvalidType() {
+ return $this->dataInvalidStringType();
+ }
+
/**
* @dataProvider dataSetUserInvalid
* @param mixed $user
@@ -136,6 +161,16 @@ class NotificationTest extends TestCase {
$this->notification->setUser($user);
}
+ /**
+ * @dataProvider dataSetUserInvalidType
+ * @param mixed $user
+ *
+ * @expectedException \TypeError
+ */
+ public function testSetUserInvalidType($user) {
+ $this->notification->setUser($user);
+ }
+
public function dataSetDateTime() {
$past = new \DateTime();
$past->sub(new \DateInterval('P1Y'));