-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
117 additions
and
5 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,92 @@ | ||
<?php | ||
/** | ||
* Nucleus - XMPP Library for PHP | ||
* | ||
* Copyright (C) 2017, Some rights reserved. | ||
* | ||
* @author Kacper "Kadet" Donat <[email protected]> | ||
* | ||
* Contact with author: | ||
* Xmpp: [email protected] | ||
* E-mail: [email protected] | ||
* | ||
* From Kadet with love. | ||
*/ | ||
|
||
namespace Kadet\Xmpp\Tests\Modules; | ||
|
||
|
||
use Kadet\Xmpp\Component\PingKeepAlive; | ||
use Kadet\Xmpp\Jid; | ||
use Kadet\Xmpp\Stanza\Iq; | ||
use Kadet\Xmpp\Stanza\Iq\Query; | ||
use Kadet\Xmpp\Tests\Stubs\ConnectorStub; | ||
use Kadet\Xmpp\XmppClient; | ||
use React\EventLoop\LoopInterface; | ||
|
||
|
||
class PingKeepAliveTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testKeepAliveTimer() | ||
{ | ||
/** @var LoopInterface|\PHPUnit_Framework_MockObject_MockObject $loop */ | ||
$loop = $this->getMockBuilder(LoopInterface::class)->setMethods(['addPeriodicTimer'])->getMockForAbstractClass(); | ||
$loop->expects($this->once())->method('addPeriodicTimer')->withConsecutive($this->greaterThan(1.0), $this->callback(function($callback) { | ||
return is_callable($callback); | ||
})); | ||
|
||
$this->getMockClient($loop)->emit('state', ['ready']); | ||
} | ||
|
||
public function testKeepAliveTick() | ||
{ | ||
/** @var LoopInterface|\PHPUnit_Framework_MockObject_MockObject $loop */ | ||
$loop = $this->getMockBuilder(LoopInterface::class)->setMethods(['addPeriodicTimer'])->getMockForAbstractClass(); | ||
|
||
/** @var callable $tick */ | ||
$tick = null; | ||
$loop->method('addPeriodicTimer')->willReturnCallback(function($_, $callback) use(&$tick) { | ||
$tick = $callback; | ||
}); | ||
|
||
$client = $this->getMockClient($loop); | ||
$client->expects($this->once())->method('write')->withConsecutive($this->callback( | ||
\Kadet\Xmpp\Utils\filter\iq\query(\Kadet\Xmpp\Utils\filter\element('ping', 'urn:xmpp:ping')) | ||
)); | ||
|
||
$client->emit('state', ['ready']); | ||
|
||
$tick(); | ||
} | ||
|
||
public function testPingResponse() | ||
{ | ||
$iq = new Iq('get', ['id' => 'testme', 'query' => new Query('urn:xmpp:ping', 'ping')]); | ||
|
||
$client = $this->getMockClient(); | ||
$client->expects($this->once())->method('write')->withConsecutive(\Kadet\Xmpp\Utils\filter\all( | ||
\Kadet\Xmpp\Utils\filter\stanza\type('result'), | ||
\Kadet\Xmpp\Utils\filter\stanza\id('testme'), | ||
\Kadet\Xmpp\Utils\filter\iq\query(\Kadet\Xmpp\Utils\filter\element('ping', 'urn:xmpp:ping')) | ||
)); | ||
|
||
$client->emit('element', [ $iq ]); | ||
} | ||
|
||
/** | ||
* @param LoopInterface $loop | ||
* @return XmppClient|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private function getMockClient(LoopInterface $loop = null) | ||
{ | ||
return $this->getMockBuilder(XmppClient::class) | ||
->setConstructorArgs([new Jid('[email protected]'), [ | ||
'connector' => new ConnectorStub(null, $loop), | ||
'default-modules' => false, | ||
'modules' => [ | ||
new PingKeepAlive() | ||
] | ||
]])->setMethods(['write']) | ||
->getMock(); | ||
} | ||
} |
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