1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
<?php
/**
* PHP version 7.1.1
* @author Hannes Kindströmmer <hannes@kindstrommer.se>
* @copyright 2017 iP.1 Networks AB
* @license https://www.gnu.org/licenses/lgpl-3.0.txt LGPL-3.0
* @version 0.3.0-beta
* @since File available since Release 0.3.0-beta
* @link http://api.ip1sms.com/Help
* @link https://github.com/iP1SMS/ip1-php-sdk
*/
namespace IP1\RESTClient\Test\Recipient;
use IP1\RESTClient\Recipient\Group;
use IP1\RESTClient\Recipient\Contact;
use IP1\RESTClient\Recipient\Membership;
use IP1\RESTClient\Test\Core\AbstractEnviromentProvider;
use IP1\RESTClient\Test\Util\Util;
use IP1\RESTClient\Core\ClassValidationArray;
class MembershipTest extends AbstractEnviromentProvider
{
/**
* @dataProvider getMembershipArguments
*/
public function testConstructor($groupID, $contactID)
{
$m = new Membership($groupID, $contactID);
$this->assertEquals($groupID, $m->getGroupID());
$this->assertEquals($contactID, $m->getContactID());
}
public function getMembershipArguments(): array
{
$argumentArray = [];
for ($i = 0; $i < 50; $i++) {
$argumentArray[] = [random_int(0, PHP_INT_MAX), random_int(0, PHP_INT_MAX)];
}
return $argumentArray;
}
/**
* @group api
*/
public function testAPI()
{
$contacts = [];
$groups = [];
$memberships = [];
for ($i = 0; $i < 10; $i++) {
$c = new Contact(Util::getRandomAlphaString(), Util::getRandomPhoneNumber());
$contacts[] = $this->getCommunicator()->add($c);
$g = new Group(Util::getRandomAlphaString(), Util::getRandomHex());
$groups[] = $this->getCommunicator()->add($g);
$m = new Membership($groups[$i]->getID(), $contacts[$i]->getID());
$memberships[] = $this->getCommunicator()->add($m);
}
$this->assertEquals(10, count($contacts));
$this->assertEquals(10, count($groups));
$this->assertEquals(10, count($memberships));
for ($i = 0; $i < count($memberships); $i++) {
$contactMemberships = $contacts[$i]->getMemberships($this->getCommunicator());
$this->assertEquals(1, count($contactMemberships));
$this->assertEquals($contactMemberships[0]->getContactID(), $contacts[$i]->getID());
$this->assertEquals($contactMemberships[0]->getGroupID(), $groups[$i]->getID());
$contactGroups = $contacts[$i]->getGroups($this->getCommunicator());
$this->assertEquals(1, count($contactGroups));
$this->assertEquals(new ClassValidationArray($groups[$i]), $contactGroups);
}
}
public function tearDown()
{
if ($this->isCommunicatorEnabled()) {
$memberships = json_decode($this->getCommunicator()->get('api/memberships'));
foreach ($memberships as $key => $value) {
$this->getCommunicator()->delete('api/memberships/'.$value->ID);
}
$contacts = json_decode($this->getCommunicator()->get('api/memberships'));
foreach ($contacts as $key => $value) {
$this->getCommunicator()->delete('api/memberships/'.$value->ID);
}
$groups = json_decode($this->getCommunicator()->get('api/groups'));
foreach ($groups as $key => $value) {
$this->getCommunicator()->delete('api/groups/'.$value->ID);
}
}
}
}
|