|
| 1 | +<?php |
| 2 | + |
| 3 | +use Clue\React\Ami\Factory; |
| 4 | +use React\Promise\PromiseInterface; |
| 5 | +use Clue\React\Ami\Client; |
| 6 | +use Clue\React\Ami\Api; |
| 7 | +use Clue\React\Ami\Protocol\Response; |
| 8 | + |
| 9 | +class FunctionalTest extends TestCase |
| 10 | +{ |
| 11 | + private static $address = false; |
| 12 | + private static $loop; |
| 13 | + |
| 14 | + public static function setUpBeforeClass() |
| 15 | + { |
| 16 | + self::$address = getenv('LOGIN'); |
| 17 | + self::$loop = React\EventLoop\Factory::create(); |
| 18 | + } |
| 19 | + |
| 20 | + public function setUp() |
| 21 | + { |
| 22 | + if (self::$address === false) { |
| 23 | + $this->markTestSkipped('No ENV named LOGIN found. Please use "export LOGIN=\'user:pass@host\'.'); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + public function testConnection() |
| 28 | + { |
| 29 | + $factory = new Factory(self::$loop); |
| 30 | + |
| 31 | + $client = $this->waitFor($factory->createClient(self::$address)); |
| 32 | + /* @var $client Client */ |
| 33 | + |
| 34 | + $this->assertFalse($client->isBusy()); |
| 35 | + |
| 36 | + return $client; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @depends testConnection |
| 41 | + * @param Client $client |
| 42 | + */ |
| 43 | + public function testPing(Client $client) |
| 44 | + { |
| 45 | + $api = new Api($client); |
| 46 | + |
| 47 | + $pong = $this->waitFor($api->ping()); |
| 48 | + /* @var $pong Response */ |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @depends testConnection |
| 53 | + * @param Client $client |
| 54 | + * @expectedException Exception |
| 55 | + */ |
| 56 | + public function testInvalidCommandGetsRejected(Client $client) |
| 57 | + { |
| 58 | + $this->waitFor($client->request($client->createAction('Invalid'))); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @depends testConnection |
| 63 | + * @param Client $client |
| 64 | + */ |
| 65 | + public function testApiLogoffDisconnects(Client $client) |
| 66 | + { |
| 67 | + $api = new Api($client); |
| 68 | + |
| 69 | + $ret = $this->waitFor($api->logoff()); |
| 70 | + /* @var $ret Response */ |
| 71 | + |
| 72 | + $this->assertFalse($client->isBusy()); |
| 73 | + |
| 74 | + //$client->on('close', $this->expectCallableOnce()); |
| 75 | + |
| 76 | + self::$loop->run(); |
| 77 | + |
| 78 | + return $client; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @depends testApiLogoffDisconnects |
| 83 | + * @param Client $client |
| 84 | + * @expectedException Exception |
| 85 | + */ |
| 86 | + public function testSendRejectedAfterClose(Client $client) |
| 87 | + { |
| 88 | + $this->waitFor($client->request($client->createAction('Ping'))); |
| 89 | + } |
| 90 | + |
| 91 | + private function waitFor(PromiseInterface $promise) |
| 92 | + { |
| 93 | + $resolved = null; |
| 94 | + $exception = null; |
| 95 | + |
| 96 | + $promise->then(function ($c) use (&$resolved) { |
| 97 | + $resolved = $c; |
| 98 | + }, function($error) use (&$exception) { |
| 99 | + $exception = $error; |
| 100 | + }); |
| 101 | + |
| 102 | + while ($resolved === null && $exception === null) { |
| 103 | + self::$loop->tick(); |
| 104 | + } |
| 105 | + |
| 106 | + if ($exception !== null) { |
| 107 | + throw $exception; |
| 108 | + } |
| 109 | + |
| 110 | + return $resolved; |
| 111 | + } |
| 112 | +} |
0 commit comments