6
6
7
7
class SearchIndexTest extends AlgoliaIntegrationTestCase
8
8
{
9
+ protected $ index ;
10
+
9
11
protected function setUp ()
10
12
{
11
13
parent ::setUp ();
12
14
13
15
if (!isset (static ::$ indexes ['main ' ])) {
14
16
static ::$ indexes ['main ' ] = self ::safeName ('general-index-mgmt ' );
15
17
}
18
+
19
+ $ this ->index = static ::getClient ()->initIndex (static ::$ indexes ['main ' ]);
16
20
}
17
21
18
22
public function testIndexDoesNotExist ()
19
23
{
20
- $ index = static ::getClient ()->initIndex (static ::$ indexes ['main ' ]);
21
-
22
- self ::assertFalse ($ index ->exists ());
24
+ self ::assertFalse ($ this ->index ->exists ());
23
25
}
24
26
25
27
public function testIndexExists ()
26
28
{
27
- $ index = static ::getClient ()->initIndex (static ::$ indexes ['main ' ]);
28
-
29
- $ index
29
+ $ this ->index
30
30
->saveObject (
31
31
array (
32
32
'firstname ' => 'Jimmie ' ,
@@ -35,27 +35,26 @@ public function testIndexExists()
35
35
)
36
36
->wait ();
37
37
38
- self ::assertTrue ($ index ->exists ());
38
+ self ::assertTrue ($ this -> index ->exists ());
39
39
}
40
40
41
41
public function testFindObject ()
42
42
{
43
- $ index = static ::getClient ()->initIndex (static ::$ indexes ['main ' ]);
44
- $ index ->clearObjects ();
45
- $ index ->saveObjects ($ this ->companies );
43
+ $ this ->index ->clearObjects ();
44
+ $ this ->index ->saveObjects ($ this ->companies );
46
45
47
- $ res = $ index ->search ('Algolia ' );
46
+ $ res = $ this -> index ->search ('Algolia ' );
48
47
$ this ->assertEquals (SearchIndex::getObjectPosition ($ res , 'nicolas-dessaigne ' ), 0 );
49
48
$ this ->assertEquals (SearchIndex::getObjectPosition ($ res , 'julien-lemoine ' ), 1 );
50
49
$ this ->assertEquals (SearchIndex::getObjectPosition ($ res , '' ), -1 );
51
50
52
51
try {
53
- $ index ->findObject (function () { return false ; });
52
+ $ this -> index ->findObject (function () { return false ; });
54
53
} catch (\Exception $ e ) {
55
54
$ this ->assertInstanceOf ('Algolia\AlgoliaSearch\Exceptions\ObjectNotFoundException ' , $ e );
56
55
}
57
56
58
- $ found = $ index ->findObject (function () { return true ; });
57
+ $ found = $ this -> index ->findObject (function () { return true ; });
59
58
$ this ->assertEquals ($ found ['position ' ], 0 );
60
59
$ this ->assertEquals ($ found ['page ' ], 0 );
61
60
@@ -64,19 +63,28 @@ public function testFindObject()
64
63
};
65
64
66
65
try {
67
- $ index ->findObject ($ callback , array ('query ' => 'algolia ' ));
66
+ $ this -> index ->findObject ($ callback , array ('query ' => 'algolia ' ));
68
67
} catch (\Exception $ e ) {
69
68
$ this ->assertInstanceOf ('Algolia\AlgoliaSearch\Exceptions\ObjectNotFoundException ' , $ e );
70
69
}
71
70
72
71
try {
73
- $ index ->findObject ($ callback , array ('query ' => '' , 'paginate ' => false , 'hitsPerPage ' => 5 ));
72
+ $ this -> index ->findObject ($ callback , array ('query ' => '' , 'paginate ' => false , 'hitsPerPage ' => 5 ));
74
73
} catch (\Exception $ e ) {
75
74
$ this ->assertInstanceOf ('Algolia\AlgoliaSearch\Exceptions\ObjectNotFoundException ' , $ e );
76
75
}
77
76
78
- $ obj = $ index ->findObject ($ callback , array ('query ' => '' , 'paginate ' => true , 'hitsPerPage ' => 5 ));
77
+ $ obj = $ this -> index ->findObject ($ callback , array ('query ' => '' , 'paginate ' => true , 'hitsPerPage ' => 5 ));
79
78
$ this ->assertEquals ($ obj ['position ' ], 0 );
80
79
$ this ->assertEquals ($ obj ['page ' ], 2 );
81
80
}
81
+
82
+ public function testSaveObjectsFails ()
83
+ {
84
+ try {
85
+ $ this ->index ->saveObjects ($ this ->companies [0 ]);
86
+ } catch (\Exception $ e ) {
87
+ $ this ->assertInstanceOf ('Algolia\AlgoliaSearch\Exceptions\ArrayOfObjectsExpected ' , $ e );
88
+ }
89
+ }
82
90
}
0 commit comments