Subversion Repositories eFlore/Applications.del

Rev

Rev 1374 | Rev 1382 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1361 raphael 1
<?php
2
require_once __DIR__ . '/../ServiceDelPhpUnit.php';
3
error_reporting(E_ALL);
4
 
5
class ObservationsTest extends ServiceDelPhpUnit {
6
	//static function creerUrl($service, $ressources, $parametres = NULL) {
7
 
8
	public function testNonExistant() {
9
		$url = $this->creerUrl('observations');
10
		$i = new Observations();
11
		$retour = $this->consulterDirectJson($i->consulter([0], []),
12
											$url);
1378 raphael 13
		// doit retourner une 404
1361 raphael 14
		$this->assertEmpty($retour, "Le json doit retourner un tableau vide. Voir : $url");
15
	}
16
 
17
	public function testExistant() {
18
		$url = $this->creerUrl('observations');
19
		$i = new Observations();
20
		$retour = $this->consulterDirectJson($i->consulter([1043942], []),
21
											$url);
1378 raphael 22
 
1361 raphael 23
		$this->hasKeysAndNotEmpty($retour, ['auteur.id', 'auteur.nom', 'date_transmission', 'observateur', 'id_observation'], $url);
24
		$this->hasKeys($retour, ['auteur.nom'], $url);
25
		$this->assertArrayHasKey('auteur.id', $retour, "attribut auteur.id manquant. Voir : $url");
26
	}
27
 
1378 raphael 28
	public function testSansImage() {
29
		$url = $this->creerUrl('observations');
30
		$i = new Observations();
31
		$retour = $this->consulterDirectJson($i->consulter([14203], []),
32
											$url);
33
		// doit retourner une 404
34
		$this->assertEmpty($retour, "Observation sans image devrait ne rien retourner. Voir : $url");
35
	}
36
 
37
	public function testRechercheNoObs() {
38
		$url = $this->creerUrl('observations');
39
		$i = new Observations();
40
		$retour = $this->consulterDirectJson($i->consulter([], ['masque.date'=>-1e11]), // 10^11 secondes avant 1970
41
											$url);
42
		// doit retourner une 404
43
		$this->assertEquals(0, $retour['entete']['total'], "Incongruité sur ['entete'][0]. Voir : $url");
44
		$this->assertEmpty($retour['resultats'], "Absence d'observation devrait retourner un result-set vide. Voir : $url");
45
	}
46
 
47
	/* guidelines pour des tests pérennes:
48
	 * - Utiliser ordre=asc pour prendre les enregistrement les plus anciens, moins susceptibles de changer
49
	 * - Restreindre par date (évite les date_transmission = NULL, qui, après des GROUP BY affectent le result-set
50
	 * - Utiliser moins de "$limite" résultats à cause de la sélection (arbitraire[GROUP-BY])) des obs ayant la même date */
1372 raphael 51
	public function testMasque() {
52
		$url = $this->creerUrl('observations');
53
		$i = new Observations();
1378 raphael 54
		$retour = $this->consulterDirectJson($i->consulter([], ['navigation.depart'=>0,'navigation.limite'=>12,'ordre'=>'asc','masque'=>'Grand','masque.date'=>2009]),
1372 raphael 55
											$url);
1378 raphael 56
		// from
57
		// "http://www.tela-botanica.org/eflore/del/services/0.1/observations?navigation.depart=0&navigation.limite=12&masque=Grand&masque.date=2009&ordre=asc"
58
		$expected = json_decode(file_get_contents("masque=G-date=2009.data.json"), true);
1374 raphael 59
 
60
		self::ignoreNullValuesAndSort($expected);
61
		self::ignoreNullValuesAndSort($retour);
62
 
63
		// echo implode(',', array_keys($retour['resultats'])) . "\n" . implode(',', array_keys($expected['resultats']));die;
1378 raphael 64
		$this->clefsIdentiques($expected, $retour, $url);
1374 raphael 65
		$this->assertEquals($expected['resultats'], $retour['resultats'], "Différences dans le tableau, $url");
1372 raphael 66
	}
67
 
1378 raphael 68
	public function testType() {
69
		@$url = $this->creerUrl('observations');
70
		$i = new Observations();
71
		$retour = $this->consulterDirectJson($i->consulter([], ['ordre'=>'asc','masque.type'=>'endiscussion']),
72
											$url);
73
		if(Config::get('nb_commentaires_discussion') != 1) {
74
			printf("can't do test: Config::get('nb_commentaires_discussion') == %d <> 1\n", Config::get('nb_commentaires_discussion'));
75
			return;
76
		}
77
 
78
		// from
79
		// "http://www.tela-botanica.org/eflore/del/services/0.1/observations?masque.type=endiscussion&ordre=asc"
80
		$expected = json_decode(file_get_contents("masque.type=endiscussion.data.json"), true);
81
 
82
		self::ignoreNullValuesAndSort($expected);
83
		self::ignoreNullValuesAndSort($retour);
84
 
85
		// echo implode(',', array_keys($retour['resultats'])) . "\n" . implode(',', array_keys($expected['resultats']));die;
86
		$this->clefsIdentiques($expected, $retour, $url);
87
		$this->assertEquals($expected['resultats'], $retour['resultats'], "Différences dans le tableau, $url");
88
	}
89
 
90
 
1372 raphael 91
	public function testMasqueEtType() {
1374 raphael 92
		@$url = $this->creerUrl('observations');
1372 raphael 93
		$i = new Observations();
94
		$retour = $this->consulterDirectJson($i->consulter([], ['navigation.depart'=>0,'navigation.limite'=>12,'ordre'=>'asc','masque'=>'G','masque.type'=>'endiscussion']),
95
											$url);
1374 raphael 96
 
1378 raphael 97
		$expected = json_decode(file_get_contents("masque=G-masque.type=endiscussion.data.json"), true);
98
 
1374 raphael 99
		self::ignoreNullValuesAndSort($expected);
100
		self::ignoreNullValuesAndSort($retour);
101
 
102
		// echo implode(',', array_keys($retour['resultats'])) . "\n" . implode(',', array_keys($expected['resultats']));die;
1378 raphael 103
		$this->clefsIdentiques($expected, $retour, $url);
1374 raphael 104
		$this->assertEquals($expected['resultats'], $retour['resultats'], "Différences dans le tableau, $url");
1372 raphael 105
	}
106
 
107
 
108
 
1361 raphael 109
	public function hasKeys($arr, $keys, $url) {
110
		foreach($keys as $k) {
111
			$this->assertArrayHasKey($k, $arr, "attribut {$k} manquant. Voir : $url");
112
		}
113
	}
114
 
115
	public function hasKeysAndNotEmpty($arr, $keys, $url) {
116
		foreach($keys as $k) {
117
			$this->assertArrayHasKey($k, $arr, "attribut {$k} manquant. Voir : $url");
118
			$this->assertNotEmpty($arr[$k], "attribut {$k} vide. Voir : $url");
119
		}
120
	}
1374 raphael 121
 
122
	static function ignoreNullValuesAndSort(&$arr) {
123
		$arr['resultats'] = array_map('array_filter', $arr['resultats']);
1378 raphael 124
		ksort($arr['resultats']);
1374 raphael 125
	}
1378 raphael 126
 
127
	public function clefsIdentiques($e, $r, $url) {
128
		$e = array_map(function($a) { return intval(trim($a, '"')); }, array_keys($e['resultats']));
129
		sort($e, SORT_NUMERIC);
130
		$r = array_map(function($a) { return intval(trim($a, '"')); }, array_keys($r['resultats']));
131
		sort($r, SORT_NUMERIC);
132
		$this->assertEquals($e, $r, "Différences dans les clefs du tableau, $url");
133
	}
134
 
1361 raphael 135
}