Commit | Line | Data |
---|---|---|
4f22d37e TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | Copyright CiviCRM LLC. All rights reserved. | | |
5 | | | | |
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 | +--------------------------------------------------------------------+ | |
10 | */ | |
11 | ||
12 | namespace Civi\Test; | |
13 | ||
14 | use Civi\WorkflowMessage\WorkflowMessage; | |
15 | ||
16 | trait WorkflowMessageTestTrait { | |
17 | ||
18 | abstract public function getWorkflowClass(): string; | |
19 | ||
20 | public function getWorkflowName(): string { | |
21 | $class = $this->getWorkflowClass(); | |
22 | return $class::WORKFLOW; | |
23 | } | |
24 | ||
25 | /** | |
26 | * @return \Civi\Api4\Generic\AbstractGetAction | |
27 | * @throws \API_Exception | |
28 | */ | |
29 | protected function findExamples(): \Civi\Api4\Generic\AbstractGetAction { | |
30 | return \Civi\Api4\WorkflowMessageExample::get(0) | |
31 | ->setSelect(['name', 'title', 'workflow', 'tags', 'data', 'asserts']) | |
32 | ->addWhere('workflow', '=', $this->getWorkflowName()) | |
33 | ->addWhere('tags', 'CONTAINS', 'phpunit'); | |
34 | } | |
35 | ||
36 | /** | |
37 | * @param array $exampleProps | |
38 | * @param string $exampleName | |
39 | * @throws \Civi\WorkflowMessage\Exception\WorkflowMessageException | |
40 | */ | |
41 | protected function assertConstructorEquivalence(array $exampleProps, $exampleName = ''): void { | |
42 | $class = $this->getWorkflowClass(); | |
43 | $instances = []; | |
44 | $instances["factory_$exampleName"] = WorkflowMessage::create($this->getWorkflowName(), $exampleProps); | |
45 | $instances["class_$exampleName"] = new $class($exampleProps); | |
46 | ||
47 | /** @var \Civi\WorkflowMessage\WorkflowMessageInterface $refInstance */ | |
48 | /** @var \Civi\WorkflowMessage\WorkflowMessageInterface $cmpInstance */ | |
49 | ||
50 | $refName = $refInstance = NULL; | |
51 | $comparisons = 0; | |
52 | foreach ($instances as $cmpName => $cmpInstance) { | |
53 | if ($refName === NULL) { | |
54 | $refName = $cmpName; | |
55 | $refInstance = $cmpInstance; | |
56 | continue; | |
57 | } | |
58 | ||
59 | $this->assertSameWorkflowMessage($refInstance, $cmpInstance, "Compare $refName vs $cmpName: "); | |
60 | $comparisons++; | |
61 | } | |
62 | $this->assertEquals(1, $comparisons); | |
63 | } | |
64 | ||
65 | /** | |
66 | * @param \Civi\WorkflowMessage\WorkflowMessageInterface $refInstance | |
67 | * @param \Civi\WorkflowMessage\WorkflowMessageInterface $cmpInstance | |
68 | * @param string|null $prefix | |
69 | */ | |
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()); | |
73 | } | |
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()"); | |
80 | } | |
81 | ||
82 | } |