|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2024 LibreCode coop and contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * Mock extension_loaded in the ConfigureCheckService namespace to control its behavior in tests |
| 11 | + */ |
| 12 | +namespace OCA\Libresign\Service\Install; |
| 13 | + |
| 14 | +function extension_loaded(string $name): bool { |
| 15 | + return \OCA\Libresign\Tests\Unit\Service\Install\ConfigureCheckServiceTest::$mockExtensionLoaded[$name] ?? \extension_loaded($name); |
| 16 | +} |
| 17 | + |
| 18 | +namespace OCA\Libresign\Tests\Unit\Service\Install; |
| 19 | + |
| 20 | +use OC\AppConfig; |
| 21 | +use OC\SystemConfig; |
| 22 | +use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory; |
| 23 | +use OCA\Libresign\Handler\SignEngine\JSignPdfHandler; |
| 24 | +use OCA\Libresign\Helper\ConfigureCheckHelper; |
| 25 | +use OCA\Libresign\Helper\JavaHelper; |
| 26 | +use OCA\Libresign\Service\Install\ConfigureCheckService; |
| 27 | +use OCA\Libresign\Service\Install\SignSetupService; |
| 28 | +use OCP\App\IAppManager; |
| 29 | +use OCP\IAppConfig; |
| 30 | +use OCP\IURLGenerator; |
| 31 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 32 | +use PHPUnit\Framework\MockObject\MockObject; |
| 33 | +use Psr\Log\LoggerInterface; |
| 34 | + |
| 35 | +final class ConfigureCheckServiceTest extends \OCA\Libresign\Tests\Unit\TestCase { |
| 36 | + public static array $mockExtensionLoaded = []; |
| 37 | + |
| 38 | + private IAppConfig $appConfig; |
| 39 | + private SystemConfig&MockObject $systemConfig; |
| 40 | + private AppConfig&MockObject $ocAppConfig; |
| 41 | + private IAppManager&MockObject $appManager; |
| 42 | + private IURLGenerator&MockObject $urlGenerator; |
| 43 | + private JSignPdfHandler&MockObject $jSignPdfHandler; |
| 44 | + private CertificateEngineFactory&MockObject $certificateEngineFactory; |
| 45 | + private SignSetupService&MockObject $signSetupService; |
| 46 | + private LoggerInterface&MockObject $logger; |
| 47 | + private JavaHelper&MockObject $javaHelper; |
| 48 | + |
| 49 | + public function setUp(): void { |
| 50 | + self::$mockExtensionLoaded = []; |
| 51 | + $this->appConfig = $this->getMockAppConfig(); |
| 52 | + $this->systemConfig = $this->createMock(SystemConfig::class); |
| 53 | + $this->ocAppConfig = $this->createMock(AppConfig::class); |
| 54 | + $this->appManager = $this->createMock(IAppManager::class); |
| 55 | + $this->urlGenerator = $this->createMock(IURLGenerator::class); |
| 56 | + $this->jSignPdfHandler = $this->createMock(JSignPdfHandler::class); |
| 57 | + $this->certificateEngineFactory = $this->createMock(CertificateEngineFactory::class); |
| 58 | + $this->signSetupService = $this->createMock(SignSetupService::class); |
| 59 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 60 | + $this->javaHelper = $this->createMock(JavaHelper::class); |
| 61 | + } |
| 62 | + |
| 63 | + public function tearDown(): void { |
| 64 | + self::$mockExtensionLoaded = []; |
| 65 | + } |
| 66 | + |
| 67 | + private function getInstance(): ConfigureCheckService { |
| 68 | + return new ConfigureCheckService( |
| 69 | + $this->appConfig, |
| 70 | + $this->systemConfig, |
| 71 | + $this->ocAppConfig, |
| 72 | + $this->appManager, |
| 73 | + $this->urlGenerator, |
| 74 | + $this->jSignPdfHandler, |
| 75 | + $this->certificateEngineFactory, |
| 76 | + $this->signSetupService, |
| 77 | + $this->logger, |
| 78 | + $this->javaHelper, |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + public static function providerCheckExtension(): array { |
| 83 | + return [ |
| 84 | + 'extension not loaded' => ['imagick', false, 1], |
| 85 | + 'extension loaded' => ['imagick', true, 0], |
| 86 | + ]; |
| 87 | + } |
| 88 | + |
| 89 | + #[DataProvider('providerCheckExtension')] |
| 90 | + public function testCheckExtension(string $extension, bool $extensionLoaded, int $expectedCount): void { |
| 91 | + self::$mockExtensionLoaded[$extension] = $extensionLoaded; |
| 92 | + |
| 93 | + $service = $this->getInstance(); |
| 94 | + $result = $service->checkImagick(); |
| 95 | + |
| 96 | + $this->assertIsArray($result); |
| 97 | + $this->assertCount($expectedCount, $result); |
| 98 | + |
| 99 | + if ($expectedCount > 0) { |
| 100 | + $this->assertInstanceOf(ConfigureCheckHelper::class, $result[0]); |
| 101 | + $this->assertEquals($extension, $result[0]->getResource()); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments