3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
14 use Civi\WorkflowMessage\WorkflowMessage
;
16 trait WorkflowMessageTestTrait
{
18 abstract public function getWorkflowClass(): string;
20 public function getWorkflowName(): string {
21 $class = $this->getWorkflowClass();
22 return $class::WORKFLOW
;
26 * @return \Civi\Api4\Generic\AbstractGetAction
27 * @throws \API_Exception
29 protected function findExamples(): \Civi\Api4\Generic\AbstractGetAction
{
30 return \Civi\Api4\ExampleData
::get(0)
31 ->setSelect(['name', 'title', 'tags', 'data', 'asserts'])
32 ->addWhere('name', 'LIKE', 'workflow/' . $this->getWorkflowName() . '/%')
33 ->addWhere('tags', 'CONTAINS', 'phpunit');
37 * @param array $exampleProps
38 * @param string $exampleName
39 * @throws \Civi\WorkflowMessage\Exception\WorkflowMessageException
41 protected function assertConstructorEquivalence(array $exampleProps, $exampleName = ''): void
{
42 $class = $this->getWorkflowClass();
44 $instances["factory_$exampleName"] = WorkflowMessage
::create($this->getWorkflowName(), $exampleProps);
45 $instances["class_$exampleName"] = new $class($exampleProps);
47 /** @var \Civi\WorkflowMessage\WorkflowMessageInterface $refInstance */
48 /** @var \Civi\WorkflowMessage\WorkflowMessageInterface $cmpInstance */
50 $refName = $refInstance = NULL;
52 foreach ($instances as $cmpName => $cmpInstance) {
53 if ($refName === NULL) {
55 $refInstance = $cmpInstance;
59 $this->assertSameWorkflowMessage($refInstance, $cmpInstance, "Compare $refName vs $cmpName: ");
62 $this->assertEquals(1, $comparisons);
66 * @param \Civi\WorkflowMessage\WorkflowMessageInterface $refInstance
67 * @param \Civi\WorkflowMessage\WorkflowMessageInterface $cmpInstance
68 * @param string|null $prefix
70 protected function assertSameWorkflowMessage(\Civi\WorkflowMessage\WorkflowMessageInterface
$refInstance, \Civi\WorkflowMessage\WorkflowMessageInterface
$cmpInstance, ?
string $prefix = NULL): void
{
71 if ($prefix === NULL) {
72 $prefix = sprintf('[%s] ', $this->getWorkflowName());
74 $this->assertEquals($refInstance->export('tplParams'), $cmpInstance->export('tplParams'), "{$prefix}Should have same export(tplParams)");
75 $this->assertEquals($refInstance->export('tokenContext'), $cmpInstance->export('tokenContext'), "{$prefix}should have same export(tokenContext)");
76 $this->assertEquals($refInstance->export('envelope'), $cmpInstance->export('envelope'), "{$prefix}Should have same export(envelope)");
77 $refExportAll = WorkflowMessage
::exportAll($refInstance);
78 $cmpExportAll = WorkflowMessage
::exportAll($cmpInstance);
79 $this->assertEquals($refExportAll, $cmpExportAll, "{$prefix}Should have same exportAll()");