Perché non riesco ad ottenere l'input e testare se non è nullo?Test PHPUnit per uscita JSON
Il mio metodo sto testando:
/**
* @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);
}
La mia prova:
/**
* @covers Category::getCategory
* @todo Implement testGetCategory().
*/
public function testGetCategory() {
$json = $this->object->getCategory();
$this->assertNotNull($json);
}
Il messaggio di errore, restituisce un array di oggetti JSON di:
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"}]..
Tempo: 0 secondi, Memoria: 10.25Mb
c'era 1 fallimento:
1)
CategoryTest::testGetCategory
Failed affermando che null non è nullo. Funzione
/var/www/praiseDB/tests/controller/CategoryTest.php:42
L'assertXXXX è sostanzialmente l'opposto di quello che stai pensando. Ad esempio, l'assertNotNull lancia l'asserzione quando il testo è Null. Il tuo $ json è NULL poiché era echo, non restituito. –