diff --git a/src/Services/SwaggerService.php b/src/Services/SwaggerService.php index fe0ec1b1..068997ab 100644 --- a/src/Services/SwaggerService.php +++ b/src/Services/SwaggerService.php @@ -811,7 +811,7 @@ protected function saveTempData() public function saveProductionData() { if (ParallelTesting::token()) { - $this->driver->appendProcessDataToTmpFile(function (array $sharedTmpData) { + $this->driver->appendProcessDataToTmpFile(function (?array $sharedTmpData) { $resultDocContent = (empty($sharedTmpData)) ? $this->generateEmptyData($this->config['info']['description']) : $sharedTmpData; diff --git a/tests/SwaggerServiceTest.php b/tests/SwaggerServiceTest.php index bef5d334..6225a3f4 100644 --- a/tests/SwaggerServiceTest.php +++ b/tests/SwaggerServiceTest.php @@ -15,11 +15,12 @@ use RonasIT\AutoDoc\Tests\Support\Mock\TestNotificationSetting; use RonasIT\AutoDoc\Tests\Support\Mock\TestRequest; use RonasIT\AutoDoc\Tests\Support\Traits\SwaggerServiceMockTrait; +use RonasIT\AutoDoc\Tests\Support\Traits\SwaggerServiceTestingTrait; use stdClass; class SwaggerServiceTest extends TestCase { - use SwaggerServiceMockTrait; + use SwaggerServiceMockTrait, SwaggerServiceTestingTrait; public function testConstructorInvalidConfigVersion() { @@ -843,9 +844,7 @@ public function testMergeTempDocumentation() { $this->mockParallelTestingToken(); - $tempFilePath = __DIR__ . '/../storage/temp_documentation.json'; - - file_put_contents($tempFilePath, json_encode($this->getJsonFixture('tmp_data_post_user_request'))); + $this->fillTempFile($this->getFixture('tmp_data_post_user_request.json')); $this->mockDriverGetTmpData($this->getJsonFixture('tmp_data_search_users_empty_request')); @@ -853,8 +852,20 @@ public function testMergeTempDocumentation() $service->saveProductionData(); - $this->assertFileExists($tempFilePath); - $this->assertFileEquals($this->generateFixturePath('tmp_data_merged.json'), $tempFilePath); + $this->assertTempFileEqualsFixture('tmp_data_merged'); + } + + public function testMergeToEmptyTempDocumentation() + { + $this->mockParallelTestingToken(); + + $this->fillTempFile(''); + + $this->mockDriverGetTmpData($this->getJsonFixture('tmp_data_search_users_empty_request')); + + app(SwaggerService::class)->saveProductionData(); + + $this->assertTempFileEqualsFixture('tmp_data_merged_to_empty_temp_documentation'); } public function testAddDataWhenInvokableClass() diff --git a/tests/fixtures/SwaggerServiceTest/tmp_data_merged_to_empty_temp_documentation.json b/tests/fixtures/SwaggerServiceTest/tmp_data_merged_to_empty_temp_documentation.json new file mode 100644 index 00000000..5297bc0b --- /dev/null +++ b/tests/fixtures/SwaggerServiceTest/tmp_data_merged_to_empty_temp_documentation.json @@ -0,0 +1 @@ +{"openapi":"3.1.0","servers":[{"url":"http:\/\/localhost"}],"paths":{"\/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":"billy.coleman@example.com","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":{"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":"your@mail.com"},"license":{"name":"","url":""}}} \ No newline at end of file diff --git a/tests/support/Traits/SwaggerServiceTestingTrait.php b/tests/support/Traits/SwaggerServiceTestingTrait.php new file mode 100644 index 00000000..9db8f8e0 --- /dev/null +++ b/tests/support/Traits/SwaggerServiceTestingTrait.php @@ -0,0 +1,24 @@ +prepareFixtureName($fixture); + + $path = $this->generateFixturePath($fixture); + + $this->assertFileEquals($path, getcwd() . '/storage/temp_documentation.json'); + } +}