Skip to content

Commit d4e0f6e

Browse files
committed
test: add test cases;
1 parent 001c225 commit d4e0f6e

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed

tests/LocalDriverTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace RonasIT\AutoDoc\Tests;
44

5+
use Illuminate\Support\Facades\ParallelTesting;
56
use RonasIT\AutoDoc\Drivers\LocalDriver;
67
use Illuminate\Contracts\Filesystem\FileNotFoundException;
78
use RonasIT\AutoDoc\Exceptions\MissedProductionFilePathException;
@@ -35,6 +36,28 @@ public function testSaveTmpData()
3536
$this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$tmpDocumentationFilePath);
3637
}
3738

39+
public function testSaveTmpDataCheckTokenBasedPath()
40+
{
41+
$token = 'workerID';
42+
43+
ParallelTesting::resolveTokenUsing(fn () => $token);
44+
45+
$tmpDocPath = __DIR__ . "/../storage/temp_documentation_{$token}.json";
46+
47+
app(LocalDriver::class)->saveTmpData(self::$tmpData);
48+
49+
$this->assertFileExists($tmpDocPath);
50+
$this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), $tmpDocPath);
51+
}
52+
53+
public function testSaveSharedTmpData()
54+
{
55+
self::$localDriverClass->saveSharedTmpData(fn () => self::$tmpData);
56+
57+
$this->assertFileExists(self::$tmpDocumentationFilePath);
58+
$this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$tmpDocumentationFilePath);
59+
}
60+
3861
public function testGetTmpData()
3962
{
4063
file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData));
@@ -51,6 +74,22 @@ public function testGetTmpDataNoFile()
5174
$this->assertNull($result);
5275
}
5376

77+
public function testGetSharedTmpData()
78+
{
79+
file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData));
80+
81+
$result = self::$localDriverClass->getSharedTmpData();
82+
83+
$this->assertEquals(self::$tmpData, $result);
84+
}
85+
86+
public function testGetSharedTmpDataNoFile()
87+
{
88+
$result = self::$localDriverClass->getSharedTmpData();
89+
90+
$this->assertNull($result);
91+
}
92+
5493
public function testCreateClassConfigEmpty()
5594
{
5695
$this->expectException(MissedProductionFilePathException::class);

tests/StorageDriverTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ public function testSaveTmpData()
4141
$this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$tmpDocumentationFilePath);
4242
}
4343

44+
public function testSaveSharedTmpData()
45+
{
46+
self::$storageDriverClass->saveSharedTmpData(fn () => self::$tmpData);
47+
48+
$this->assertFileExists(self::$tmpDocumentationFilePath);
49+
$this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$tmpDocumentationFilePath);
50+
}
51+
4452
public function testGetTmpData()
4553
{
4654
file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData));
@@ -57,6 +65,22 @@ public function testGetTmpDataNoFile()
5765
$this->assertNull($result);
5866
}
5967

