Testing PHPUnit for JSON Output

Why can't I get input and check if it is null?

My method I'm 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);
}

Error message, it returns an array of JSON objects:

PHPUnit 3.7.8 by Sebastian Bergman.

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::testGetCategoryFailed to claim that null is not null.

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

+5
source share
1 answer

Your getCategory () function has something in common with something:

echo json_encode($data);

But he does not return anything. Therefore, the $ json variable will be zero in your test.

You would probably want to return a value at the end of the function:

return json_encode($data);

, expectOutputString() expectOutputRegex() . , , :

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

, , . phpunit.

+5

All Articles