|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Kerox\Fcm\Model\Message; |
| 6 | + |
| 7 | +use Kerox\Fcm\Model\Message\Condition; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +class ConditionTest extends TestCase |
| 11 | +{ |
| 12 | + public function testConditionAndWithOnlyTopics(): void |
| 13 | + { |
| 14 | + $condition = (new Condition())->and('TopicA', 'TopicB', 'TopicC'); |
| 15 | + |
| 16 | + $this->assertSame("'TopicA' in topics && 'TopicB' in topics && 'TopicC' in topics", $condition); |
| 17 | + } |
| 18 | + |
| 19 | + public function testConditionOrWithOnlyTopics(): void |
| 20 | + { |
| 21 | + $condition = (new Condition())->or('TopicA', 'TopicB', 'TopicC'); |
| 22 | + |
| 23 | + $this->assertSame("'TopicA' in topics || 'TopicB' in topics || 'TopicC' in topics", $condition); |
| 24 | + } |
| 25 | + |
| 26 | + public function testConditionNotWithTopic(): void |
| 27 | + { |
| 28 | + $condition = (new Condition())->not('TopicA'); |
| 29 | + |
| 30 | + $this->assertSame("!('TopicA' in topics)", $condition); |
| 31 | + } |
| 32 | + |
| 33 | + public function testConditionAndWithClosure(): void |
| 34 | + { |
| 35 | + $condition = (new Condition())->and('TopicA', static function () { |
| 36 | + return (new Condition())->or('TopicB', 'TopicC'); |
| 37 | + }); |
| 38 | + |
| 39 | + $this->assertSame("'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)", $condition); |
| 40 | + } |
| 41 | + |
| 42 | + public function testConditionOrWithClosure(): void |
| 43 | + { |
| 44 | + $condition = (new Condition())->or('TopicA', static function () { |
| 45 | + return (new Condition())->and('TopicB', 'TopicC'); |
| 46 | + }); |
| 47 | + |
| 48 | + $this->assertSame("'TopicA' in topics || ('TopicB' in topics && 'TopicC' in topics)", $condition); |
| 49 | + } |
| 50 | + |
| 51 | + public function testConditionNotWithClosure(): void |
| 52 | + { |
| 53 | + $condition = (new Condition())->not(static function () { |
| 54 | + return (new Condition())->and('TopicA', 'TopicB'); |
| 55 | + }); |
| 56 | + |
| 57 | + $this->assertSame("!('TopicA' in topics && 'TopicB' in topics)", $condition); |
| 58 | + } |
| 59 | + |
| 60 | + public function testConditionWithMultipleClosure(): void |
| 61 | + { |
| 62 | + $condition = (new Condition())->and('TopicA', static function () { |
| 63 | + return (new Condition())->or('TopicB', static function () { |
| 64 | + return (new Condition())->and('TopicC', 'TopicD', static function () { |
| 65 | + return (new Condition())->not('TopicE'); |
| 66 | + }); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + $this->assertSame("'TopicA' in topics && ('TopicB' in topics || ('TopicC' in topics && 'TopicD' in topics && (!('TopicE' in topics))))", $condition); |
| 71 | + } |
| 72 | + |
| 73 | + public function testConditionAndWithInvalidTopics(): void |
| 74 | + { |
| 75 | + $this->expectException(\InvalidArgumentException::class); |
| 76 | + $this->expectExceptionMessage('"Topic A" is an invalid topic name.'); |
| 77 | + |
| 78 | + (new Condition())->and('Topic A', 'TopicB', 'TopicC'); |
| 79 | + } |
| 80 | +} |
0 commit comments