-
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
7 changed files
with
220 additions
and
7 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,85 @@ | ||
<?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\Component\Subscription; | ||
|
||
|
||
use Kadet\Xmpp\Component\Component; | ||
use Kadet\Xmpp\Jid; | ||
use Kadet\Xmpp\Stanza\Presence; | ||
use Kadet\Xmpp\Utils\BetterEmitter; | ||
|
||
use function \Kadet\Xmpp\Utils\filter\{ | ||
stanza\type, in | ||
}; | ||
|
||
class SubscriptionManager extends Component | ||
{ | ||
use BetterEmitter; | ||
|
||
protected function init() | ||
{ | ||
$this->_client->on('presence', function(...$args) { | ||
$this->handleSubscriptionRequest(...$args); | ||
}, type('subscribe')); | ||
} | ||
|
||
private function handleSubscriptionRequest(Presence $presence) | ||
{ | ||
$this->emit('request', [ $presence ]); | ||
} | ||
|
||
/** | ||
* Sends subscription request presence to server. | ||
* | ||
* @param Jid|string $jid | ||
* @return \React\Promise\ExtendedPromiseInterface | ||
*/ | ||
public function subscribe($jid) | ||
{ | ||
return $this->_client->send($this->presence('subscribe', $jid)); | ||
} | ||
|
||
/** | ||
* Sends subscription removal request presence to server. | ||
* | ||
* @param Jid|string $jid | ||
* @return \React\Promise\ExtendedPromiseInterface | ||
*/ | ||
public function unsubscribe(Jid $jid) | ||
{ | ||
return $this->_client->send($this->presence('unsubscribe', $jid)); | ||
} | ||
|
||
/** | ||
* Sends subscription cancellation request presence to server. | ||
* | ||
* @param Jid|string $jid | ||
* @return \React\Promise\ExtendedPromiseInterface | ||
*/ | ||
public function cancel($jid) | ||
{ | ||
return $this->_client->send($this->presence('unsubscribed', $jid)); | ||
} | ||
|
||
private function presence($type, $jid) | ||
{ | ||
$jid = $jid instanceof Jid ? $jid : new Jid($jid); | ||
return new Presence([ | ||
'type' => $type, | ||
'to' => $jid->bare() | ||
]); | ||
} | ||
} |
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,113 @@ | ||
<?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\Subscription; | ||
|
||
|
||
use Kadet\Xmpp\Component\Subscription\SubscriptionManager; | ||
use Kadet\Xmpp\Jid; | ||
use Kadet\Xmpp\Stanza\Presence; | ||
use Kadet\Xmpp\Tests\Stubs\ConnectorStub; | ||
use Kadet\Xmpp\XmppClient; | ||
use PHPUnit_Framework_MockObject_MockObject as Mock; | ||
|
||
use function Kadet\Xmpp\Utils\filter\{ | ||
all, not, pass | ||
}; | ||
use function Kadet\Xmpp\Utils\filter\stanza\{ | ||
to, type, id | ||
}; | ||
|
||
class SubscriptionManagerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var XmppClient|Mock | ||
*/ | ||
private $_client; | ||
|
||
/** | ||
* @var SubscriptionManager | ||
*/ | ||
private $_manager; | ||
|
||
/** | ||
* @return XmppClient|Mock | ||
*/ | ||
public function getMockClient() | ||
{ | ||
return $this->getMockBuilder(XmppClient::class) | ||
->setConstructorArgs([new Jid('local@domain'), [ | ||
'connector' => new ConnectorStub(), | ||
'default-modules' => false, | ||
'modules' => [ | ||
$this->_manager = new SubscriptionManager() | ||
] | ||
]])->setMethods(['write']) | ||
->getMock(); | ||
} | ||
|
||
protected function setUp() | ||
{ | ||
$this->_client = $this->getMockClient(); | ||
} | ||
|
||
public function testSubscriptionRequest() | ||
{ | ||
$jid = new Jid('[email protected]'); | ||
|
||
$this->_client->expects($this->once())->method('write')->withConsecutive($this->callback(all( | ||
type('subscribe'), id(not(null)), to((string)$jid->bare()) | ||
))); | ||
|
||
$this->_manager->subscribe($jid); | ||
} | ||
|
||
|
||
public function testSubscriptionRequestEvent() | ||
{ | ||
$jid = new Jid('[email protected]'); | ||
|
||
$presence = new Presence(['type' => 'subscribe', 'from' => $jid]); | ||
|
||
$mock = $this->getMockBuilder('stdClass')->setMethods(['predicate'])->getMock(); | ||
$mock->expects($this->once())->method('predicate')->withConsecutive($presence); | ||
|
||
$this->_manager->on('request', [$mock, 'predicate']); | ||
$this->_client->emit('element', [ $presence ]); | ||
} | ||
|
||
public function testSubscriptionRemovalRequest() | ||
{ | ||
$jid = new Jid('[email protected]'); | ||
|
||
$this->_client->expects($this->once())->method('write')->withConsecutive($this->callback(all( | ||
type('unsubscribe'), id(not(null)), to((string)$jid->bare()) | ||
))); | ||
|
||
$this->_manager->unsubscribe($jid); | ||
} | ||
|
||
|
||
public function testSubscriptionCancellation() | ||
{ | ||
$jid = new Jid('[email protected]'); | ||
|
||
$this->_client->expects($this->once())->method('write')->withConsecutive($this->callback(all( | ||
type('unsubscribed'), id(not(null)), to((string)$jid->bare()) | ||
))); | ||
|
||
$this->_manager->cancel($jid); | ||
} | ||
} |
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