(REF) WorkflowMessageTest - Use dataProvider
authorTim Otten <totten@civicrm.org>
Wed, 15 Jun 2022 08:12:28 +0000 (01:12 -0700)
committerTim Otten <totten@civicrm.org>
Thu, 16 Jun 2022 01:20:29 +0000 (18:20 -0700)
tests/phpunit/api/v4/Entity/WorkflowMessageTest.php

index a8b7377b01a7ea13f0a15be0dfe01f2c6fb4828c..162379d0cc28f99eb2892c26b5b4b961bf01ca06 100644 (file)
@@ -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));
+      }
     }
   }