Skip to content

Commit fd3d2d5

Browse files
author
Amir Tocker
committed
Update tests to use TestHelper
1 parent c3aa150 commit fd3d2d5

File tree

5 files changed

+69
-76
lines changed

5 files changed

+69
-76
lines changed

tests/ApiTest.php

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99

1010
use Cloudinary;
1111
use Exception;
12-
use PHPUnit\Framework\TestCase;
1312

14-
class ApiTest extends TestCase {
13+
class ApiTest extends \PHPUnit\Framework\TestCase {
1514
static $api_test_tag = "cloudinary_php_test";
1615
static $initialized = FALSE;
1716
static $timestamp_tag;
@@ -25,35 +24,32 @@ class ApiTest extends TestCase {
2524
static $api_test_upload_preset_2;
2625
static $api_test_upload_preset_3;
2726

28-
const LOGO_PNG = "tests/logo.png";
29-
const LOGO_SIZE = 3381;
30-
const RAW_FILE = "tests/docx.docx";
3127
static $api_test_transformation = "api_test_transformation";
3228
static $api_test_transformation_2 = "api_test_transformation2";
3329
static $api_test_transformation_3 = "api_test_transformation3";
3430
const URL_QUERY_REGEX = "\??(\w+=\w*&?)*";
35-
/** @var \Cloudinary\Api $api */
31+
32+
/** @var \Cloudinary\Api $api */
3633
private $api;
3734

3835
public static function setUpBeforeClass()
3936
{
4037
Curl::$instance = new Curl();
4138
if (Cloudinary::config_get("api_secret")) {
42-
$random_suffix = "_" . rand(11111, 99999);
43-
self::$api_test_tag = self::$api_test_tag . "_". $random_suffix;
44-
self::$api_test_upload_preset = "api_test_upload_preset" . $random_suffix;
45-
self::$api_test_upload_preset_2 = "api_test_upload_preset2" . $random_suffix;
46-
self::$api_test_upload_preset_3 = "api_test_upload_preset3" . $random_suffix;
47-
48-
self::$api_test = "api_test" . $random_suffix;
49-
self::$api_test_2 = "api_test2" . $random_suffix;
50-
self::$api_test_3 = "api_test,3" . $random_suffix;
51-
self::$api_test_4 = "api_test4" . $random_suffix;
52-
self::$api_test_5 = "api_test5" . $random_suffix;
53-
54-
self::$api_test_transformation = "api_test_transformation" . $random_suffix;
55-
self::$api_test_transformation_2 = "api_test_transformation2" . $random_suffix;
56-
self::$api_test_transformation_3 = "api_test_transformation3" . $random_suffix;
39+
self::$api_test_tag = self::$api_test_tag . "_". SUFFIX;
40+
self::$api_test_upload_preset = "api_test_upload_preset" . SUFFIX;
41+
self::$api_test_upload_preset_2 = "api_test_upload_preset2" . SUFFIX;
42+
self::$api_test_upload_preset_3 = "api_test_upload_preset3" . SUFFIX;
43+
44+
self::$api_test = "api_test" . SUFFIX;
45+
self::$api_test_2 = "api_test2" . SUFFIX;
46+
self::$api_test_3 = "api_test,3" . SUFFIX;
47+
self::$api_test_4 = "api_test4" . SUFFIX;
48+
self::$api_test_5 = "api_test5" . SUFFIX;
49+
50+
self::$api_test_transformation = "api_test_transformation" . SUFFIX;
51+
self::$api_test_transformation_2 = "api_test_transformation2" . SUFFIX;
52+
self::$api_test_transformation_3 = "api_test_transformation3" . SUFFIX;
5753

5854
self::$timestamp_tag = self::$api_test_tag . "_" . time();
5955
self::upload_sample_resources();
@@ -144,9 +140,9 @@ protected static function delete_presets($api) {
144140
*/
145141
protected static function upload_sample_resources()
146142
{
147-
Uploader::upload(self::LOGO_PNG,
143+
Uploader::upload(TEST_IMG,
148144
array("public_id" => self::$api_test, "tags" => array(self::$api_test_tag, self::$timestamp_tag), "context" => "key=value", "eager" => array("transformation" => array("width" => 100, "crop" => "scale"))));
149-
Uploader::upload(self::LOGO_PNG,
145+
Uploader::upload(TEST_IMG,
150146
array("public_id" => self::$api_test_2, "tags" => array(self::$api_test_tag, self::$timestamp_tag), "context" => "key=value", "eager" => array("transformation" => array("width" => 100, "crop" => "scale"))));
151147
}
152148

@@ -252,7 +248,7 @@ function test07_resource_metadata() {
252248
$resource = $this->api->resource(self::$api_test);
253249
$this->assertNotEquals($resource, NULL);
254250
$this->assertEquals($resource["public_id"], self::$api_test);
255-
$this->assertEquals($resource["bytes"], self::LOGO_SIZE);
251+
$this->assertEquals($resource["bytes"], LOGO_SIZE);
256252
$this->assertEquals(count($resource["derived"]), 1);
257253
}
258254

@@ -427,7 +423,7 @@ function test18_usage() {
427423
function test19_delete_derived() {
428424
// should allow deleting all resources
429425
$this->markTestSkipped("Not enabled by default - remove this line to test");
430-
Uploader::upload(self::LOGO_PNG, array("public_id"=> self::$api_test_5, "eager"=>array("transformation"=>array("width"=> 101,"crop" => "scale"))));
426+
Uploader::upload(TEST_IMG, array("public_id"=> self::$api_test_5, "eager"=>array("transformation"=>array("width"=> 101,"crop" => "scale"))));
431427
$resource = $this->api->resource(self::$api_test_5);
432428
$this->assertNotEquals($resource, NULL);
433429
$this->assertEquals(count($resource["derived"]), 1);
@@ -440,7 +436,7 @@ function test19_delete_derived() {
440436

441437
function test20_manual_moderation() {
442438
// should support setting manual moderation status
443-
$resource = Uploader::upload(self::LOGO_PNG, array("moderation"=>"manual"));
439+
$resource = Uploader::upload(TEST_IMG, array("moderation"=>"manual"));
444440
$this->assertEquals($resource["moderation"][0]["status"], "pending");
445441
$this->assertEquals($resource["moderation"][0]["kind"], "manual");
446442

@@ -455,7 +451,7 @@ function test20_manual_moderation() {
455451
*/
456452
function test22_raw_conversion() {
457453
// should support requesting raw_convert
458-
$resource = Uploader::upload(self::RAW_FILE, array("resource_type"=>"raw"));
454+
$resource = Uploader::upload(RAW_FILE, array("resource_type"=>"raw"));
459455
$this->api->update($resource["public_id"], array("raw_convert" => "illegal", "resource_type"=>"raw"));
460456
}
461457

@@ -566,10 +562,10 @@ function test31_update_upload_presets() {
566562

567563
function test32_folder_listing() {
568564
$this->markTestSkipped("For this test to work, 'Auto-create folders' should be enabled in the Upload Settings, and the account should be empty of folders. Comment out this line if you really want to test it.");
569-
Uploader::upload(self::LOGO_PNG, array("public_id" => "test_folder1/item"));
570-
Uploader::upload(self::LOGO_PNG, array("public_id" => "test_folder2/item"));
571-
Uploader::upload(self::LOGO_PNG, array("public_id" => "test_folder1/test_subfolder1/item"));
572-
Uploader::upload(self::LOGO_PNG, array("public_id" => "test_folder1/test_subfolder2/item"));
565+
Uploader::upload(TEST_IMG, array("public_id" => "test_folder1/item"));
566+
Uploader::upload(TEST_IMG, array("public_id" => "test_folder2/item"));
567+
Uploader::upload(TEST_IMG, array("public_id" => "test_folder1/test_subfolder1/item"));
568+
Uploader::upload(TEST_IMG, array("public_id" => "test_folder1/test_subfolder2/item"));
573569
$result = $this->api->root_folders();
574570
$this->assertContains(array("name" => "test_folder1", "path" => "test_folder1"), $result["folders"]);
575571
$this->assertContains(array("name" => "test_folder2", "path" => "test_folder2"), $result["folders"]);

tests/CloudinaryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
$base = realpath(dirname(__FILE__).DIRECTORY_SEPARATOR.'..');
33
use PHPUnit\Framework\TestCase;
44
require_once(join(DIRECTORY_SEPARATOR, array($base, 'src', 'Cloudinary.php')));
5-
class CloudinaryTest extends TestCase {
5+
class CloudinaryTest extends \PHPUnit\Framework\TestCase {
66

77
const DEFAULT_ROOT_PATH = 'http://res.cloudinary.com/test123/';
88
const DEFAULT_UPLOAD_PATH = 'http://res.cloudinary.com/test123/image/upload/';

tests/SearchTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class SearchTest extends PHPUnit_Framework_TestCase {
1414

1515
public static function setUpBeforeClass() {
1616
Curl::$instance = new Curl();
17+
\Cloudinary::reset_config();
1718
foreach ( range( 1, 3 ) as $i ) {
1819
Uploader::upload( TEST_IMG,
1920
array(
@@ -111,7 +112,6 @@ public function test_should_return_all_images_tagged() {
111112
public function test_should_return_resource() {
112113

113114
$results = $this->search->expression( "public_id:" . UNIQUE_TEST_TAG . "_1" )->execute();
114-
print_r( $results );
115115
$this->assertEquals( count( $results['resources'] ), 1 );
116116
}
117117

tests/TestHelper.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
define('UNIQUE_TEST_TAG', TEST_TAG . "_" . SUFFIX);
1111

1212
// For compatibility with the new versions of phpunit
13-
if (!class_exists('\PHPUnit_Framework_TestCase') &&
14-
class_exists('\PHPUnit\Framework\TestCase')) {
15-
class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase');
16-
}
13+
if ( ! class_exists( '\PHPUnit\Framework\TestCase' ) &&
14+
class_exists( '\PHPUnit_Framework_TestCase' )
15+
) {
16+
/** @noinspection PhpUndefinedClassInspection */
17+
class_alias( '\PHPUnit_Framework_TestCase', '\PHPUnit\Framework\TestCase' );
18+
}
1719

1820
/**
1921
* Class Curl

tests/UploaderTest.php

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@
77
require_once( 'TestHelper.php');
88
use Cloudinary;
99
use Exception;
10-
use PHPUnit\Framework\TestCase;
1110

12-
class UploaderTest extends TestCase {
13-
const LOGO_PNG = "tests/logo.png";
14-
15-
const TEST_ICO = "tests/favicon.ico";
11+
class UploaderTest extends \PHPUnit\Framework\TestCase {
1612

1713
public $url_prefix;
1814
public static function setUpBeforeClass() {
@@ -31,25 +27,24 @@ public function tearDown() {
3127
Curl::$instance = new Curl();
3228
}
3329

34-
public function test_upload() {
35-
$result = Uploader::upload(self::LOGO_PNG);
36-
37-
switch ($result["resource_type"]) {
38-
case "image":
39-
// generate image tag
40-
break;
41-
case "video":
42-
// generate video tag
43-
break;
44-
}
30+
public function test_upload() {
31+
$result = Uploader::upload( TEST_IMG );
32+
switch ( $result["resource_type"] ) {
33+
case "image":
34+
// generate image tag
35+
break;
36+
case "video":
37+
// generate video tag
38+
break;
39+
}
4540

4641
$this->assertEquals($result["width"], 241);
4742
$this->assertEquals($result["height"], 51);
4843
$expected_signature = Cloudinary::api_sign_request(array("public_id"=>$result["public_id"], "version"=>$result["version"]), Cloudinary::config_get("api_secret"));
4944
$this->assertEquals($result["signature"], $expected_signature);
5045
Curl::mockUpload($this);
5146

52-
Uploader::upload(self::LOGO_PNG, array("ocr"=>"adv_ocr"));
47+
Uploader::upload(TEST_IMG, array("ocr"=>"adv_ocr"));
5348
$fields = Curl::$instance->fields();
5449
$this->assertArraySubset(array("ocr"=>"adv_ocr"),$fields);
5550

@@ -107,21 +102,21 @@ public function test_build_eager() {
107102

108103
public function test_eager() {
109104
Curl::mockUpload($this);
110-
Uploader::upload(self::LOGO_PNG, array("eager"=>array("crop"=>"scale", "width"=>"2.0")));
105+
Uploader::upload(TEST_IMG, array("eager"=>array("crop"=>"scale", "width"=>"2.0")));
111106
$fields = Curl::$instance->fields();
112107
$this->assertArraySubset(array("eager"=> "c_scale,w_2.0"),$fields);
113108
}
114109

115110
public function test_upload_async() {
116111
Curl::mockUpload($this);
117-
Uploader::upload(self::LOGO_PNG, array("transformation"=>array("crop"=>"scale", "width"=>"2.0"), "async"=>TRUE));
112+
Uploader::upload(TEST_IMG, array("transformation"=>array("crop"=>"scale", "width"=>"2.0"), "async"=>TRUE));
118113
$fields = Curl::$instance->fields();
119114
$this->assertArraySubset(array("async"=> TRUE),$fields);
120115
}
121-
116+
122117
public function test_headers() {
123-
Uploader::upload(self::LOGO_PNG, array("headers"=>array("Link: 1")));
124-
Uploader::upload(self::LOGO_PNG, array("headers"=>array("Link" => "1")));
118+
Uploader::upload(TEST_IMG, array("headers"=>array("Link: 1")));
119+
Uploader::upload(TEST_IMG, array("headers"=>array("Link" => "1")));
125120
}
126121

127122
public function test_text() {
@@ -132,7 +127,7 @@ public function test_text() {
132127

133128
public function test_tags() {
134129
$api = new \Cloudinary\Api();
135-
$result = Uploader::upload(self::LOGO_PNG);
130+
$result = Uploader::upload(TEST_IMG);
136131
Curl::mockUpload($this);
137132
Uploader::add_tag("tag1", "foobar");
138133
assertUrl($this, "/image/tags");
@@ -170,16 +165,16 @@ public function test_huge_public_id_list() {
170165
}
171166
public function test_use_filename() {
172167
$api = new \Cloudinary\Api();
173-
$result = Uploader::upload(self::LOGO_PNG, array("use_filename"=>TRUE));
168+
$result = Uploader::upload(TEST_IMG, array("use_filename"=>TRUE));
174169
$this->assertRegExp('/logo_[a-zA-Z0-9]{6}/', $result["public_id"]);
175-
$result = Uploader::upload(self::LOGO_PNG, array("use_filename"=>TRUE, "unique_filename"=>FALSE));
170+
$result = Uploader::upload(TEST_IMG, array("use_filename"=>TRUE, "unique_filename"=>FALSE));
176171
$this->assertEquals("logo", $result["public_id"]);
177172
}
178173

179174
public function test_allowed_formats() {
180175
//should allow whitelisted formats if allowed_formats
181176
$formats = array("png");
182-
$result = Uploader::upload(self::LOGO_PNG, array("allowed_formats" => $formats));
177+
$result = Uploader::upload(TEST_IMG, array("allowed_formats" => $formats));
183178
$this->assertEquals($result["format"], "png");
184179
}
185180

@@ -188,7 +183,7 @@ public function test_allowed_formats_with_illegal_format() {
188183
$error_found = FALSE;
189184
$formats = array("jpg");
190185
try{
191-
Uploader::upload(self::LOGO_PNG, array("allowed_formats" => $formats));
186+
Uploader::upload(TEST_IMG, array("allowed_formats" => $formats));
192187
} catch (Exception $e) {
193188
$error_found = TRUE;
194189
}
@@ -198,14 +193,14 @@ public function test_allowed_formats_with_illegal_format() {
198193
public function test_allowed_formats_with_format() {
199194
//should allow non whitelisted formats if type is specified and convert to that type
200195
$formats = array("jpg");
201-
$result = Uploader::upload(self::LOGO_PNG, array("allowed_formats" => $formats, "format" => "jpg"));
196+
$result = Uploader::upload(TEST_IMG, array("allowed_formats" => $formats, "format" => "jpg"));
202197
$this->assertEquals("jpg", $result["format"]);
203198
}
204199

205200
public function test_face_coordinates() {
206201
//should allow sending face and custom coordinates
207202
$face_coordinates = array(array(120, 30, 109, 51), array(121, 31, 110, 51));
208-
$result = Uploader::upload(self::LOGO_PNG, array("face_coordinates" => $face_coordinates, "faces" => TRUE));
203+
$result = Uploader::upload(TEST_IMG, array("face_coordinates" => $face_coordinates, "faces" => TRUE));
209204
$this->assertEquals($face_coordinates, $result["faces"]);
210205

211206
$different_face_coordinates = array(array(122, 32, 111, 152));
@@ -220,7 +215,7 @@ public function test_face_coordinates() {
220215
public function test_context() {
221216
//should allow sending context
222217
$context = array("caption" => "cap=caps", "alt" => "alternative|alt=a");
223-
$result = Uploader::upload(self::LOGO_PNG, array("context" => $context));
218+
$result = Uploader::upload(TEST_IMG, array("context" => $context));
224219

225220
$api = new \Cloudinary\Api();
226221
$info = $api->resource($result["public_id"]);
@@ -232,7 +227,7 @@ public function test_context() {
232227

233228
public function test_context_api() {
234229
$api = new \Cloudinary\Api();
235-
$result = Uploader::upload(self::LOGO_PNG);
230+
$result = Uploader::upload(TEST_IMG);
236231
Uploader::add_context('alt=testAlt|custom=testCustom', $result['public_id']);
237232
// fwrite(STDERR, print_r($result['public_id'], TRUE));
238233
assertUrl($this, "/image/context");
@@ -275,7 +270,7 @@ public function test_cl_image_upload_tag() {
275270

276271
function test_manual_moderation() {
277272
// should support setting manual moderation status
278-
$resource = Uploader::upload(self::LOGO_PNG, array("moderation"=>"manual"));
273+
$resource = Uploader::upload(TEST_IMG, array("moderation"=>"manual"));
279274
$this->assertEquals($resource["moderation"][0]["status"], "pending");
280275
$this->assertEquals($resource["moderation"][0]["kind"], "manual");
281276
}
@@ -295,7 +290,7 @@ function test_raw_conversion() {
295290
*/
296291
function test_categorization() {
297292
// should support requesting categorization
298-
Uploader::upload(self::LOGO_PNG, array("categorization" => "illegal"));
293+
Uploader::upload(TEST_IMG, array("categorization" => "illegal"));
299294
}
300295

301296
/**
@@ -304,7 +299,7 @@ function test_categorization() {
304299
*/
305300
function test_detection() {
306301
// should support requesting detection
307-
Uploader::upload(self::LOGO_PNG, array("detection" => "illegal"));
302+
Uploader::upload(TEST_IMG, array("detection" => "illegal"));
308303
}
309304

310305
/**
@@ -313,7 +308,7 @@ function test_detection() {
313308
*/
314309
function test_auto_tagging() {
315310
// should support requesting auto_tagging
316-
Uploader::upload(self::LOGO_PNG, array("auto_tagging" => 0.5));
311+
Uploader::upload(TEST_IMG, array("auto_tagging" => 0.5));
317312
}
318313

319314
/**
@@ -322,7 +317,7 @@ function test_auto_tagging() {
322317
*/
323318
function test_background_removal() {
324319
// should support requesting background_removal
325-
Uploader::upload(self::LOGO_PNG, array("background_removal" => "illegal"));
320+
Uploader::upload(TEST_IMG, array("background_removal" => "illegal"));
326321
}
327322

328323
function test_large_upload() {
@@ -357,7 +352,7 @@ function test_upload_preset() {
357352
// should support unsigned uploading using presets
358353
$api = new \Cloudinary\Api();
359354
$preset = $api->create_upload_preset(array("folder"=>"upload_folder", "unsigned"=>TRUE));
360-
$result = Uploader::unsigned_upload(self::LOGO_PNG, $preset["name"]);
355+
$result = Uploader::unsigned_upload(TEST_IMG, $preset["name"]);
361356
$this->assertRegExp('/^upload_folder\/[a-z0-9]+$/', $result["public_id"]);
362357
$api->delete_upload_preset($preset["name"]);
363358

@@ -375,7 +370,7 @@ function test_upload_timeout()
375370
try {
376371
// Use a lengthy PNG transformation
377372
$transformation = array("crop" => "scale", "width" => "2.0", "angle" => 33);
378-
Uploader::upload(self::LOGO_PNG, array("eager"=>array("transformation"=>array($transformation, $transformation))));
373+
Uploader::upload(TEST_IMG, array("eager"=>array("transformation"=>array($transformation, $transformation))));
379374
} catch ( Exception $e ) {
380375
// Finally not supported in PHP 5.3
381376
Cloudinary::config(array( "timeout", $timeout ));
@@ -386,7 +381,7 @@ function test_upload_timeout()
386381

387382
function test_upload_responsive_breakpoints()
388383
{
389-
$response = Uploader::upload(self::LOGO_PNG, array("responsive_breakpoints"=>array(array("create_derived"=>FALSE))));
384+
$response = Uploader::upload(TEST_IMG, array("responsive_breakpoints"=>array(array("create_derived"=>FALSE))));
390385
$this->assertArrayHasKey("responsive_breakpoints", $response, "Should return responsive_breakpoints information");
391386
$this->assertEquals(2, count($response["responsive_breakpoints"][0]["breakpoints"]));
392387
}

0 commit comments

Comments
 (0)