forked from cloudinary/cloudinary_php
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSearchTest.php
More file actions
180 lines (155 loc) · 6.04 KB
/
Copy pathSearchTest.php
File metadata and controls
180 lines (155 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace Cloudinary {
$base = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . "..");
require_once(join(DIRECTORY_SEPARATOR, array($base, "src", "Cloudinary.php")));
require_once(join(DIRECTORY_SEPARATOR, array($base, "src", "Uploader.php")));
require_once(join(DIRECTORY_SEPARATOR, array($base, "src", "Api.php")));
require_once(join(DIRECTORY_SEPARATOR, array($base, "src", "Search.php")));
require_once("TestHelper.php");
use PHPUnit\Framework\TestCase;
class SearchTest extends TestCase
{
use RetryTrait;
public $search;
public static function setUpBeforeClass()
{
Curl::$instance = new Curl();
\Cloudinary::reset_config();
foreach (range(1, 3) as $i) {
Uploader::upload(
TEST_IMG,
array(
"public_id" => UNIQUE_TEST_TAG . "_" . $i,
"tags" => array(TEST_TAG, UNIQUE_TEST_TAG),
"context" => "stage=value",
"eager" => array(
"transformation" => array(
"width" => 100,
"crop" => "scale",
),
),
)
);
}
sleep(3);
}
public function setUp()
{
\Cloudinary::reset_config();
if (!\Cloudinary::config_get("api_secret")) {
$this->markTestSkipped("Please setup environment for Search test to run");
}
$this->search = new Search();
}
public function tearDown()
{
Curl::$instance = new Curl();
}
public static function tearDownAfterClass()
{
Curl::$instance = new Curl();
(new Api())->delete_resources_by_tag(UNIQUE_TEST_TAG);
}
public function test_empty_query()
{
$result = $this->search->as_array();
$this->assertEquals(count($result), 0, "Should generate an empty query JSON");
}
public function test_should_add_expression_as_array()
{
$query = $this->search->expression('format:jpg')->as_array();
$this->assertEquals($query, array("expression" => 'format:jpg'));
}
public function test_should_add_sort_by_as_array()
{
$query = $this->search->sort_by('created_at', 'asc')->sort_by('updated_at', 'desc')->as_array();
$this->assertEquals(
$query,
array(
"sort_by" => array(
array('created_at' => 'asc'),
array('updated_at' => 'desc'),
),
)
);
}
public function test_should_add_max_results_as_array()
{
$query = $this->search->max_results('10')->as_array();
$this->assertEquals($query, array("max_results" => '10'));
}
public function test_should_add_next_cursor_as_array()
{
$query = $this
->search
->next_cursor('ec471a97ba510904ab57460b3ba3150ec29b6f8563eb1c10f6925ed0c6813f33cfa62ec6cf5ad96be6d6fa3ac3a76ccb')
->as_array();
$this->assertEquals(
$query,
array("next_cursor" => 'ec471a97ba510904ab57460b3ba3150ec29b6f8563eb1c10f6925ed0c6813f33cfa62ec6cf5ad96be6d6fa3ac3a76ccb')
);
}
public function test_should_add_aggregations_arguments_as_array_as_array()
{
$query = $this->search->aggregate('format')->aggregate('size_category')->as_array();
$this->assertEquals($query, array("aggregate" => ["format", "size_category"]));
}
public function test_should_add_with_field_as_array()
{
$query = $this->search->with_field('context')->with_field('tags')->as_array();
$this->assertEquals($query, array("with_field" => ["context", "tags"]));
}
/**
* @retry 3
*/
public function test_should_return_all_images_tagged()
{
$results = $this->search->expression("tags:" . UNIQUE_TEST_TAG)->execute();
$this->assertEquals(count($results['resources']), 3);
}
/**
* @retry 3
*/
public function test_should_return_resource()
{
$results = $this->search->expression("public_id:" . UNIQUE_TEST_TAG . "_1")->execute();
$this->assertEquals(count($results['resources']), 1);
}
public function test_execute_with_params()
{
Curl::mockApi($this);
$result = $this
->search
->expression("format:jpg")
->max_results(10)
->next_cursor("abcd")
->sort_by("created_at", "asc")
->sort_by("updated_at")
->aggregate("format")
->aggregate("resource_type")
->with_field("tags")
->with_field("image_metadata")
->execute();
assertEncodedRequestFields(
$this,
array(
"sort_by" => array(
array("created_at" => "asc"),
array("updated_at" => "desc"),
),
"aggregate" => array("format", "resource_type"),
"with_field" => array("tags", "image_metadata"),
"expression" => "format:jpg",
"max_results" => 10,
"next_cursor" => "abcd"
)
);
assertJson(
$this,
json_encode(array("Content-type: application/json", "Accept: application/json")),
json_encode(Curl::$instance->getopt(CURLOPT_HTTPHEADER)),
"Should use right headers for execution of advanced search api"
);
}
}
}