-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from norkunas/new-apis
Add new api endpoints
- Loading branch information
Showing
10 changed files
with
323 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace OneSignal\Resolver; | ||
|
||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
class NotificationHistoryResolver implements ResolverInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolve(array $data) | ||
{ | ||
return (new OptionsResolver()) | ||
->setRequired('events') | ||
->setAllowedTypes('events', 'string') | ||
->setAllowedValues('events', ['sent', 'clicked']) | ||
->setRequired('email') | ||
->setAllowedTypes('email', 'string') | ||
->resolve($data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace OneSignal\Resolver; | ||
|
||
use Symfony\Component\OptionsResolver\Options; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
class SegmentResolver implements ResolverInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolve(array $data) | ||
{ | ||
return (new OptionsResolver()) | ||
->setDefined('id') | ||
->setAllowedTypes('id', 'string') | ||
->setRequired('name') | ||
->setAllowedTypes('name', 'string') | ||
->setDefined('filters') | ||
->setAllowedTypes('filters', 'array') | ||
->setNormalizer('filters', function (Options $options, array $values) { | ||
return $this->normalizeFilters($options, $values); | ||
}) | ||
->resolve($data); | ||
} | ||
|
||
private function normalizeFilters(Options $options, array $values) | ||
{ | ||
$filters = []; | ||
|
||
foreach ($values as $filter) { | ||
if (isset($filter['field']) || isset($filter['operator'])) { | ||
$filters[] = $filter; | ||
} | ||
} | ||
|
||
return $filters; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
tests/OneSignal/Tests/Resolver/NotificationHistoryResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace OneSignal\Tests\Resolver; | ||
|
||
use OneSignal\Resolver\NotificationHistoryResolver; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class NotificationHistoryResolverTest extends TestCase | ||
{ | ||
/** | ||
* @var NotificationHistoryResolver | ||
*/ | ||
private $notificationHistoryResolver; | ||
|
||
public function setUp() | ||
{ | ||
$this->notificationHistoryResolver = new NotificationHistoryResolver(); | ||
} | ||
|
||
public function testResolveWithValidValues() | ||
{ | ||
$expectedData = [ | ||
'events' => 'sent', | ||
'email' => 'example@example.com', | ||
]; | ||
|
||
$this->assertEquals($expectedData, $this->notificationHistoryResolver->resolve($expectedData)); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException | ||
*/ | ||
public function testResolveWithMissingRequiredValue() | ||
{ | ||
$this->notificationHistoryResolver->resolve([]); | ||
} | ||
|
||
public function wrongValueTypesProvider() | ||
{ | ||
return [ | ||
[['events' => 666, 'email' => 'example@example.com']], | ||
[['events' => 'sent', 'email' => 666]], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider wrongValueTypesProvider | ||
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException | ||
*/ | ||
public function testResolveWithWrongValueTypes($wrongOption) | ||
{ | ||
$this->notificationHistoryResolver->resolve($wrongOption); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException | ||
*/ | ||
public function testResolveWithWrongOption() | ||
{ | ||
$this->notificationHistoryResolver->resolve(['events' => 'sent', 'wrongOption' => 'wrongValue']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace OneSignal\Tests\Resolver; | ||
|
||
use OneSignal\Resolver\SegmentResolver; | ||
use OneSignal\Tests\PrivateAccessorTrait; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
class SegmentResolverTest extends TestCase | ||
{ | ||
use PrivateAccessorTrait; | ||
|
||
/** | ||
* @var SegmentResolver | ||
*/ | ||
private $segmentResolver; | ||
|
||
public function setUp() | ||
{ | ||
$this->segmentResolver = new SegmentResolver(); | ||
} | ||
|
||
public function testResolveWithValidValues() | ||
{ | ||
$expectedData = [ | ||
'id' => '52d5a7cb-59fe-4d0c-a0b9-9a39a21475ad', | ||
'name' => 'Custom Segment', | ||
'filters' => [], | ||
]; | ||
|
||
$this->assertEquals($expectedData, $this->segmentResolver->resolve($expectedData)); | ||
} | ||
|
||
public function wrongValueTypesProvider() | ||
{ | ||
return [ | ||
[['id' => 666, 'name' => '']], | ||
[['name' => 666]], | ||
[['filters' => 666, 'name' => '']], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider wrongValueTypesProvider | ||
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException | ||
*/ | ||
public function testResolveWithWrongValueTypes($wrongOption) | ||
{ | ||
$this->segmentResolver->resolve($wrongOption); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException | ||
*/ | ||
public function testResolveWithWrongOption() | ||
{ | ||
$this->segmentResolver->resolve(['wrongOption' => 'wrongValue']); | ||
} | ||
|
||
/****** Private functions testing ******/ | ||
|
||
public function testNormalizeFilters() | ||
{ | ||
$method = $this->getPrivateMethod(SegmentResolver::class, 'normalizeFilters'); | ||
|
||
$inputData = [ | ||
new OptionsResolver(), | ||
[ | ||
['wrongField' => 'wrongValue'], | ||
['field' => 'session_count', 'relation' => '>', 'value' => '1'], | ||
['operator' => 'AND'], | ||
['field' => 'tag', 'relation' => '!=', 'key' => 'tag_key', 'value' => '1'], | ||
['operator' => 'OR'], | ||
['field' => 'last_session', 'relation' => '<', 'value' => '30'], | ||
], | ||
]; | ||
|
||
$expectedData = | ||
[ | ||
['field' => 'session_count', 'relation' => '>', 'value' => '1'], | ||
['operator' => 'AND'], | ||
['field' => 'tag', 'relation' => '!=', 'key' => 'tag_key', 'value' => '1'], | ||
['operator' => 'OR'], | ||
['field' => 'last_session', 'relation' => '<', 'value' => '30'], | ||
]; | ||
|
||
$this->assertEquals($expectedData, $method->invokeArgs($this->segmentResolver, $inputData)); | ||
} | ||
} |