checker = $this->createMock(Checker::class); $this->fileAccessHelper = $this->createMock(FileAccessHelper::class); $this->signCore = new SignCore( $this->checker, $this->fileAccessHelper ); } public function testExecuteWithMissingPrivateKey(): void { $inputInterface = $this->createMock(InputInterface::class); $outputInterface = $this->createMock(OutputInterface::class); $inputInterface ->expects($this->exactly(3)) ->method('getOption') ->willReturnMap([ ['privateKey', null], ['certificate', 'certificate'], ['path', 'certificate'], ]); $outputInterface ->expects($this->any()) ->method('writeln') ->willReturnCallback(function (string $message) { $this->assertEquals('--privateKey, --certificate and --path are required.', $message); }); $this->assertSame(1, self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface])); } public function testExecuteWithMissingCertificate(): void { $inputInterface = $this->createMock(InputInterface::class); $outputInterface = $this->createMock(OutputInterface::class); $inputInterface ->expects($this->exactly(3)) ->method('getOption') ->willReturnMap([ ['privateKey', 'privateKey'], ['certificate', null], ['path', 'certificate'], ]); $outputInterface ->expects($this->any()) ->method('writeln') ->willReturnCallback(function (string $message) { $this->assertEquals('--privateKey, --certificate and --path are required.', $message); }); $this->assertSame(1, self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface])); } public function testExecuteWithNotExistingPrivateKey(): void { $inputInterface = $this->createMock(InputInterface::class); $outputInterface = $this->createMock(OutputInterface::class); $inputInterface ->expects($this->exactly(3)) ->method('getOption') ->willReturnMap([ ['privateKey', 'privateKey'], ['certificate', 'certificate'], ['path', 'certificate'], ]); $this->fileAccessHelper ->method('file_get_contents') ->willReturnMap([ ['privateKey', false], ]); $outputInterface ->expects($this->any()) ->method('writeln') ->willReturnCallback(function (string $message) { $this->assertEquals('Private key "privateKey" does not exists.', $message); }); $this->assertSame(1, self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface])); } public function testExecuteWithNotExistingCertificate(): void { $inputInterface = $this->createMock(InputInterface::class); $outputInterface = $this->createMock(OutputInterface::class); $inputInterface ->expects($this->exactly(3)) ->method('getOption') ->willReturnMap([ ['privateKey', 'privateKey'], ['certificate', 'certificate'], ['path', 'certificate'], ]); $this->fileAccessHelper ->expects($this->any()) ->method('file_get_contents') ->willReturnMap([ ['privateKey', file_get_contents(\OC::$SERVERROOT . '/tests/data/integritycheck/core.key')], ['certificate', false], ]); $outputInterface ->expects($this->any()) ->method('writeln') ->willReturnCallback(function (string $message) { $this->assertEquals('Certificate "certificate" does not exists.', $message); }); $this->assertSame(1, self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface])); } public function testExecuteWithException(): void { $inputInterface = $this->createMock(InputInterface::class); $outputInterface = $this->createMock(OutputInterface::class); $inputInterface ->expects($this->exactly(3)) ->method('getOption') ->willReturnMap([ ['privateKey', 'privateKey'], ['certificate', 'certificate'], ['path', 'certificate'], ]); $this->fileAccessHelper ->expects($this->any()) ->method('file_get_contents') ->willReturnMap([ ['privateKey', file_get_contents(\OC::$SERVERROOT . '/tests/data/integritycheck/core.key')], ['certificate', file_get_contents(\OC::$SERVERROOT . '/tests/data/integritycheck/core.crt')], ]); $this->checker ->expects($this->once()) ->method('writeCoreSignature') ->willThrowException(new \Exception('My exception message')); $outputInterface ->expects($this->any()) ->method('writeln') ->willReturnCallback(function (string $message) { $this->assertEquals('Error: My exception message', $message); }); $this->assertEquals(1, self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface])); } public function testExecute(): void { $inputInterface = $this->createMock(InputInterface::class); $outputInterface = $this->createMock(OutputInterface::class); $inputInterface ->expects($this->exactly(3)) ->method('getOption') ->willReturnMap([ ['privateKey', 'privateKey'], ['certificate', 'certificate'], ['path', 'certificate'], ]); $this->fileAccessHelper ->expects($this->any()) ->method('file_get_contents') ->willReturnMap([ ['privateKey', file_get_contents(\OC::$SERVERROOT . '/tests/data/integritycheck/core.key')], ['certificate', file_get_contents(\OC::$SERVERROOT . '/tests/data/integritycheck/core.crt')], ]); $this->checker ->expects($this->once()) ->method('writeCoreSignature'); $outputInterface ->expects($this->any()) ->method('writeln') ->willReturnCallback(function (string $message) { $this->assertEquals('Successfully signed "core"', $message); }); $this->assertEquals(0, self::invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface])); } }