-1

I have a need to test output of my code before I send the output for PDF rendering.

Currently I have decided on a switch statement that does two different things depending on whether I am in production or in testing mode.

  1. Render PDF with data that goes on PDF in a web browser. Nothing is returned to caller
  2. Do not render the PDF but just return the data for test/other purposes

Just to show some code to clarify ... Here is what I have now:

$output = $this->prepOutputData();
switch ($this->strategy)
{
    //outputs binary PDF, no return statement
    case OutlineStrategy::PDF_STRATEGY:
        $pdfEngine = new PdfEngine();

        $em = DoctrineConnector::getEntityManager();
        $metaData = $em->getRepository(PdfMetadata::class)->findAll();

        $pdfEngine->arrangeDataOnPdf($metaData, $output);
        $pdfEngine->printPdfToBrowser();
        break;

    //returns data that goes onto PDF 
    //for testing or for consumption by non-PDF entity
    case OutlineStrategy::TEST_STRATEGY:
        return $output;
        break;
}

Goal

My goal is to be able to show PDF in production but NOT show PDF for testing and yet to be able to run test checks on the $output data. How can I do that?

  • Why do you have such radically different interfaces for what is conceptually the same operation? – Bart van Ingen Schenau Dec 11 '15 at 19:02
  • I am not sure what you mean.... are you saying displaying a binary PDF and returning an array is the same operation? What do you suggest instead? I can see perhaps I can just return data in both cases, but somehow, somewhere I do need to render out the PDF. How to do that is my goal for this question. How do I switch from production and testing modes without having to modify code (code should be able to run both production and test modes). – Dennis Dec 11 '15 at 19:03
  • The strategy pattern is if you have one (conceptual) operation that can be performed in several ways (for example, showing data in either a PDF or a word document). Showing a PDF and returning the raw data to the caller seem like two completely different operations in two different functions. – Bart van Ingen Schenau Dec 11 '15 at 19:14
  • If verifying the PDF in test mode is a bottleneck for you, you could choose to render the data as a simple text document in that case in test mode and as PDF in production mode. – Bart van Ingen Schenau Dec 11 '15 at 19:16
  • I could do that yes. But do I need to be using this pattern at all? For example, I can set up my tests in such as way that they test the $output before it is sent to PDF Engine. That way I may not need a Strategy at all, but instead set up my test code to call appropriate methods to retrieve $output before it is sent off to the PdfEngine. For example I use do set up a $runAsTest flag, and then if ($runAsTest) return $output; I do not need a text document. I need to be able to test my $output as correct, before it is printed, via PDF, or text, or otherwise. – Dennis Dec 11 '15 at 19:26
1

I removed strategy constructs (OutlineStrategy::PDF_STRATEGY, etc) out of my code. Then rewrote code to use services.

I will test services and not the PDF generation. aka PDF Generation will be untested, but the processes leading up to it will be tested.

Per Doc Brown wrote a function just for testing output.

public function generateOutline($input)
{
    $output = $this->getOutput($input);
    $this->pdfService->renderPdf($output);
}

public function testOutput($input)
{
    return $this->getOutput($input);
}

private function getOutput($input)
{
    $data = $this->inputFilter->getOutlineRequestData($input);
    $output = $this->processor->processInput($data);
    return $output;
}

Notice public/private modifiers

1

Separating getOutput($input) from generateOutline($input) and testing whether getOutput($input) returns the correct results is an important step.

If the generated pdf requires testing you can test:

Automatically testing the UX part of testing the generated PDF is more difficult (I don't know whether tools for these tests exist).:

  • Is the PDF readable?
  • Is the layout correct?
  • Does is follow typesetting best practices?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.