Subversion Repositories eFlore/Applications.del

Rev

Rev 1372 | Rev 1378 | 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);
13
		// TODO: en-tête malgré tout ?
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);
1372 raphael 22
		return; // TODO
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
 
1372 raphael 28
	public function testMasque() {
29
		$url = $this->creerUrl('observations');
30
		$i = new Observations();
31
		$retour = $this->consulterDirectJson($i->consulter([], ['navigation.depart'=>0,'navigation.limite'=>12,'ordre'=>'asc','masque'=>'G']),
32
											$url);
33
		$expected = unserialize(file_get_contents("masque=G.data.json"));
1374 raphael 34
 
35
		self::ignoreNullValuesAndSort($expected);
36
		self::ignoreNullValuesAndSort($retour);
37
 
38
		// echo implode(',', array_keys($retour['resultats'])) . "\n" . implode(',', array_keys($expected['resultats']));die;
39
		$this->assertEquals(array_keys($expected['resultats']), array_keys($retour['resultats']), "JSON error, $url");
40
		$this->assertEquals($expected['resultats'], $retour['resultats'], "Différences dans le tableau, $url");
1372 raphael 41
	}
42
 
43
	public function testMasqueEtType() {
1374 raphael 44
		@$url = $this->creerUrl('observations');
1372 raphael 45
		$i = new Observations();
46
		$retour = $this->consulterDirectJson($i->consulter([], ['navigation.depart'=>0,'navigation.limite'=>12,'ordre'=>'asc','masque'=>'G','masque.type'=>'endiscussion']),
47
											$url);
48
		$expected = unserialize(file_get_contents("masque=G-masque.type=endiscussion.data.json"));
1374 raphael 49
 
50
		self::ignoreNullValuesAndSort($expected);
51
		self::ignoreNullValuesAndSort($retour);
52
 
53
		// echo implode(',', array_keys($retour['resultats'])) . "\n" . implode(',', array_keys($expected['resultats']));die;
54
		$this->assertEquals(array_keys($expected['resultats']), array_keys($retour['resultats']), "JSON error, $url");
55
		$this->assertEquals($expected['resultats'], $retour['resultats'], "Différences dans le tableau, $url");
1372 raphael 56
	}
57
 
58
 
59
 
1361 raphael 60
	public function hasKeys($arr, $keys, $url) {
61
		foreach($keys as $k) {
62
			$this->assertArrayHasKey($k, $arr, "attribut {$k} manquant. Voir : $url");
63
		}
64
	}
65
 
66
	public function hasKeysAndNotEmpty($arr, $keys, $url) {
67
		foreach($keys as $k) {
68
			$this->assertArrayHasKey($k, $arr, "attribut {$k} manquant. Voir : $url");
69
			$this->assertNotEmpty($arr[$k], "attribut {$k} vide. Voir : $url");
70
		}
71
	}
1374 raphael 72
 
73
	static function ignoreNullValuesAndSort(&$arr) {
74
		$arr['resultats'] = array_map('array_filter', $arr['resultats']);
75
		ksort($arr);
76
	}
1361 raphael 77
}