From 1f28277930e19d9b24ac6f2b843d54bd70cb1589 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 16 Feb 2023 15:51:23 -0800 Subject: [PATCH] (REF) Move test for APIv4 casting. Make it easier to mock different types. --- tests/phpunit/Civi/API/RequestTest.php | 30 ----- .../api/v4/Action/AbstractActionTest.php | 107 ++++++++++++++++++ 2 files changed, 107 insertions(+), 30 deletions(-) create mode 100644 tests/phpunit/api/v4/Action/AbstractActionTest.php diff --git a/tests/phpunit/Civi/API/RequestTest.php b/tests/phpunit/Civi/API/RequestTest.php index bd5777509f..83d85f7a4a 100644 --- a/tests/phpunit/Civi/API/RequestTest.php +++ b/tests/phpunit/Civi/API/RequestTest.php @@ -68,34 +68,4 @@ class RequestTest extends \CiviUnitTestCase { Request::create($inEntity, $inAction, ['version' => $inVersion], NULL); } - public function getCastingExamples(): array { - $exs = []; - // We run the same tests on `$checkPermissions` (which has real PHP setter method) - // and `$useTrash` (which has a generic magic method) to show that casting is similar. - $exs[] = ['Contact.delete checkPermissions', 0, FALSE]; - $exs[] = ['Contact.delete checkPermissions', '0', FALSE]; - $exs[] = ['Contact.delete checkPermissions', 1, TRUE]; - $exs[] = ['Contact.delete checkPermissions', '1', TRUE]; - $exs[] = ['Contact.delete useTrash', 0, FALSE]; - $exs[] = ['Contact.delete useTrash', '0', FALSE]; - $exs[] = ['Contact.delete useTrash', 1, TRUE]; - $exs[] = ['Contact.delete useTrash', '1', TRUE]; - return $exs; - } - - /** - * @param $entityActionField - * @param $inputValue - * @param $expectValue - * @dataProvider getCastingExamples - */ - public function testCasting(string $entityActionField, $inputValue, $expectValue): void { - [$entity, $action, $field] = preg_split('/[ \.]/', $entityActionField); - $request = Request::create($entity, $action, ['version' => 4, $field => $inputValue]); - $getter = 'get' . ucfirst($field); - $actualValue = call_user_func([$request, $getter]); - $this->assertEquals(gettype($actualValue), gettype($expectValue)); - $this->assertTrue($actualValue === $expectValue); - } - } diff --git a/tests/phpunit/api/v4/Action/AbstractActionTest.php b/tests/phpunit/api/v4/Action/AbstractActionTest.php new file mode 100644 index 0000000000..ee028fd7c7 --- /dev/null +++ b/tests/phpunit/api/v4/Action/AbstractActionTest.php @@ -0,0 +1,107 @@ +simpleBool = $simpleBool; + return $this; + } + + /** + * @param \Civi\Api4\Generic\Result $result + */ + public function _run(Result $result) { + } + + }; + } + + public function getCastingExamples(): array { + $exs = []; + + $exs['bool'] = [ + ['simpleBool', 'magicBool'], + [ + // Each item is an example: [$inputValue, $expectValue] + [0, FALSE], + ['0', FALSE], + [1, TRUE], + ['1', TRUE], + ], + ]; + + return $exs; + } + + /** + * When you set a property on an APIv4 action, it should apply some type-casting rules -- even + * if the property has magic methods. + * + * @param string[] $fields + * Name of a PHP properties to test. (See `createExample()` object.) + * @param array $conversions + * List of inputs and their expected outputs. + * Ex: [[1, TRUE], ['1', TRUE]] + * @dataProvider getCastingExamples + * @see \Civi\Api4\Utils\ReflectionUtils::castTypeSoftly() + */ + public function testCasting(array $fields, array $conversions): void { + $this->assertTrue(!empty($fields) && !empty($conversions)); + foreach ($fields as $field) { + foreach ($conversions as $conversion) { + [$inputValue, $expectValue] = $conversion; + $desc = sprintf("For field %s, casting should convert %s to %s", $field, json_encode($inputValue), json_encode($expectValue)); + $setter = 'set' . ucfirst($field); + $getter = 'get' . ucfirst($field); + + $request = $this->createExample(); + call_user_func([$request, $setter], $inputValue); + $actualValue = call_user_func([$request, $getter]); + $this->assertEquals(gettype($actualValue), gettype($expectValue), "$desc"); + $this->assertTrue($actualValue === $expectValue, $desc); + } + } + } + +} -- 2.25.1