|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OneSignal\Tests\Resolver; |
| 6 | + |
| 7 | +use OneSignal\Resolver\AppOutcomesResolver; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
| 10 | +use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; |
| 11 | +use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException; |
| 12 | + |
| 13 | +class AppOutcomeResolverTest extends TestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var AppOutcomesResolver |
| 17 | + */ |
| 18 | + private $appResolver; |
| 19 | + |
| 20 | + protected function setUp(): void |
| 21 | + { |
| 22 | + $this->appResolver = new AppOutcomesResolver(); |
| 23 | + } |
| 24 | + |
| 25 | + public function testResolveWithValidValues(): void |
| 26 | + { |
| 27 | + $expectedData = [ |
| 28 | + 'outcome_names' => [ |
| 29 | + 'os__session_duration.count', |
| 30 | + 'os__click.count', |
| 31 | + ], |
| 32 | + 'outcome_time_range' => '1mo', |
| 33 | + 'outcome_platforms' => [0, 1, 2], |
| 34 | + 'outcome_attribution' => 'direct', |
| 35 | + ]; |
| 36 | + |
| 37 | + self::assertEquals( |
| 38 | + array_merge($expectedData, [ |
| 39 | + 'outcome_platforms' => '0,1,2', |
| 40 | + ]), |
| 41 | + $this->appResolver->resolve($expectedData) |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + public function testResolveWithMissingRequiredValue(): void |
| 46 | + { |
| 47 | + $this->expectException(MissingOptionsException::class); |
| 48 | + |
| 49 | + $this->appResolver->resolve([]); |
| 50 | + } |
| 51 | + |
| 52 | + public function wrongValueTypesProvider(): iterable |
| 53 | + { |
| 54 | + yield [['outcome_names' => 100]]; |
| 55 | + yield [['outcome_names' => [1, 2]]]; |
| 56 | + yield [['outcome_time_range' => 1]]; |
| 57 | + yield [['outcome_time_range' => '2d']]; |
| 58 | + yield [['outcome_platforms' => 0]]; |
| 59 | + yield [['outcome_platforms' => ['0']]]; |
| 60 | + yield [['outcome_platforms' => [100]]]; |
| 61 | + yield [['outcome_attribution' => []]]; |
| 62 | + yield [['outcome_attribution' => 'indirect']]; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @dataProvider wrongValueTypesProvider |
| 67 | + */ |
| 68 | + public function testResolveWithWrongValueTypes(array $wrongOption): void |
| 69 | + { |
| 70 | + $this->expectException(InvalidOptionsException::class); |
| 71 | + |
| 72 | + $requiredOptions = [ |
| 73 | + 'outcome_names' => ['os__click.count'], |
| 74 | + ]; |
| 75 | + |
| 76 | + $this->appResolver->resolve(array_merge($requiredOptions, $wrongOption)); |
| 77 | + } |
| 78 | + |
| 79 | + public function testResolveWithWrongOption(): void |
| 80 | + { |
| 81 | + $this->expectException(UndefinedOptionsException::class); |
| 82 | + |
| 83 | + $this->appResolver->resolve(['wrongOption' => 'wrongValue']); |
| 84 | + } |
| 85 | +} |
0 commit comments