Skip to content

Commit a7a426b

Browse files
committed
Adding better tests for path resolving
1 parent 6cab3c5 commit a7a426b

2 files changed

Lines changed: 117 additions & 3 deletions

File tree

tests/Unit/APITest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Piwik\Access;
1818
use Piwik\Container\StaticContainer;
1919
use Piwik\Plugins\OpenApiDocs\API;
20+
use Piwik\Plugins\OpenApiDocs\Specs\PathResolver;
2021
use Piwik\Tests\Framework\Mock\FakeAccess;
2122

2223
/**
@@ -100,12 +101,21 @@ public function testGetOpenApiSpecThrowsExceptionWhenSpecIsNotAValidPlugin()
100101
$api->getOpenApiSpec('DefinitelyNotARealPlugin');
101102
}
102103

103-
public function testGetSpecFilePathUsesPluginSpecificFileName()
104+
public function testGetSpecFilePathDelegatesToPathResolver()
104105
{
105-
$api = new API();
106+
$pathResolver = $this->createMock(PathResolver::class);
107+
$pathResolver->expects($this->once())
108+
->method('getSpecFilePath')
109+
->with('CustomAlerts')
110+
->willReturn('/shared/specs/CustomAlerts_openapi_spec_v1.0.0.json');
111+
112+
$api = $this->getMockBuilder(API::class)
113+
->onlyMethods(['getSpecPathResolver'])
114+
->getMock();
115+
$api->method('getSpecPathResolver')->willReturn($pathResolver);
106116

107117
$this->assertSame(
108-
PIWIK_INCLUDE_PATH . '/plugins/OpenApiDocs/tmp/specs/CustomAlerts_openapi_spec_v1.0.0.json',
118+
'/shared/specs/CustomAlerts_openapi_spec_v1.0.0.json',
109119
$this->callProtectedMethod($api, 'getSpecFilePath', ['CustomAlerts'])
110120
);
111121
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
/**
4+
* Matomo - free/libre analytics platform
5+
*
6+
* @link https://matomo.org
7+
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Piwik\Plugins\OpenApiDocs\tests\Unit\Specs;
13+
14+
require_once PIWIK_INCLUDE_PATH . '/plugins/OpenApiDocs/vendor/autoload.php';
15+
16+
use PHPUnit\Framework\TestCase;
17+
use Piwik\Container\Container;
18+
use Piwik\Plugins\OpenApiDocs\Specs\PathResolver;
19+
20+
/**
21+
* @group OpenApiDocs
22+
* @group OpenApiDocs_Unit
23+
* @group OpenApiDocs_PathResolverTest
24+
*/
25+
class PathResolverTest extends TestCase
26+
{
27+
public function testReturnsPluginLocalPathsWhenCloudIsDisabled(): void
28+
{
29+
$resolver = new PathResolver('/plugins/OpenApiDocs', false);
30+
31+
$this->assertSame('/plugins/OpenApiDocs/tmp/specs/', $resolver->getSpecDirectory());
32+
$this->assertSame('/plugins/OpenApiDocs/tmp/annotations/', $resolver->getAnnotationsDirectory());
33+
$this->assertSame('/plugins/OpenApiDocs/tmp/responses/', $resolver->getResponsesDirectory());
34+
}
35+
36+
public function testReturnsSharedPathsWhenCloudIsEnabledAndDistributedCachePathExists(): void
37+
{
38+
$resolver = new PathResolver('/plugins/OpenApiDocs', true, $this->buildContainerStub(true, '/cache/distributed'));
39+
40+
$this->assertSame('/cache/distributed/OpenApiDocs/specs/', $resolver->getSpecDirectory());
41+
$this->assertSame('/cache/distributed/OpenApiDocs/annotations/', $resolver->getAnnotationsDirectory());
42+
$this->assertSame('/cache/distributed/OpenApiDocs/responses/', $resolver->getResponsesDirectory());
43+
}
44+
45+
public function testFallsBackToPluginLocalPathsWhenCloudCachePathIsMissing(): void
46+
{
47+
$resolver = new PathResolver('/plugins/OpenApiDocs', true, $this->buildContainerStub(false));
48+
49+
$this->assertSame('/plugins/OpenApiDocs/tmp/specs/', $resolver->getSpecDirectory());
50+
$this->assertSame('/plugins/OpenApiDocs/tmp/annotations/', $resolver->getAnnotationsDirectory());
51+
$this->assertSame('/plugins/OpenApiDocs/tmp/responses/', $resolver->getResponsesDirectory());
52+
}
53+
54+
public function testFallsBackToPluginLocalPathsWhenCloudCachePathIsEmpty(): void
55+
{
56+
$resolver = new PathResolver('/plugins/OpenApiDocs', true, $this->buildContainerStub(true, ' '));
57+
58+
$this->assertSame('/plugins/OpenApiDocs/tmp/specs/', $resolver->getSpecDirectory());
59+
$this->assertSame('/plugins/OpenApiDocs/tmp/annotations/', $resolver->getAnnotationsDirectory());
60+
$this->assertSame('/plugins/OpenApiDocs/tmp/responses/', $resolver->getResponsesDirectory());
61+
}
62+
63+
public function testBuildsFilePathsUsingExpectedNamingConventions(): void
64+
{
65+
$resolver = new PathResolver('/plugins/OpenApiDocs', true, $this->buildContainerStub(true, '/cache/distributed/'));
66+
67+
$this->assertSame(
68+
'/cache/distributed/OpenApiDocs/specs/CustomAlerts_openapi_spec_v2.0.0.yaml',
69+
$resolver->getSpecFilePath('CustomAlerts', '2.0.0', 'YAML')
70+
);
71+
$this->assertSame(
72+
'/cache/distributed/OpenApiDocs/annotations/CustomAlertsGeneratedAnnotations.php',
73+
$resolver->getAnnotationFilePath('CustomAlerts')
74+
);
75+
$this->assertSame(
76+
'/cache/distributed/OpenApiDocs/annotations/matomo_api_method_info.json',
77+
$resolver->getApiMethodInfoFilePath('matomo')
78+
);
79+
$this->assertSame(
80+
'/cache/distributed/OpenApiDocs/responses/CustomAlerts.getAlerts.json',
81+
$resolver->getExampleResponseFilePath('CustomAlerts', 'getAlerts', 'JSON')
82+
);
83+
}
84+
85+
private function buildContainerStub(bool $hasDistributedCachePath, string $distributedCachePath = ''): Container
86+
{
87+
$container = $this->getMockBuilder(Container::class)
88+
->disableOriginalConstructor()
89+
->onlyMethods(['has', 'get'])
90+
->getMock();
91+
92+
$container->method('has')
93+
->with('CloudDistributedCachePath')
94+
->willReturn($hasDistributedCachePath);
95+
96+
if ($hasDistributedCachePath) {
97+
$container->method('get')
98+
->with('CloudDistributedCachePath')
99+
->willReturn($distributedCachePath);
100+
}
101+
102+
return $container;
103+
}
104+
}

0 commit comments

Comments
 (0)