Subversion Repositories eFlore/Applications.del

Rev

Rev 1372 | Rev 1378 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
require_once __DIR__ . '/../ServiceDelPhpUnit.php';
error_reporting(E_ALL);

class ObservationsTest extends ServiceDelPhpUnit {
        //static function creerUrl($service, $ressources, $parametres = NULL) {
        
        public function testNonExistant() {
                $url = $this->creerUrl('observations');
                $i = new Observations();
                $retour = $this->consulterDirectJson($i->consulter([0], []),
                                                                                        $url);
                // TODO: en-tête malgré tout ?
                $this->assertEmpty($retour, "Le json doit retourner un tableau vide. Voir : $url");
        }

        public function testExistant() {
                $url = $this->creerUrl('observations');
                $i = new Observations();
                $retour = $this->consulterDirectJson($i->consulter([1043942], []),
                                                                                        $url);
                return; // TODO
                $this->hasKeysAndNotEmpty($retour, ['auteur.id', 'auteur.nom', 'date_transmission', 'observateur', 'id_observation'], $url);
                $this->hasKeys($retour, ['auteur.nom'], $url);
                $this->assertArrayHasKey('auteur.id', $retour, "attribut auteur.id manquant. Voir : $url");
        }

        public function testMasque() {
                $url = $this->creerUrl('observations');
                $i = new Observations();
                $retour = $this->consulterDirectJson($i->consulter([], ['navigation.depart'=>0,'navigation.limite'=>12,'ordre'=>'asc','masque'=>'G']),
                                                                                        $url);
                $expected = unserialize(file_get_contents("masque=G.data.json"));

                self::ignoreNullValuesAndSort($expected);
                self::ignoreNullValuesAndSort($retour);

                // echo implode(',', array_keys($retour['resultats'])) . "\n" . implode(',', array_keys($expected['resultats']));die;
                $this->assertEquals(array_keys($expected['resultats']), array_keys($retour['resultats']), "JSON error, $url");
                $this->assertEquals($expected['resultats'], $retour['resultats'], "Différences dans le tableau, $url");
        }

        public function testMasqueEtType() {
                @$url = $this->creerUrl('observations');
                $i = new Observations();
                $retour = $this->consulterDirectJson($i->consulter([], ['navigation.depart'=>0,'navigation.limite'=>12,'ordre'=>'asc','masque'=>'G','masque.type'=>'endiscussion']),
                                                                                        $url);
                $expected = unserialize(file_get_contents("masque=G-masque.type=endiscussion.data.json"));

                self::ignoreNullValuesAndSort($expected);
                self::ignoreNullValuesAndSort($retour);

                // echo implode(',', array_keys($retour['resultats'])) . "\n" . implode(',', array_keys($expected['resultats']));die;
                $this->assertEquals(array_keys($expected['resultats']), array_keys($retour['resultats']), "JSON error, $url");
                $this->assertEquals($expected['resultats'], $retour['resultats'], "Différences dans le tableau, $url");
        }



        public function hasKeys($arr, $keys, $url) {
                foreach($keys as $k) {
                        $this->assertArrayHasKey($k, $arr, "attribut {$k} manquant. Voir : $url");
                }
        }

        public function hasKeysAndNotEmpty($arr, $keys, $url) {
                foreach($keys as $k) {
                        $this->assertArrayHasKey($k, $arr, "attribut {$k} manquant. Voir : $url");
                        $this->assertNotEmpty($arr[$k], "attribut {$k} vide. Voir : $url");
                }
        }

        static function ignoreNullValuesAndSort(&$arr) {
                $arr['resultats'] = array_map('array_filter', $arr['resultats']);
                ksort($arr);
        }
}