-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathCertificateEngineFactoryTest.php
More file actions
86 lines (73 loc) · 2.75 KB
/
CertificateEngineFactoryTest.php
File metadata and controls
86 lines (73 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory;
use OCA\Libresign\Handler\CertificateEngine\CfsslHandler;
use OCA\Libresign\Handler\CertificateEngine\NoneHandler;
use OCA\Libresign\Handler\CertificateEngine\OpenSslHandler;
use OCP\IAppConfig;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
class CertificateEngineFactoryTest extends \OCA\Libresign\Tests\Unit\TestCase {
private IAppConfig $appConfig;
private OpenSslHandler&MockObject $openSslHandler;
private CfsslHandler&MockObject $cfsslHandler;
private NoneHandler&MockObject $noneHandler;
public function tearDown(): void {
$ref = new \ReflectionClass(CertificateEngineFactory::class);
$prop = $ref->getProperty('engine');
$prop->setValue($ref, null);
}
public function setUp(): void {
$this->appConfig = $this->getMockAppConfigWithReset();
$this->openSslHandler = $this->createMock(OpenSslHandler::class);
$this->cfsslHandler = $this->createMock(CfsslHandler::class);
$this->noneHandler = $this->createMock(NoneHandler::class);
}
private function getInstance(): CertificateEngineFactory {
return new CertificateEngineFactory(
$this->appConfig,
$this->openSslHandler,
$this->cfsslHandler,
$this->noneHandler,
);
}
public static function providerGetEngineReturnsCorrectHandler(): array {
return [
'openssl engine' => ['openssl', OpenSslHandler::class],
'cfssl engine' => ['cfssl', CfsslHandler::class],
'none engine' => ['none', NoneHandler::class],
];
}
#[DataProvider('providerGetEngineReturnsCorrectHandler')]
public function testGetEngineReturnsCorrectHandler(string $engineName, string $handlerClass): void {
$mockHandler = match ($handlerClass) {
OpenSslHandler::class => $this->openSslHandler,
CfsslHandler::class => $this->cfsslHandler,
NoneHandler::class => $this->noneHandler,
};
\OC::$server->registerService($handlerClass, fn () => $mockHandler);
$factory = $this->getInstance();
$actual = $factory->getEngine($engineName, ['cert' => 'abc']);
$this->assertSame($mockHandler, $actual);
}
#[DataProvider('providerThrowsExceptionOnInvalidEngine')]
public function testThrowsExceptionOnInvalidEngine(string $invalidName): void {
$this->expectException(LibresignException::class);
$this->expectExceptionMessage('Certificate engine not found: ' . $invalidName);
$factory = $this->getInstance();
$factory->getEngine($invalidName);
}
public static function providerThrowsExceptionOnInvalidEngine(): array {
return [
['invalid'],
['test'],
['fake'],
['dummy'],
];
}
}