From 409b1e0c30d28179f6244c1f180838a473d7582c Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 15 Jun 2022 01:12:28 -0700 Subject: [PATCH] (REF) WorkflowMessageTest - Use dataProvider --- .../api/v4/Entity/WorkflowMessageTest.php | 67 +++++++++++++------ 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/tests/phpunit/api/v4/Entity/WorkflowMessageTest.php b/tests/phpunit/api/v4/Entity/WorkflowMessageTest.php index a8b7377b01..162379d0cc 100644 --- a/tests/phpunit/api/v4/Entity/WorkflowMessageTest.php +++ b/tests/phpunit/api/v4/Entity/WorkflowMessageTest.php @@ -66,29 +66,54 @@ class WorkflowMessageTest extends Api4TestBase implements TransactionalInterface $this->assertRegExp('/The role is myrole./', $result['text']); } - public function testRenderExamples() { - $examples = \Civi\Api4\ExampleData::get(0) - ->addWhere('tags', 'CONTAINS', 'phpunit') - ->addSelect('name', 'data', 'asserts') - ->execute(); - $this->assertTrue($examples->rowCount >= 1); - foreach ($examples as $example) { - $this->assertTrue(!empty($example['data']['modelProps']), sprintf('Example (%s) is tagged phpunit. It should have modelProps.', $example['name'])); - $this->assertTrue(!empty($example['asserts']['default']), sprintf('Example (%s) is tagged phpunit. It should have assertions.', $example['name'])); - $result = \Civi\Api4\WorkflowMessage::render(0) - ->setWorkflow($example['data']['workflow']) - ->setValues($example['data']['modelProps']) - ->execute() - ->single(); - foreach ($example['asserts']['default'] as $num => $assert) { - $msg = sprintf('Check assertion(%s) on example (%s)', $num, $example['name']); - if (isset($assert['regex'])) { - $this->assertRegExp($assert['regex'], $result[$assert['for']], $msg); - } - else { - $this->fail('Unrecognized assertion: ' . json_encode($assert)); + public function getRenderExamples(): array { + $metas = \Civi\Test::examples()->getMetas(); + $results = []; + foreach ($metas as $name => $meta) { + if (empty($meta['workflow'])) { + continue; + } + if (empty($meta['tags']) || !in_array('phpunit', $meta['tags'])) { + continue; + } + if ($exampleFilter = getenv('WORKFLOW_EXAMPLES')) { + if (!preg_match($exampleFilter, $name)) { + continue; } } + $results[$name] = [$meta['name']]; + } + return $results; + } + + /** + * @param string $name + * @throws \API_Exception + * @throws \Civi\API\Exception\UnauthorizedException + * @dataProvider getRenderExamples + */ + public function testRenderExamples(string $name) { + $example = \Civi\Api4\ExampleData::get(0) + ->addWhere('name', '=', $name) + ->addSelect('name', 'file', 'data', 'asserts') + ->execute() + ->single(); + + $this->assertTrue(!empty($example['data']['modelProps']), sprintf('Example (%s) is tagged phpunit. It should have modelProps.', $example['name'])); + $this->assertTrue(!empty($example['asserts']['default']), sprintf('Example (%s) is tagged phpunit. It should have assertions.', $example['name'])); + $result = \Civi\Api4\WorkflowMessage::render(0) + ->setWorkflow($example['data']['workflow']) + ->setValues($example['data']['modelProps']) + ->execute() + ->single(); + foreach ($example['asserts']['default'] as $num => $assert) { + $msg = sprintf('Check assertion(%s) on example (%s)', $num, $example['name']); + if (isset($assert['regex'])) { + $this->assertRegExp($assert['regex'], $result[$assert['for']], $msg); + } + else { + $this->fail('Unrecognized assertion: ' . json_encode($assert)); + } } } -- 2.25.1