public function testSocketClient() { $thriftClient = $this->createSocketServer(); $this->assertInstanceOf('ThriftModel\\Test\\Test', $thriftClient->getFactory('ThriftModel\\Test\\Test')); //$pid = pcntl_fork(); $handler = new TestHandler($this->factory); $processor = $this->factory->getProcessorInstance('test', $handler); $this->assertInstanceOf('ThriftModel\\Test\\TestServiceProcessor', $processor); $transport = new TServerSocket('localhost', 9090); $outputTransportFactory = $inputTransportFactory = new TTransportFactory($transport); $outputProtocolFactory = $inputProtocolFactory = new TBinaryProtocolFactory(); $server = new TSimpleServer($processor, $transport, $inputTransportFactory, $outputTransportFactory, $inputProtocolFactory, $outputProtocolFactory); $pid = pcntl_fork(); if ($pid > 0) { //Wait for the server to launch sleep(2); $client = $thriftClient->getClient(); $this->assertInstanceOf('ThriftModel\\Test\\TestServiceClient', $client); // Void Method $this->assertNull($client->ping()); // Return test object $test = $client->get(1); $this->assertInstanceOf('ThriftModel\\Test\\Test', $test); $this->assertEquals(1, $test->id); $this->assertEquals('TEST', $test->content); // Wrong ID return Exception try { $response = $client->get(-1); } catch (\Exception $e) { $this->assertInstanceOf('ThriftModel\\Test\\InvalidValueException', $e); } // Return array of test object $test = $client->getList(1); $this->assertInternalType('array', $test); $this->assertCount(2, $test); $this->assertContainsOnly('ThriftModel\\Test\\Test', $test); $this->assertEquals(1, $test[0]->id); $this->assertEquals('TEST', $test[0]->content); $this->assertEquals(1, $test[1]->id); $this->assertEquals('TEST2', $test[1]->content); // Wrong ID return Exception try { $response = $client->getList(-1); } catch (\Exception $e) { $this->assertInstanceOf('ThriftModel\\Test\\InvalidValueException', $e); } //Create test $testObject = $this->factory->getInstance('ThriftModel\\Test\\Test', array('content' => 'OK')); $test = $client->create($testObject); $this->assertTrue((bool) $test); $testObject->content = ''; // Wrong ID return Exception try { $response = $client->create($testObject); } catch (\Exception $e) { $this->assertInstanceOf('ThriftModel\\Test\\InvalidValueException', $e); } posix_kill($pid, SIGTERM); pcntl_wait($status, WNOHANG); } elseif ($pid === 0) { $server->serve(); $transport->close(); posix_kill($pid, SIGTERM); exit(0); } }
use Thrift\Protocol\TBinaryProtocol; use Thrift\Server\TServerSocket; use Thrift\Server\TSimpleServer; use Thrift\Transport\TPhpStream; use Thrift\Transport\TBufferedTransport; use Thrift\Transport\TSocket; class ExampleService implements \Example\ExampleServiceIf { /** * @return string */ public function ping() { return 'ping method called '; } /** * @param \Example\AuthenticateRequest $authenticateRequest * @return \Example\AuthenticateResponse * @throws \Example\ServerError\ServerError */ public function authenticate(\Example\AuthenticateRequest $authenticateRequest) { // TODO: Implement authenticate() method. } } $handler = new ExampleService(); $processor = new \Example\ExampleServiceProcessor($handler); $transport = new TServerSocket(); $server = new TSimpleServer($processor, $transport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocolFactory(), new TBinaryProtocolFactory()); $server->serve();