Skip to content

Commit

Permalink
improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
d8vjork committed Jan 5, 2024
1 parent 556cf6c commit 91f37d5
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ protected function registerAboutCommandInfo(): void
{
// The about command is only available in Laravel 9 and up so we need to check if it's available to us
if (! class_exists(AboutCommand::class)) {
// @codeCoverageIgnoreStart
return;
// @codeCoverageIgnoreEnd
}

AboutCommand::add('Integrations', [
Expand Down
59 changes: 59 additions & 0 deletions tests/RenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,65 @@ public function testGenerateRender()
$this->assertEquals('ai9PAfUoDV1mfbjQsGzDtjCJMTNk2N7FDFb8fTwwgnYqFdfgvcWryniPQnkUTke4', $response->getRenderId());
}

public function testGenerateRenderConvertToAcceptsString()
{
$mockClient = new MockClient([
Render\GenerateDocumentUsingTemplate::class => MockResponse::make([
'success' => true,
'data' => [
'renderId' => 'ai9PAfUoDV1mfbjQsGzDtjCJMTNk2N7FDFb8fTwwgnYqFdfgvcWryniPQnkUTke4',
],
]),
]);

$this->carbone->withMockClient($mockClient);

$data = new \OpenSoutheners\CarboneSdk\Data\Render(
data: [
'foo' => 'bar',
'hello' => 'world',
],
convertTo: 'pdf'
);

$response = $this->carbone->render()->generate(
'qjaRALD5xIdNdwaXrbLZbuWuZfrGPPT1kdoh82mULevAv904gWNtba9kkAwU5Uef',
$data
);

$mockClient->assertSent(Render\GenerateDocumentUsingTemplate::class);

$this->assertTrue($response->ok());
$this->assertEquals('ai9PAfUoDV1mfbjQsGzDtjCJMTNk2N7FDFb8fTwwgnYqFdfgvcWryniPQnkUTke4', $response->getRenderId());
}

public function testGenerateRenderGetRenderIdAsNullWhenFailure()
{
$mockClient = new MockClient([
Render\GenerateDocumentUsingTemplate::class => MockResponse::make([
'success' => false,
'error' => 'Invalid templateId',
], 400),
]);

$this->carbone->withMockClient($mockClient);

$data = new \OpenSoutheners\CarboneSdk\Data\Render(
data: [
'foo' => 'bar',
'hello' => 'world',
],
convertTo: RenderFormat::Pdf
);

$response = $this->carbone->render()->generate('dunno', $data);

$mockClient->assertSent(Render\GenerateDocumentUsingTemplate::class);

$this->assertTrue($response->failed());
$this->assertNull($response->getRenderId());
}

public function testRetrieveRender()
{
$content = file_get_contents(__DIR__.'/fixtures/my_template.xlsx');
Expand Down
39 changes: 39 additions & 0 deletions tests/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ public function testUploadTemplate()
$this->assertEquals('qjaRALD5xIdNdwaXrbLZbuWuZfrGPPT1kdoh82mULevAv904gWNtba9kkAwU5Uef', $response->getTemplateId());
}

public function testUploadTemplateThatFails()
{
$mockClient = new MockClient([
Template\UploadTemplate::class => MockResponse::make([
'success' => false,
'error' => 'Cannot store template',
], 400),
]);

$this->carbone->withMockClient($mockClient);

$templateContent = '';

$response = $this->carbone->template()->upload($templateContent);

$mockClient->assertSent(Template\UploadTemplate::class);

$this->assertTrue($response->failed());
$this->assertNull($response->getTemplateId());
}

public function testBase64UploadTemplate()
{
$mockClient = new MockClient([
Expand Down Expand Up @@ -83,6 +104,24 @@ public function testDeleteTemplate()
$this->assertEquals(true, $response->json('success'));
}

public function testCheckTemplateExist()
{
$mockClient = new MockClient([
Template\CheckTemplateExists::class => MockResponse::make(),
]);

$this->carbone->withMockClient($mockClient);

$templateId = 'qjaRALD5xIdNdwaXrbLZbuWuZfrGPPT1kdoh82mULevAv904gWNtba9kkAwU5Uef';

$response = $this->carbone->template()->exists($templateId);

$mockClient->assertSent(Template\CheckTemplateExists::class);

$this->assertTrue($response->ok());
$this->assertEmpty($response->json());
}

public function testDownloadTemplate()
{
$content = file_get_contents(__DIR__.'/fixtures/my_template.xlsx');
Expand Down

0 comments on commit 91f37d5

Please sign in to comment.