68+
public function testGetSharedTmpData()
69+
{
70+
file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData));
71+
72+
$result = self::$storageDriverClass->getSharedTmpData();
73+
74+
$this->assertEquals(self::$tmpData, $result);
75+
}
76+
77+
public function testGetSharedTmpDataNoFile()
78+
{
79+
$result = self::$storageDriverClass->getSharedTmpData();
80+
81+
$this->assertNull($result);
82+
}
83+
6084
public function testCreateClassConfigEmpty()
6185
{
6286
$this->expectException(MissedProductionFilePathException::class);

tests/SwaggerServiceTest.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
use RonasIT\AutoDoc\Exceptions\SpecValidation\InvalidSwaggerSpecException;
1717
use RonasIT\AutoDoc\Exceptions\SpecValidation\InvalidSwaggerVersionException;
1818
use RonasIT\AutoDoc\Exceptions\SpecValidation\MissingExternalRefException;
19-
use RonasIT\AutoDoc\Exceptions\SpecValidation\MissingLocalRefException;
2019
use RonasIT\AutoDoc\Exceptions\SpecValidation\MissingFieldException;
20+
use RonasIT\AutoDoc\Exceptions\SpecValidation\MissingLocalRefException;
2121
use RonasIT\AutoDoc\Exceptions\SpecValidation\MissingPathParamException;
2222
use RonasIT\AutoDoc\Exceptions\SpecValidation\MissingPathPlaceholderException;
2323
use RonasIT\AutoDoc\Exceptions\SpecValidation\MissingRefFileException;
@@ -811,4 +811,19 @@ public function testAddDataDescriptionForRouteConditionals()
811811

812812
app(SwaggerService::class)->addData($request, $response);
813813
}
814+
815+
public function testMergeTempDocumentation()
816+
{
817+
$this->mockDriverGetTmpDataAndGetSharedTmpData(
818+
tmpData: $this->getJsonFixture('tmp_data_search_users_empty_request'),
819+
sharedTmpData: $this->getJsonFixture('tmp_data_post_user_request'),
820+
);
821+
822+
$service = app(SwaggerService::class);
823+
824+
$service->mergeTempDocumentation();
825+
826+
$this->assertFileExists(storage_path('temp_documentation.json'));
827+
$this->assertFileEquals($this->generateFixturePath('tmp_data_merged.json'), storage_path('temp_documentation.json'));
828+
}
814829
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"openapi":"3.1.0","servers":[{"url":"http:\/\/localhost"}],"paths":{"\/users":{"post":{"tags":["users"],"consumes":["application\/x-www-form-urlencoded"],"produces":["application\/json"],"parameters":[],"requestBody":{"required":true,"description":"","content":{"application\/x-www-form-urlencoded":{"schema":{"$ref":"#\/components\/schemas\/usersObject"}}}},"responses":{"200":{"description":"Operation successfully done","content":{"application\/json":{"schema":{"$ref":"#\/components\/schemas\/postUsers200ResponseObject","type":"object"},"example":[{"id":1,"name":"admin","users":[{"id":1,"name":"admin"}]},{"id":2,"name":"client","users":[{"id":2,"name":"first_client"},{"id":3,"name":"second_client"}]}]}}}},"security":[{"jwt":[]}],"description":"","summary":"test","deprecated":false}},"\/api\/users":{"get":{"tags":["api"],"produces":["application\/json"],"parameters":[],"responses":{"200":{"description":"OK","content":{"application\/json":{"schema":{"$ref":"#\/components\/schemas\/getApiusers200ResponseObject","type":"object"},"example":{"current_page":1,"data":[{"id":1,"first_name":"Billy","last_name":"Coleman","email":"[email protected]","created_at":null,"updated_at":null,"role_id":1,"date_of_birth":"1986-05-20","phone":"+79535482530","position":"admin","starts_on":"2022-04-16 00:00:00","hr_id":null,"manager_id":null,"lead_id":null,"avatar_id":null,"deleted_at":null,"company_id":1}],"first_page_url":"http:\/\/localhost\/api\/users?page=1","from":1,"last_page":1,"last_page_url":"http:\/\/localhost\/api\/users?page=1","links":[{"url":null,"label":"« Previous","active":false},{"url":"http:\/\/localhost\/api\/users?page=1","label":"1","active":true},{"url":null,"label":"Next »","active":false}],"next_page_url":null,"path":"http:\/\/localhost\/api\/users","per_page":20,"prev_page_url":null,"to":1,"total":1}}}}},"security":[],"description":"","consumes":[]}}},"components":{"schemas":{"usersObject":{"type":"object","properties":{"query":{"type":"string","description":""},"user_id":{"type":"integer","description":"with_to_array_rule_string_name"},"is_email_enabled":{"type":"string","description":"test_rule_without_to_string"}},"example":{"users":[1,2],"query":null},"required":["query"]},"postUsers200ResponseObject":{"type":"array","properties":{"items":{"allOf":[{"type":"array"}]}}},"getApiusers200ResponseObject":{"type":"object","properties":{"current_page":{"type":"integer"},"data":{"type":"array"},"first_page_url":{"type":"string"},"from":{"type":"integer"},"last_page":{"type":"integer"},"last_page_url":{"type":"string"},"links":{"type":"array"},"next_page_url":{"nullable":true},"path":{"type":"string"},"per_page":{"type":"integer"},"prev_page_url":{"nullable":true},"to":{"type":"integer"},"total":{"type":"integer"}}}}},"info":{"description":"This is automatically collected documentation","version":"0.0.0","title":"Name of Your Application","termsOfService":"","contact":{"email":"[email protected]"}},"securityDefinitions":{"jwt":{"type":"apiKey","name":"authorization","in":"header"}}}

tests/support/Traits/SwaggerServiceMockTrait.php

+17
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,23 @@ protected function mockDriverGetTmpData($tmpData, $driverClass = LocalDriver::cl
6464
$this->app->instance($driverClass, $driver);
6565
}
6666

67+
protected function mockDriverGetTmpDataAndGetSharedTmpData(array $tmpData, array $sharedTmpData, string $driverClass = LocalDriver::class): void
68+
{
69+
$driver = $this->mockClass($driverClass, ['getTmpData', 'readJsonFromStream']);
70+
71+
$driver
72+
->expects($this->exactly(1))
73+
->method('getTmpData')
74+
->willReturn($tmpData);
75+
76+
$driver
77+
->expects($this->exactly(1))
78+
->method('readJsonFromStream')
79+
->willReturn($sharedTmpData);
80+
81+
$this->app->instance($driverClass, $driver);
82+
}
83+
6784
protected function mockDriverGetDocumentation($data, $driverClass = LocalDriver::class): void
6885
{
6986
$driver = $this->mockClass($driverClass, ['getDocumentation']);

0 commit comments

Comments
 (0)