Skip to content

Commit

Permalink
fix error with phpunit10 (assertObjectHasAttribute deprecated)
Browse files Browse the repository at this point in the history
  • Loading branch information
tenzap committed Jun 21, 2023
1 parent 85d02ab commit 31f32b4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
36 changes: 22 additions & 14 deletions tests/codeigniter/core/Loader_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class Loader_test extends CI_TestCase {

private $ci_obj;

private $realAssertObjectHasProperty;

public function set_up()
{
// Instantiate a new loader
Expand All @@ -16,6 +18,12 @@ public function set_up()
// Set subclass prefix
$this->prefix = 'MY_';
$this->ci_set_config('subclass_prefix', $this->prefix);

// assertObjectHasAttribute() is deprecated and will be removed in PHPUnit 10.
// It was replaced by assertObjectHasProperty() in phpunit 10.1.0+
$this->realAssertObjectHasProperty = method_exists($this, 'assertObjectHasProperty')
? 'assertObjectHasProperty'
: 'assertObjectHasAttribute';
}

// --------------------------------------------------------------------
Expand All @@ -36,7 +44,7 @@ public function test_library()
// Test loading as an array.
$this->assertInstanceOf('CI_Loader', $this->load->library(array($lib)));
$this->assertTrue(class_exists($class), $class.' does not exist');
$this->assertObjectHasAttribute($lib, $this->ci_obj);
call_user_func_array(array($this, $this->realAssertObjectHasProperty), array($lib, $this->ci_obj));
$this->assertInstanceOf($class, $this->ci_obj->$lib);

// Create library in VFS
Expand Down Expand Up @@ -88,21 +96,21 @@ public function test_library_extension()
$this->assertInstanceOf('CI_Loader', $this->load->library($lib));
$this->assertTrue(class_exists($class), $class.' does not exist');
$this->assertTrue(class_exists($ext), $ext.' does not exist');
$this->assertObjectHasAttribute($name, $this->ci_obj);
call_user_func_array(array($this, $this->realAssertObjectHasProperty), array($name, $this->ci_obj));
$this->assertInstanceOf($class, $this->ci_obj->$name);
$this->assertInstanceOf($ext, $this->ci_obj->$name);

// Test reloading with object name
$obj = 'exttest';
$this->assertInstanceOf('CI_Loader', $this->load->library($lib, NULL, $obj));
$this->assertObjectHasAttribute($obj, $this->ci_obj);
call_user_func_array(array($this, $this->realAssertObjectHasProperty), array($obj, $this->ci_obj));
$this->assertInstanceOf($class, $this->ci_obj->$obj);
$this->assertInstanceOf($ext, $this->ci_obj->$obj);

// Test reloading
unset($this->ci_obj->$name);
$this->assertInstanceOf('CI_Loader', $this->load->library($lib));
$this->assertObjectHasAttribute($name, $this->ci_obj);
call_user_func_array(array($this, $this->realAssertObjectHasProperty), array($name, $this->ci_obj));

// Create baseless library
$name = 'ext_baseless_lib';
Expand Down Expand Up @@ -140,7 +148,7 @@ public function test_library_config()
$obj = 'testy';
$this->assertInstanceOf('CI_Loader', $this->load->library($lib, NULL, $obj));
$this->assertTrue(class_exists($class), $class.' does not exist');
$this->assertObjectHasAttribute($obj, $this->ci_obj);
call_user_func_array(array($this, $this->realAssertObjectHasProperty), array($obj, $this->ci_obj));
$this->assertInstanceOf($class, $this->ci_obj->$obj);
$this->assertEquals($cfg, $this->ci_obj->$obj->config);

Expand Down Expand Up @@ -172,7 +180,7 @@ public function test_load_library_in_application_dir()

// Was the model class instantiated.
$this->assertTrue(class_exists($class), $class.' does not exist');
$this->assertObjectHasAttribute($lib, $this->ci_obj);
call_user_func_array(array($this, $this->realAssertObjectHasProperty), array($lib, $this->ci_obj));
$this->assertInstanceOf($class, $this->ci_obj->$lib);
}

Expand All @@ -193,13 +201,13 @@ class_exists('CI_Driver_Library', TRUE);
// Test loading as an array.
$this->assertInstanceOf('CI_Loader', $this->load->driver(array($driver)));
$this->assertTrue(class_exists($class), $class.' does not exist');
$this->assertObjectHasAttribute($driver, $this->ci_obj);
call_user_func_array(array($this, $this->realAssertObjectHasProperty), array($driver, $this->ci_obj));
$this->assertInstanceOf($class, $this->ci_obj->$driver);

// Test loading as a library with a name
$obj = 'testdrive';
$this->assertInstanceOf('CI_Loader', $this->load->library($driver, NULL, $obj));
$this->assertObjectHasAttribute($obj, $this->ci_obj);
call_user_func_array(array($this, $this->realAssertObjectHasProperty), array($obj, $this->ci_obj));
$this->assertInstanceOf($class, $this->ci_obj->$obj);

