APIv4 - Update tests to use new Invasive helper
authorcolemanw <coleman@civicrm.org>
Sat, 27 May 2023 15:38:01 +0000 (11:38 -0400)
committercolemanw <coleman@civicrm.org>
Sat, 27 May 2023 15:38:01 +0000 (11:38 -0400)
tests/phpunit/api/v4/Action/BasicActionsTest.php
tests/phpunit/api/v4/Action/EvaluateConditionTest.php

index fad0c8f079cc3cd845b3f709a31e47907b767257..b1242786889539dace78a3c1c807efeeea4dfbe7 100644 (file)
@@ -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() {
index 551bcb41d2c9bf09d4646b0abf8f4737f7ea45e1..95c6e67962f0993c0023b7664c1d861bda8b8810 100644 (file)
@@ -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]));
   }
 
 }