Skip to content

Commit

Permalink
Add support for custom fake data
Browse files Browse the repository at this point in the history
  • Loading branch information
leganz committed Dec 13, 2020
1 parent 559de4a commit 3521ed3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
23 changes: 15 additions & 8 deletions src/Classes/PdfPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class PdfPrinter implements PdfPrinterInterface
public String $authToken;

public bool $testmode = false;
public array $fakeData = [];

public function __construct(PdfPrinterSetting $settings = null, Client $client = null)
{
Expand All @@ -48,10 +49,21 @@ public function authBearer(String $token): self
return $this;
}

public function useTestmode():self
public function useTestmode(array $fakeData = []):self
{
$this->testmode = true;

if (! empty($fakeData)) {
$this->fakeData = $fakeData;
} else {
$this->fakeData = [
'statusCode' => 200,
'uploaded' => false,
'downloadUrl' => 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf',
'filename' => 'dummy',
];
}

return $this;
}

Expand All @@ -66,7 +78,7 @@ public function useTestmode():self
public function create(String $url, PdfPrinterOption $options = null, callable $callback = null): self
{
if ($options !== null && $options->testmode === true) {
$this->useTestmode();
$this->useTestmode($options->fakeData);
}

try {
Expand All @@ -85,12 +97,7 @@ public function create(String $url, PdfPrinterOption $options = null, callable $
}

if ($this->testmode === true) {
$response = new GuzzleResponse(200, [], json_encode([
'statusCode' => 200,
'uploaded' => false,
'downloadUrl' => 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf',
'filename' => 'dummy',
]));
$response = new GuzzleResponse(200, [], json_encode($this->fakeData));
} else {
$response = $this->client->request('POST', $this->settings->url('api/browse'), [
'headers' => $headers,
Expand Down
7 changes: 6 additions & 1 deletion src/Classes/PdfPrinterOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class PdfPrinterOption
public ?String $token;
public bool $autodelete = false;
public bool $testmode = false;
public array $fakeData = [];

public function __construct(String $filename, String $postBackUrl = null, array $postBackBody = [], String $token = null, bool $autodelete = false)
{
Expand All @@ -20,10 +21,14 @@ public function __construct(String $filename, String $postBackUrl = null, array
$this->autodelete = $autodelete;
}

public function useTestmode()
public function useTestmode(array $fakeData = [])
{
$this->testmode = true;

if (! empty($fakeData)) {
$this->fakeData = $fakeData;
}

return $this;
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/PdfPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,23 @@ public function testIfPdfPrinterSupportTestmode():void
});
}

public function testIfPdfPrinterSupportTestmodeWithCustomFakeData():void
{
$response = new PdfPrinterMockResponse(200, []);
$pdfPrinter = $this->createApiMock([$response]);

$result = $pdfPrinter->useTestmode([
'statusCode' => 200,
'uploaded' => false,
'downloadUrl' => 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf',
'filename' => 'monkey',
])->create('http://127.0.0.1:8000', null, function ($instance, $result, $options, $successful) {
$this->assertEquals(200, $result->statusCode);
$this->assertTrue($successful);
$this->assertEquals('monkey.pdf', $result->filename);
});
}

/**
* Test if the print request can fail.
*/
Expand Down

0 comments on commit 3521ed3

Please sign in to comment.