// Test a string given to params
Expand All @@ -222,7 +230,7 @@ public function test_models()

// Was the model class instantiated.
$this->assertTrue(class_exists($model));
$this->assertObjectHasAttribute($model, $this->ci_obj);
call_user_func_array(array($this, $this->realAssertObjectHasProperty), array($model, $this->ci_obj));

// Test no model given
$this->assertInstanceOf('CI_Loader', $this->load->model(''));
Expand All @@ -248,8 +256,8 @@ public function test_model_subdir()

// Was the model class instantiated?
$this->assertTrue(class_exists($model));
$this->assertObjectHasAttribute($name, $this->ci_obj);
$this->assertObjectHasAttribute($name, $this->ci_obj);
call_user_func_array(array($this, $this->realAssertObjectHasProperty), array($name, $this->ci_obj));
call_user_func_array(array($this, $this->realAssertObjectHasProperty), array($name, $this->ci_obj));
$this->assertInstanceOf($base, $this->ci_obj->$name);
$this->assertInstanceOf($model, $this->ci_obj->$name);

Expand Down Expand Up @@ -607,17 +615,17 @@ public function test_initialize()

// Verify library
$this->assertTrue(class_exists($lib_class), $lib_class.' does not exist');
$this->assertObjectHasAttribute($lib, $this->ci_obj);
call_user_func_array(array($this, $this->realAssertObjectHasProperty), array($lib, $this->ci_obj));
$this->assertInstanceOf($lib_class, $this->ci_obj->$lib);

// Verify driver
$this->assertTrue(class_exists($drv_class), $drv_class.' does not exist');
$this->assertObjectHasAttribute($drv, $this->ci_obj);
call_user_func_array(array($this, $this->realAssertObjectHasProperty), array($drv, $this->ci_obj));
$this->assertInstanceOf($drv_class, $this->ci_obj->$drv);

// Verify model
$this->assertTrue(class_exists($model), $model.' does not exist');
$this->assertObjectHasAttribute($model, $this->ci_obj);
call_user_func_array(array($this, $this->realAssertObjectHasProperty), array($model, $this->ci_obj));
$this->assertInstanceOf($model, $this->ci_obj->$model);

// Verify config calls
Expand Down
17 changes: 13 additions & 4 deletions tests/codeigniter/libraries/Driver_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Driver_test extends CI_TestCase {

private $name;

private $realAssertObjectHasProperty;

/**
* Set up test framework
*/
Expand All @@ -25,6 +27,12 @@ public function set_up()
// Create mock driver library
$this->name = 'Driver';
$this->lib = new Mock_Libraries_Driver();

// assertObjectHasAttribute() is deprecated and will be removed in PHPUnit 10.
// It was replaced by assertObjectHasProperty() in phpunit 10.1.0+
$this->realAssertObjectHasProperty = method_exists($this, 'assertObjectHasProperty')
? 'assertObjectHasProperty'
: 'assertObjectHasAttribute';
}

/**
Expand All @@ -51,12 +59,12 @@ public function test_load_driver()
$this->assertEquals($this->name, $this->lib->get_name());

// Was driver loaded?
$this->assertObjectHasAttribute($driver, $this->lib);
call_user_func_array(array($this, $this->realAssertObjectHasProperty), array($driver, $this->lib));
$this->assertInstanceOf($class, $this->lib->$driver);
$this->assertInstanceOf('CI_Driver', $this->lib->$driver);

// Was decorate called?
$this->assertObjectHasAttribute($prop, $this->lib->$driver);
call_user_func_array(array($this, $this->realAssertObjectHasProperty), array($prop, $this->lib->$driver));
$this->assertTrue($this->lib->$driver->$prop);

// Do we get an error for an invalid driver?
Expand Down Expand Up @@ -86,7 +94,8 @@ public function test_load_app_driver()
$this->assertNotNull($this->lib->load_driver($driver));

// Was driver loaded?
$this->assertObjectHasAttribute($driver, $this->lib);
call_user_func_array(array($this, $this->realAssertObjectHasProperty), array($driver, $this->lib));

$this->assertInstanceOf($class, $this->lib->$driver);
$this->assertInstanceOf('CI_Driver', $this->lib->$driver);

Expand Down Expand Up @@ -120,7 +129,7 @@ public function test_load_driver_ext()
$this->assertNotNull($this->lib->load_driver($driver));

// Was driver loaded?
$this->assertObjectHasAttribute($driver, $this->lib);
call_user_func_array(array($this, $this->realAssertObjectHasProperty), array($driver, $this->lib));
$this->assertInstanceOf($class, $this->lib->$driver);
$this->assertInstanceOf($baseclass, $this->lib->$driver);
$this->assertInstanceOf('CI_Driver', $this->lib->$driver);
Expand Down

0 comments on commit 31f32b4

Please sign in to comment.