|
| 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\ApiReference\tests\Fixtures; |
| 13 | + |
| 14 | +use Piwik\Plugins\ApiReference\ApiReference; |
| 15 | +use Piwik\Plugins\ApiReference\Specs\PathResolver; |
| 16 | +use Piwik\Tests\Framework\Fixture; |
| 17 | +use Piwik\Updater; |
| 18 | + |
| 19 | +class SwaggerPageFixture extends Fixture |
| 20 | +{ |
| 21 | + public $dateTime = '2010-01-03 00:00:00'; |
| 22 | + public $idSite = 1; |
| 23 | + |
| 24 | + public function setUp(): void |
| 25 | + { |
| 26 | + $this->setUpWebsite(); |
| 27 | + $this->markApiReferenceAsInstalled(); |
| 28 | + $this->writeOpenApiSpecFixtures(); |
| 29 | + } |
| 30 | + |
| 31 | + public function tearDown(): void |
| 32 | + { |
| 33 | + $this->removeOpenApiSpecFixtures(); |
| 34 | + } |
| 35 | + |
| 36 | + private function setUpWebsite(): void |
| 37 | + { |
| 38 | + if (!self::siteCreated($this->idSite)) { |
| 39 | + $idSite = self::createWebsite($this->dateTime); |
| 40 | + $this->assertSame($this->idSite, $idSite); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private function writeOpenApiSpecFixtures(): void |
| 45 | + { |
| 46 | + $resolver = new PathResolver(); |
| 47 | + $specDirectory = $resolver->getSpecDirectory(); |
| 48 | + |
| 49 | + if (!is_dir($specDirectory)) { |
| 50 | + mkdir($specDirectory, 0777, true); |
| 51 | + } |
| 52 | + |
| 53 | + file_put_contents( |
| 54 | + $this->getReferrersSpecPath(), |
| 55 | + json_encode($this->getReferrersSpec(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + private function markApiReferenceAsInstalled(): void |
| 60 | + { |
| 61 | + (new Updater())->markComponentSuccessfullyUpdated('ApiReference', '5.0.0'); |
| 62 | + } |
| 63 | + |
| 64 | + private function removeOpenApiSpecFixtures(): void |
| 65 | + { |
| 66 | + $specPath = $this->getReferrersSpecPath(); |
| 67 | + |
| 68 | + if (is_file($specPath)) { |
| 69 | + unlink($specPath); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private function getReferrersSpecPath(): string |
| 74 | + { |
| 75 | + return (new PathResolver())->getSpecFilePath('Referrers', ApiReference::DEFAULT_SPEC_VERSION); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @return array<string, mixed> |
| 80 | + */ |
| 81 | + private function getReferrersSpec(): array |
| 82 | + { |
| 83 | + $description = 'Returns example reporting data for referrer types.'; |
| 84 | + |
| 85 | + return [ |
| 86 | + 'openapi' => '3.1.0', |
| 87 | + 'info' => [ |
| 88 | + 'title' => 'Reporting API for Referrers plugin', |
| 89 | + 'version' => ApiReference::DEFAULT_SPEC_VERSION, |
| 90 | + 'description' => 'Fixture-backed OpenAPI data for ApiReference UI screenshot tests.', |
| 91 | + ], |
| 92 | + 'tags' => [ |
| 93 | + [ |
| 94 | + 'name' => 'Referrers', |
| 95 | + 'description' => 'Referrer reporting endpoints.', |
| 96 | + ], |
| 97 | + ], |
| 98 | + 'paths' => [ |
| 99 | + '/index.php?module=API&method=Referrers.getReferrerType' => [ |
| 100 | + 'get' => [ |
| 101 | + 'tags' => ['Referrers'], |
| 102 | + 'summary' => 'Get referrer types', |
| 103 | + 'description' => $description, |
| 104 | + 'parameters' => [ |
| 105 | + [ |
| 106 | + 'name' => 'idSite', |
| 107 | + 'in' => 'query', |
| 108 | + 'required' => true, |
| 109 | + 'schema' => ['type' => 'integer'], |
| 110 | + ], |
| 111 | + [ |
| 112 | + 'name' => 'period', |
| 113 | + 'in' => 'query', |
| 114 | + 'required' => true, |
| 115 | + 'schema' => ['type' => 'string'], |
| 116 | + ], |
| 117 | + [ |
| 118 | + 'name' => 'date', |
| 119 | + 'in' => 'query', |
| 120 | + 'required' => true, |
| 121 | + 'schema' => ['type' => 'string'], |
| 122 | + ], |
| 123 | + ], |
| 124 | + 'responses' => [ |
| 125 | + '200' => [ |
| 126 | + 'description' => 'Successful response', |
| 127 | + 'content' => [ |
| 128 | + 'application/json' => [ |
| 129 | + 'schema' => [ |
| 130 | + 'type' => 'array', |
| 131 | + 'items' => [ |
| 132 | + 'type' => 'object', |
| 133 | + 'properties' => [ |
| 134 | + 'label' => ['type' => 'string'], |
| 135 | + 'nb_visits' => ['type' => 'integer'], |
| 136 | + ], |
| 137 | + ], |
| 138 | + ], |
| 139 | + ], |
| 140 | + ], |
| 141 | + ], |
| 142 | + ], |
| 143 | + ], |
| 144 | + ], |
| 145 | + '/index.php?module=API&method=Referrers.getCampaigns' => [ |
| 146 | + 'post' => [ |
| 147 | + 'tags' => ['Referrers'], |
| 148 | + 'summary' => 'Get campaigns', |
| 149 | + 'description' => 'Returns example campaign reporting data.', |
| 150 | + 'requestBody' => [ |
| 151 | + 'required' => false, |
| 152 | + 'content' => [ |
| 153 | + 'application/x-www-form-urlencoded' => [ |
| 154 | + 'schema' => [ |
| 155 | + 'type' => 'object', |
| 156 | + 'properties' => [ |
| 157 | + 'segment' => ['type' => 'string'], |
| 158 | + ], |
| 159 | + ], |
| 160 | + ], |
| 161 | + ], |
| 162 | + ], |
| 163 | + 'responses' => [ |
| 164 | + '200' => [ |
| 165 | + 'description' => 'Successful response', |
| 166 | + 'content' => [ |
| 167 | + 'application/json' => [ |
| 168 | + 'schema' => [ |
| 169 | + 'type' => 'array', |
| 170 | + 'items' => [ |
| 171 | + 'type' => 'object', |
| 172 | + 'properties' => [ |
| 173 | + 'label' => ['type' => 'string'], |
| 174 | + 'revenue' => ['type' => 'number'], |
| 175 | + ], |
| 176 | + ], |
| 177 | + ], |
| 178 | + ], |
| 179 | + ], |
| 180 | + ], |
| 181 | + ], |
| 182 | + ], |
| 183 | + ], |
| 184 | + ], |
| 185 | + ]; |
| 186 | + } |
| 187 | +} |
0 commit comments