|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OneSignal\Tests\Resolver; |
| 4 | + |
| 5 | +use OneSignal\Resolver\SegmentResolver; |
| 6 | +use OneSignal\Tests\PrivateAccessorTrait; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 9 | + |
| 10 | +class SegmentResolverTest extends TestCase |
| 11 | +{ |
| 12 | + use PrivateAccessorTrait; |
| 13 | + |
| 14 | + /** |
| 15 | + * @var SegmentResolver |
| 16 | + */ |
| 17 | + private $segmentResolver; |
| 18 | + |
| 19 | + public function setUp() |
| 20 | + { |
| 21 | + $this->segmentResolver = new SegmentResolver(); |
| 22 | + } |
| 23 | + |
| 24 | + public function testResolveWithValidValues() |
| 25 | + { |
| 26 | + $expectedData = [ |
| 27 | + 'id' => '52d5a7cb-59fe-4d0c-a0b9-9a39a21475ad', |
| 28 | + 'name' => 'Custom Segment', |
| 29 | + 'filters' => [], |
| 30 | + ]; |
| 31 | + |
| 32 | + $this->assertEquals($expectedData, $this->segmentResolver->resolve($expectedData)); |
| 33 | + } |
| 34 | + |
| 35 | + public function wrongValueTypesProvider() |
| 36 | + { |
| 37 | + return [ |
| 38 | + [['id' => 666, 'name' => '']], |
| 39 | + [['name' => 666]], |
| 40 | + [['filters' => 666, 'name' => '']], |
| 41 | + ]; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @dataProvider wrongValueTypesProvider |
| 46 | + * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException |
| 47 | + */ |
| 48 | + public function testResolveWithWrongValueTypes($wrongOption) |
| 49 | + { |
| 50 | + $this->segmentResolver->resolve($wrongOption); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException |
| 55 | + */ |
| 56 | + public function testResolveWithWrongOption() |
| 57 | + { |
| 58 | + $this->segmentResolver->resolve(['wrongOption' => 'wrongValue']); |
| 59 | + } |
| 60 | + |
| 61 | + /****** Private functions testing ******/ |
| 62 | + |
| 63 | + public function testNormalizeFilters() |
| 64 | + { |
| 65 | + $method = $this->getPrivateMethod(SegmentResolver::class, 'normalizeFilters'); |
| 66 | + |
| 67 | + $inputData = [ |
| 68 | + new OptionsResolver(), |
| 69 | + [ |
| 70 | + ['wrongField' => 'wrongValue'], |
| 71 | + ['field' => 'session_count', 'relation' => '>', 'value' => '1'], |
| 72 | + ['operator' => 'AND'], |
| 73 | + ['field' => 'tag', 'relation' => '!=', 'key' => 'tag_key', 'value' => '1'], |
| 74 | + ['operator' => 'OR'], |
| 75 | + ['field' => 'last_session', 'relation' => '<', 'value' => '30'], |
| 76 | + ], |
| 77 | + ]; |
| 78 | + |
| 79 | + $expectedData = |
| 80 | + [ |
| 81 | + ['field' => 'session_count', 'relation' => '>', 'value' => '1'], |
| 82 | + ['operator' => 'AND'], |
| 83 | + ['field' => 'tag', 'relation' => '!=', 'key' => 'tag_key', 'value' => '1'], |
| 84 | + ['operator' => 'OR'], |
| 85 | + ['field' => 'last_session', 'relation' => '<', 'value' => '30'], |
| 86 | + ]; |
| 87 | + |
| 88 | + $this->assertEquals($expectedData, $method->invokeArgs($this->segmentResolver, $inputData)); |
| 89 | + } |
| 90 | +} |
0 commit comments