From: colemanw Date: Sat, 27 May 2023 15:38:01 +0000 (-0400) Subject: APIv4 - Update tests to use new Invasive helper X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=aa388991ebeb1802a7e3266bff50b34d01adef1b;p=civicrm-core.git APIv4 - Update tests to use new Invasive helper --- diff --git a/tests/phpunit/api/v4/Action/BasicActionsTest.php b/tests/phpunit/api/v4/Action/BasicActionsTest.php index fad0c8f079..b124278688 100644 --- a/tests/phpunit/api/v4/Action/BasicActionsTest.php +++ b/tests/phpunit/api/v4/Action/BasicActionsTest.php @@ -25,6 +25,7 @@ use Civi\Api4\Utils\CoreUtil; use Civi\Core\Event\GenericHookEvent; use Civi\Test\CiviEnvBuilder; use Civi\Test\HookInterface; +use Civi\Test\Invasive; use Civi\Test\TransactionalInterface; /** @@ -232,45 +233,43 @@ class BasicActionsTest extends Api4TestBase implements HookInterface, Transactio ->addWhere('size', 'LIKE', 'big') ->addWhere('shape', 'LIKE', '%a'); - $itemsToGet = new \ReflectionMethod($get, '_itemsToGet'); - $itemsToGet->setAccessible(TRUE); + $itemsToGet = [$get, '_itemsToGet']; - $this->assertEquals(['red', 'blue'], $itemsToGet->invoke($get, 'color')); - $this->assertEquals(['one'], $itemsToGet->invoke($get, 'group')); - $this->assertEquals(['big'], $itemsToGet->invoke($get, 'size')); - $this->assertEmpty($itemsToGet->invoke($get, 'shape')); - $this->assertEmpty($itemsToGet->invoke($get, 'weight')); + $this->assertEquals(['red', 'blue'], Invasive::call($itemsToGet, ['color'])); + $this->assertEquals(['one'], Invasive::call($itemsToGet, ['group'])); + $this->assertEquals(['big'], Invasive::call($itemsToGet, ['size'])); + $this->assertEmpty(Invasive::call($itemsToGet, ['shape'])); + $this->assertEmpty(Invasive::call($itemsToGet, ['weight'])); } public function testFieldsToGet() { $get = MockBasicEntity::get() ->addWhere('color', '!=', 'green'); - $isFieldSelected = new \ReflectionMethod($get, '_isFieldSelected'); - $isFieldSelected->setAccessible(TRUE); + $isFieldSelected = [$get, '_isFieldSelected']; // If no "select" is set, should always return true - $this->assertTrue($isFieldSelected->invoke($get, 'color')); - $this->assertTrue($isFieldSelected->invoke($get, 'shape')); - $this->assertTrue($isFieldSelected->invoke($get, 'size', 'color', 'shape')); + $this->assertTrue(Invasive::call($isFieldSelected, ['color'])); + $this->assertTrue(Invasive::call($isFieldSelected, ['shape'])); + $this->assertTrue(Invasive::call($isFieldSelected, ['size', 'color', 'shape'])); // With a non-empty "select" fieldsToSelect() will return fields needed to evaluate each clause. $get->addSelect('identifier'); - $this->assertTrue($isFieldSelected->invoke($get, 'color', 'shape', 'size')); - $this->assertTrue($isFieldSelected->invoke($get, 'identifier')); - $this->assertFalse($isFieldSelected->invoke($get, 'shape', 'size', 'weight')); - $this->assertFalse($isFieldSelected->invoke($get, 'group')); + $this->assertTrue(Invasive::call($isFieldSelected, ['color', 'shape', 'size'])); + $this->assertTrue(Invasive::call($isFieldSelected, ['identifier'])); + $this->assertFalse(Invasive::call($isFieldSelected, ['shape', 'size', 'weight'])); + $this->assertFalse(Invasive::call($isFieldSelected, ['group'])); $get->addClause('OR', ['shape', '=', 'round'], ['AND', [['size', '=', 'big'], ['weight', '!=', 'small']]]); - $this->assertTrue($isFieldSelected->invoke($get, 'color')); - $this->assertTrue($isFieldSelected->invoke($get, 'identifier')); - $this->assertTrue($isFieldSelected->invoke($get, 'shape')); - $this->assertTrue($isFieldSelected->invoke($get, 'size')); - $this->assertTrue($isFieldSelected->invoke($get, 'group', 'weight')); - $this->assertFalse($isFieldSelected->invoke($get, 'group')); + $this->assertTrue(Invasive::call($isFieldSelected, ['color'])); + $this->assertTrue(Invasive::call($isFieldSelected, ['identifier'])); + $this->assertTrue(Invasive::call($isFieldSelected, ['shape'])); + $this->assertTrue(Invasive::call($isFieldSelected, ['size'])); + $this->assertTrue(Invasive::call($isFieldSelected, ['group', 'weight'])); + $this->assertFalse(Invasive::call($isFieldSelected, ['group'])); $get->addOrderBy('group'); - $this->assertTrue($isFieldSelected->invoke($get, 'group')); + $this->assertTrue(Invasive::call($isFieldSelected, ['group'])); } public function testWildcardSelect() { diff --git a/tests/phpunit/api/v4/Action/EvaluateConditionTest.php b/tests/phpunit/api/v4/Action/EvaluateConditionTest.php index 551bcb41d2..95c6e67962 100644 --- a/tests/phpunit/api/v4/Action/EvaluateConditionTest.php +++ b/tests/phpunit/api/v4/Action/EvaluateConditionTest.php @@ -21,6 +21,7 @@ namespace api\v4\Action; use Civi\Api4\MockBasicEntity; use api\v4\Api4TestBase; +use Civi\Test\Invasive; use Civi\Test\TransactionalInterface; /** @@ -29,10 +30,7 @@ use Civi\Test\TransactionalInterface; class EvaluateConditionTest extends Api4TestBase implements TransactionalInterface { public function testEvaluateCondition() { - $action = MockBasicEntity::get(); - $reflection = new \ReflectionClass($action); - $method = $reflection->getMethod('evaluateCondition'); - $method->setAccessible(TRUE); + $method = [MockBasicEntity::get(), 'evaluateCondition']; $data = [ 'nada' => 0, @@ -43,14 +41,14 @@ class EvaluateConditionTest extends Api4TestBase implements TransactionalInterfa 'values' => ['one' => 1, 'two' => 2, 'three' => 3], ]; - $this->assertFalse($method->invoke($action, '$uno > $dos', $data)); - $this->assertTrue($method->invoke($action, '$uno < $dos', $data)); - $this->assertTrue($method->invoke($action, '$apple == "red" && $banana != "red"', $data)); - $this->assertFalse($method->invoke($action, '$apple == "red" && $banana != "yellow"', $data)); - $this->assertTrue($method->invoke($action, '$values.one == $uno', $data)); - $this->assertTrue($method->invoke($action, '$values.one + $dos == $values.three', $data)); - $this->assertTrue($method->invoke($action, 'empty($nada)', $data)); - $this->assertFalse($method->invoke($action, 'empty($values)', $data)); + $this->assertFalse(Invasive::call($method, ['$uno > $dos', $data])); + $this->assertTrue(Invasive::call($method, ['$uno < $dos', $data])); + $this->assertTrue(Invasive::call($method, ['$apple == "red" && $banana != "red"', $data])); + $this->assertFalse(Invasive::call($method, ['$apple == "red" && $banana != "yellow"', $data])); + $this->assertTrue(Invasive::call($method, ['$values.one == $uno', $data])); + $this->assertTrue(Invasive::call($method, ['$values.one + $dos == $values.three', $data])); + $this->assertTrue(Invasive::call($method, ['empty($nada)', $data])); + $this->assertFalse(Invasive::call($method, ['empty($values)', $data])); } }