From 3a7cf40aaa678bea1df143d2982d603b7a334eec Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Wed, 27 Nov 2019 15:27:18 +0100 Subject: Mode to modern phpunit Signed-off-by: Roeland Jago Douma --- .../tests/Command/TestEnableMasterKey.php | 2 +- apps/encryption/tests/Crypto/CryptTest.php | 11 +++++--- apps/encryption/tests/Crypto/DecryptAllTest.php | 2 +- apps/encryption/tests/Crypto/EncryptAllTest.php | 2 +- apps/encryption/tests/Crypto/EncryptionTest.php | 13 +++++---- apps/encryption/tests/HookManagerTest.php | 12 ++------ apps/encryption/tests/KeyManagerTest.php | 20 +++++++------- apps/encryption/tests/SessionTest.php | 32 ++++++++++------------ apps/encryption/tests/Settings/AdminTest.php | 2 +- 9 files changed, 46 insertions(+), 50 deletions(-) (limited to 'apps/encryption') diff --git a/apps/encryption/tests/Command/TestEnableMasterKey.php b/apps/encryption/tests/Command/TestEnableMasterKey.php index ef6bd9eec22..927cc1328df 100644 --- a/apps/encryption/tests/Command/TestEnableMasterKey.php +++ b/apps/encryption/tests/Command/TestEnableMasterKey.php @@ -53,7 +53,7 @@ class TestEnableMasterKey extends TestCase { /** @var \Symfony\Component\Console\Input\InputInterface | \PHPUnit_Framework_MockObject_MockObject */ protected $input; - public function setUp(): void { + protected function setUp(): void { parent::setUp(); $this->util = $this->getMockBuilder(Util::class) diff --git a/apps/encryption/tests/Crypto/CryptTest.php b/apps/encryption/tests/Crypto/CryptTest.php index 1731f25d532..053c4ce8f03 100644 --- a/apps/encryption/tests/Crypto/CryptTest.php +++ b/apps/encryption/tests/Crypto/CryptTest.php @@ -53,7 +53,7 @@ class CryptTest extends TestCase { /** @var Crypt */ private $crypt; - public function setUp(): void { + protected function setUp(): void { parent::setUp(); $this->logger = $this->getMockBuilder(ILogger::class) @@ -132,9 +132,10 @@ class CryptTest extends TestCase { /** * test generateHeader with invalid key format * - * @expectedException \InvalidArgumentException */ public function testGenerateHeaderInvalid() { + $this->expectException(\InvalidArgumentException::class); + $this->crypt->generateHeader('unknown'); } @@ -252,9 +253,10 @@ class CryptTest extends TestCase { /** * @dataProvider dataTestHasSignatureFail - * @expectedException \OCP\Encryption\Exceptions\GenericEncryptionException */ public function testHasSignatureFail($cipher) { + $this->expectException(\OCP\Encryption\Exceptions\GenericEncryptionException::class); + $data = 'encryptedContent00iv001234567890123456xx'; $this->invokePrivate($this->crypt, 'hasSignature', array($data, $cipher)); } @@ -371,9 +373,10 @@ class CryptTest extends TestCase { /** * test exception if cipher is unknown * - * @expectedException \InvalidArgumentException */ public function testGetKeySizeFailure() { + $this->expectException(\InvalidArgumentException::class); + $this->invokePrivate($this->crypt, 'getKeySize', ['foo']); } diff --git a/apps/encryption/tests/Crypto/DecryptAllTest.php b/apps/encryption/tests/Crypto/DecryptAllTest.php index b314bb5b543..1b94d1d96cf 100644 --- a/apps/encryption/tests/Crypto/DecryptAllTest.php +++ b/apps/encryption/tests/Crypto/DecryptAllTest.php @@ -53,7 +53,7 @@ class DecryptAllTest extends TestCase { /** @var QuestionHelper | \PHPUnit_Framework_MockObject_MockObject */ protected $questionHelper; - public function setUp(): void { + protected function setUp(): void { parent::setUp(); $this->util = $this->getMockBuilder(Util::class) diff --git a/apps/encryption/tests/Crypto/EncryptAllTest.php b/apps/encryption/tests/Crypto/EncryptAllTest.php index f5f042aac66..f8be7210054 100644 --- a/apps/encryption/tests/Crypto/EncryptAllTest.php +++ b/apps/encryption/tests/Crypto/EncryptAllTest.php @@ -90,7 +90,7 @@ class EncryptAllTest extends TestCase { /** @var EncryptAll */ protected $encryptAll; - function setUp(): void { + protected function setUp(): void { parent::setUp(); $this->setupUser = $this->getMockBuilder(Setup::class) ->disableOriginalConstructor()->getMock(); diff --git a/apps/encryption/tests/Crypto/EncryptionTest.php b/apps/encryption/tests/Crypto/EncryptionTest.php index 4136673a6ec..94e0729317f 100644 --- a/apps/encryption/tests/Crypto/EncryptionTest.php +++ b/apps/encryption/tests/Crypto/EncryptionTest.php @@ -73,7 +73,7 @@ class EncryptionTest extends TestCase { /** @var \OCP\Files\Storage|\PHPUnit_Framework_MockObject_MockObject */ private $storageMock; - public function setUp(): void { + protected function setUp(): void { parent::setUp(); $this->storageMock = $this->getMockBuilder(Storage::class) @@ -131,9 +131,10 @@ class EncryptionTest extends TestCase { /** * test if public key from owner is missing * - * @expectedException \OCA\Encryption\Exceptions\PublicKeyMissingException */ public function testEndUser2() { + $this->expectException(\OCA\Encryption\Exceptions\PublicKeyMissingException::class); + $this->instance->begin('/foo/bar', 'user2', 'r', array(), array('users' => array('user1', 'user2', 'user3'))); $this->endTest(); } @@ -431,11 +432,11 @@ class EncryptionTest extends TestCase { ); } - /** - * @expectedException \OC\Encryption\Exceptions\DecryptionFailedException - * @expectedExceptionMessage Can not decrypt this file, probably this is a shared file. Please ask the file owner to reshare the file with you. - */ + public function testDecrypt() { + $this->expectException(\OC\Encryption\Exceptions\DecryptionFailedException::class); + $this->expectExceptionMessage('Can not decrypt this file, probably this is a shared file. Please ask the file owner to reshare the file with you.'); + $this->instance->decrypt('abc'); } diff --git a/apps/encryption/tests/HookManagerTest.php b/apps/encryption/tests/HookManagerTest.php index 5ff76f918d4..39797942256 100644 --- a/apps/encryption/tests/HookManagerTest.php +++ b/apps/encryption/tests/HookManagerTest.php @@ -38,9 +38,7 @@ class HookManagerTest extends TestCase { */ private static $instance; - /** - * - */ + public function testRegisterHookWithArray() { self::$instance->registerHook([ $this->getMockBuilder(IHook::class)->disableOriginalConstructor()->getMock(), @@ -54,9 +52,7 @@ class HookManagerTest extends TestCase { } - /** - * - */ + public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); // have to make instance static to preserve data between tests @@ -64,9 +60,7 @@ class HookManagerTest extends TestCase { } - /** - * - */ + public function testRegisterHooksWithInstance() { $mock = $this->getMockBuilder(IHook::class)->disableOriginalConstructor()->getMock(); /** @var \OCA\Encryption\Hooks\Contracts\IHook $mock */ diff --git a/apps/encryption/tests/KeyManagerTest.php b/apps/encryption/tests/KeyManagerTest.php index 6e5c51db3c8..fd2cac6461b 100644 --- a/apps/encryption/tests/KeyManagerTest.php +++ b/apps/encryption/tests/KeyManagerTest.php @@ -79,7 +79,7 @@ class KeyManagerTest extends TestCase { /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */ private $configMock; - public function setUp(): void { + protected function setUp(): void { parent::setUp(); $this->userId = 'user1'; $this->systemKeyId = 'systemKeyId'; @@ -220,10 +220,10 @@ class KeyManagerTest extends TestCase { ]; } - /** - * @expectedException \OCA\Encryption\Exceptions\PrivateKeyMissingException - */ + public function testUserHasKeysMissingPrivateKey() { + $this->expectException(\OCA\Encryption\Exceptions\PrivateKeyMissingException::class); + $this->keyStorageMock->expects($this->exactly(2)) ->method('getUserKey') ->willReturnCallback(function ($uid, $keyID, $encryptionModuleId) { @@ -236,10 +236,10 @@ class KeyManagerTest extends TestCase { $this->instance->userHasKeys($this->userId); } - /** - * @expectedException \OCA\Encryption\Exceptions\PublicKeyMissingException - */ + public function testUserHasKeysMissingPublicKey() { + $this->expectException(\OCA\Encryption\Exceptions\PublicKeyMissingException::class); + $this->keyStorageMock->expects($this->exactly(2)) ->method('getUserKey') ->willReturnCallback(function ($uid, $keyID, $encryptionModuleId){ @@ -536,10 +536,10 @@ class KeyManagerTest extends TestCase { ); } - /** - * @expectedException \Exception - */ + public function testGetMasterKeyPasswordException() { + $this->expectException(\Exception::class); + $this->configMock->expects($this->once())->method('getSystemValue')->with('secret') ->willReturn(''); diff --git a/apps/encryption/tests/SessionTest.php b/apps/encryption/tests/SessionTest.php index 25c652b36af..5a0a65c77b3 100644 --- a/apps/encryption/tests/SessionTest.php +++ b/apps/encryption/tests/SessionTest.php @@ -41,11 +41,11 @@ class SessionTest extends TestCase { /** @var \OCP\ISession|\PHPUnit_Framework_MockObject_MockObject */ private $sessionMock; - /** - * @expectedException \OCA\Encryption\Exceptions\PrivateKeyMissingException - * @expectedExceptionMessage Private Key missing for user: please try to log-out and log-in again - */ + public function testThatGetPrivateKeyThrowsExceptionWhenNotSet() { + $this->expectException(\OCA\Encryption\Exceptions\PrivateKeyMissingException::class); + $this->expectExceptionMessage('Private Key missing for user: please try to log-out and log-in again'); + $this->instance->getPrivateKey(); } @@ -84,42 +84,44 @@ class SessionTest extends TestCase { } /** - * @expectedException \Exception * @expectExceptionMessage 'Please activate decrypt all mode first' */ public function testGetDecryptAllUidException() { + $this->expectException(\Exception::class); + $this->instance->getDecryptAllUid(); } /** - * @expectedException \Exception * @expectExceptionMessage 'No uid found while in decrypt all mode' */ public function testGetDecryptAllUidException2() { + $this->expectException(\Exception::class); + $this->instance->prepareDecryptAll(null, 'key'); $this->instance->getDecryptAllUid(); } /** - * @expectedException \OCA\Encryption\Exceptions\PrivateKeyMissingException * @expectExceptionMessage 'Please activate decrypt all mode first' */ public function testGetDecryptAllKeyException() { + $this->expectException(\OCA\Encryption\Exceptions\PrivateKeyMissingException::class); + $this->instance->getDecryptAllKey(); } /** - * @expectedException \OCA\Encryption\Exceptions\PrivateKeyMissingException * @expectExceptionMessage 'No key found while in decrypt all mode' */ public function testGetDecryptAllKeyException2() { + $this->expectException(\OCA\Encryption\Exceptions\PrivateKeyMissingException::class); + $this->instance->prepareDecryptAll('user', null); $this->instance->getDecryptAllKey(); } - /** - * - */ + public function testSetAndGetStatusWillSetAndReturn() { // Check if get status will return 0 if it has not been set before $this->assertEquals(0, $this->instance->getStatus()); @@ -186,9 +188,7 @@ class SessionTest extends TestCase { return null; } - /** - * - */ + public function testClearWillRemoveValues() { $this->instance->setPrivateKey('privateKey'); $this->instance->setStatus('initStatus'); @@ -198,9 +198,7 @@ class SessionTest extends TestCase { $this->assertEmpty(self::$tempStorage); } - /** - * - */ + protected function setUp(): void { parent::setUp(); $this->sessionMock = $this->createMock(ISession::class); diff --git a/apps/encryption/tests/Settings/AdminTest.php b/apps/encryption/tests/Settings/AdminTest.php index 9df483916b7..8f68f06b6bd 100644 --- a/apps/encryption/tests/Settings/AdminTest.php +++ b/apps/encryption/tests/Settings/AdminTest.php @@ -50,7 +50,7 @@ class AdminTest extends TestCase { /** @var ISession */ private $session; - public function setUp(): void { + protected function setUp(): void { parent::setUp(); $this->l = $this->getMockBuilder(IL10N::class)->getMock(); -- cgit v1.2.3