Skip to content

Commit c2e23de

Browse files
committed
Added serialize / unserialize to client:
- basically proxies the verification object's serialization support - sets the client, so active methods (check, cancel, trigger, sync) work - TODO: make main client aware of serialization so can use that instead of native functions
1 parent ea2e25f commit c2e23de

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/Verify/Client.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,25 @@ public function check($verification, $code, $ip = null)
104104
return $this->checkError($verification, $data);
105105
}
106106

107+
public function serialize(Verification $verification)
108+
{
109+
return serialize($verification);
110+
}
111+
112+
public function unserialize($verification)
113+
{
114+
if(is_string($verification)){
115+
$verification = unserialize($verification);
116+
}
117+
118+
if(!($verification instanceof Verification)){
119+
throw new \InvalidArgumentException('expected verification object or serialize verification object');
120+
}
121+
122+
$verification->setClient($this);
123+
return $verification;
124+
}
125+
107126
protected function control($verification, $cmd)
108127
{
109128
if(!($verification instanceof Verification)){

test/Verify/ClientTest.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,41 @@ public function getApiMethods()
6969
];
7070
}
7171

72+
public function testUnserializeAcceptsObject()
73+
{
74+
$mock = $this->getMockBuilder('Nexmo\Verify\Verification')
75+
->setConstructorArgs(['14845551212', 'Test Verify'])
76+
->setMethods(['setClient'])
77+
->getMock();
78+
79+
$mock->expects($this->once())->method('setClient')->with($this->client);
80+
81+
$this->client->unserialize($mock);
82+
}
83+
84+
public function testUnserializeSetsClient()
85+
{
86+
$verification = new Verification('14845551212', 'Test Verify');
87+
$verification->setResponse($this->getResponse('start'));
88+
89+
$string = serialize($verification);
90+
$object = $this->client->unserialize($string);
91+
92+
$this->assertInstanceOf('Nexmo\Verify\Verification', $object);
93+
94+
$search = $this->setupClientForSearch('search');
95+
$object->sync();
96+
$this->assertSame($search, $object->getResponse());
97+
}
98+
99+
public function testSerializeMatchesEntity()
100+
{
101+
$verification = new Verification('14845551212', 'Test Verify');
102+
$verification->setResponse($this->getResponse('start'));
103+
104+
$string = serialize($verification);
105+
$this->assertSame($string, $this->client->serialize($verification));
106+
}
72107

73108
public function testCanStartVerification()
74109
{
@@ -402,7 +437,7 @@ public function testCheckNotReplaceResponse()
402437
$this->client->check($verification, '1234');
403438
$this->assertSame($old, $verification->getResponse());
404439
}
405-
440+
406441
protected function setupClientForCheck($response, $code, $ip = null)
407442
{
408443
$response = $this->getResponse($response);

0 commit comments

Comments
 (0)