Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit Testing for JSON Output

Why can't I get the input and test if it's not null?

My method I am testing:

/**
 * @method: getCategory
 * retrieves the categories
 * @return json category data
 */
public function getCategory() {

    $cat = $this->em->getRepository('Entities\Category')->findAll();
    $data = array();
    foreach ($cat as $res) {
        $data[] = array(
            'catId' => $res->__get('catId'),
            'category' => $res->__get('category'),
            'item' => $res->__get('item')
        );
    }
    echo json_encode($data);
}

My test:

 /**
 * @covers Category::getCategory
 * @todo   Implement testGetCategory().
 */
public function testGetCategory() {
    $json = $this->object->getCategory();
    $this->assertNotNull($json);
}

The error message, it returns a JSON array of objects:

PHPUnit 3.7.8 by Sebastian Bergmann.

F[{"catId":1,"category":"FLORALS2","item":"RED ROSES"}, {"catId":2,"category":"TENTS","item":"12X14"}, {"catId":3,"category":"FLORAL","item":"WHITE ROSES"}, {"catId":4,"category":"TENTS","item":"15X24"}, {"catId":5,"category":"CHAIRS","item":"BLACK CHAIR"}, {"catId":6,"category":"CHAIRS","item":"RED CHAIRS"}, {"catId":7,"category":"TENTS","item":"23X23"}, {"catId":8,"category":"CANDLES","item":"RED CANDLES"}, {"catId":9,"category":"CANDLES","item":"WHITE CANDLES"}, {"catId":10,"category":"CANDLES","item":"BLACK CANDLES"}, {"catId":11,"category":"CANDLES","item":"ORANGE CANDLES"}, {"catId":12,"category":"TABLE","item":"4X8 TABLE"}, {"catId":13,"category":"DRAPERYS","item":"24\" WHITE LINEN"}, {"catId":14,"category":"LINEN","item":"WHITE CURTAINS"}, {"catId":17,"category":"DRAPERY","item":"SILK TABLE CLOTH"}, {"catId":18,"category":"FLORAL","item":"ORANGE DAISIES"}]..

Time: 0 seconds, Memory: 10.25Mb

There was 1 failure:

1) CategoryTest::testGetCategory Failed asserting that null is not null.

/var/www/praiseDB/tests/controller/CategoryTest.php:42

like image 200
robasc Avatar asked May 01 '13 21:05

robasc


People also ask

What is PHPUnit testing?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit design for unit testing systems that began with SUnit and became popular with JUnit. Even a small software development project usually takes hours of hard work.

What is PHPUnit used for?

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks.

What are PHPUnit fixtures?

One of the most time-consuming parts of writing tests is writing the code to set the world up in a known state and then return it to its original state when the test is complete. This known state is called the fixture of the test.


1 Answers

Your getCategory() function echoes something out:

echo json_encode($data);

But it doesn't return anything. Therefore the $json variable will be null in your test.

You probably meant to return the value at the end of the function instead:

return json_encode($data);

To test output, you would need to use the expectOutputString() or expectOutputRegex() methods in your test. To test for non-empty output, I believe the following should do:

/**
 * @covers Category::getCategory
 * @todo   Implement testGetCategory().
 */
public function testGetCategory() {
    $this->expectOutputRegex('/./');
    $this->object->getCategory();
}

See the phpunit documentation for details on how to assert output.

like image 117
Thierry Avatar answered Sep 30 '22 18:09

Thierry