public function write(RecordInterface $record) { $xmlWriter = new XMLWriter(); $xmlWriter->openMemory(); $xmlWriter->setIndent(true); $xmlWriter->startDocument('1.0', 'UTF-8'); $xmlWriter->startElement('soap:Envelope'); $xmlWriter->writeAttribute('xmlns:soap', 'http://schemas.xmlsoap.org/soap/envelope/'); if ($record instanceof ExceptionRecord) { $xmlWriter->startElement('soap:Body'); $xmlWriter->startElement('soap:Fault'); $xmlWriter->writeElement('faultcode', 'soap:Server'); $xmlWriter->writeElement('faultstring', $record->getMessage()); if ($record->getTrace()) { $xmlWriter->startElement('detail'); $graph = new GraphTraverser(); $graph->traverse($record, new Visitor\XmlWriterVisitor($xmlWriter, $this->namespace)); $xmlWriter->endElement(); } $xmlWriter->endElement(); $xmlWriter->endElement(); } else { $xmlWriter->startElement('soap:Body'); $record = new Record($this->requestMethod . 'Response', $record->getRecordInfo()->getFields()); $graph = new GraphTraverser(); $graph->traverse($record, new Visitor\XmlWriterVisitor($xmlWriter, $this->namespace)); $xmlWriter->endElement(); } $xmlWriter->endElement(); $xmlWriter->endDocument(); return $xmlWriter->outputMemory(); }
public function testHandle() { // channel $channel = $this->getMock('PhpAmqpLib\\Channel\\AMQPChannel', ['basic_publish'], [], '', false); $channel->expects($this->once())->method('basic_publish')->with($this->callback(function ($message) { /** @var \PhpAmqpLib\Message\AMQPMessage $message */ $this->assertInstanceOf('PhpAmqpLib\\Message\\AMQPMessage', $message); $this->assertEquals(['content_type' => 'application/json', 'delivery_mode' => 2], $message->get_properties()); $this->assertJsonStringEqualsJsonString('{"foo": "bar"}', $message->body); return true; }), $this->equalTo(''), $this->equalTo('foo')); // connection $connection = $this->getMock('PhpAmqpLib\\Connection\\AMQPStreamConnection', ['channel'], [], '', false); $connection->expects($this->once())->method('channel')->will($this->returnValue($channel)); // connector $connector = $this->getMock('Fusio\\Engine\\ConnectorInterface', ['getConnection'], [], '', false); $connector->expects($this->once())->method('getConnection')->with($this->equalTo(1))->will($this->returnValue($connection)); $action = new MqAmqp(); $action->setConnector($connector); $action->setResponse(Environment::getService('response')); $parameters = $this->getParameters(['connection' => 1, 'queue' => 'foo']); $body = Record::fromArray(['foo' => 'bar']); $response = $action->handle($this->getRequest('POST', [], [], [], $body), $parameters, $this->getContext()); $body = ['success' => true, 'message' => 'Push was successful']; $this->assertInstanceOf('Fusio\\Engine\\ResponseInterface', $response); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals([], $response->getHeaders()); $this->assertEquals($body, $response->getBody()); }
public function testHandle() { $action = new Transform(); $action->setTemplateFactory(Environment::getService('template_factory')); $action->setProcessor(Environment::getService('processor')); $patch = <<<JSON [ { "op": "test", "path": "/title", "value": "foo" }, { "op": "remove", "path": "/id" }, { "op": "add", "path": "/foo", "value": "bar" }, { "op": "replace", "path": "/author/name", "value": "foo" } ] JSON; $parameters = $this->getParameters(['action' => 3, 'patch' => $patch]); $body = Record::fromArray(['id' => 1, 'title' => 'foo', 'author' => Record::fromArray(['name' => 'bar'])]); $request = $this->getRequest(null, [], [], [], $body); $response = $action->handle($request, $parameters, $this->getContext()); $body = new \stdClass(); $body->id = 1; $body->title = 'foo'; $body->content = 'bar'; $body->date = '2015-02-27 19:59:15'; $this->assertInstanceOf('Fusio\\Engine\\ResponseInterface', $response); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals([], $response->getHeaders()); $this->assertEquals($body, $response->getBody()); }
public function testHandle() { $action = new Condition(); $action->setConnection(Environment::getService('connection')); $action->setProcessor(Environment::getService('processor')); $action->setCache(Environment::getService('cache')); $expression = 'rateLimit.getRequestsPerMonth() == 2 && '; $expression .= 'rateLimit.getRequestsPerDay() == 2 && '; $expression .= 'rateLimit.getRequestsOfRoutePerMonth() == 0 && '; $expression .= 'rateLimit.getRequestsOfRoutePerDay() == 0 && '; $expression .= 'app.getName() == "Foo-App" && '; $expression .= 'uriFragments.get("news_id") == 1 && '; $expression .= 'parameters.get("count") == 4 && '; $expression .= 'body.get("foo") == "bar" '; $parameters = $this->getParameters(['condition' => $expression, 'true' => 3, 'false' => 0]); $body = Record::fromArray(['foo' => 'bar']); $response = $action->handle($this->getRequest('POST', ['news_id' => 1], ['count' => 4], [], $body), $parameters, $this->getContext()); $body = new \stdClass(); $body->id = 1; $body->title = 'foo'; $body->content = 'bar'; $body->date = '2015-02-27 19:59:15'; $this->assertInstanceOf('Fusio\\Engine\\ResponseInterface', $response); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals([], $response->getHeaders()); $this->assertEquals($body, $response->getBody()); }
public function testHandle() { // connection $connection = $this->getMock('Pheanstalk\\Pheanstalk', ['useTube', 'put'], [], '', false); $connection->expects($this->once())->method('useTube')->with($this->equalTo('foo'))->will($this->returnValue($connection)); $connection->expects($this->once())->method('put')->with($this->callback(function ($body) { $this->assertJsonStringEqualsJsonString('{"foo": "bar"}', $body); return true; }))->will($this->returnValue($connection)); // connector $connector = $this->getMock('Fusio\\Engine\\ConnectorInterface', ['getConnection'], [], '', false); $connector->expects($this->once())->method('getConnection')->with($this->equalTo(1))->will($this->returnValue($connection)); $action = new MqBeanstalk(); $action->setConnection(Environment::getService('connection')); $action->setConnector($connector); $action->setResponse(Environment::getService('response')); $parameters = $this->getParameters(['connection' => 1, 'queue' => 'foo']); $body = Record::fromArray(['foo' => 'bar']); $response = $action->handle($this->getRequest('POST', [], [], [], $body), $parameters, $this->getContext()); $body = ['success' => true, 'message' => 'Push was successful']; $this->assertInstanceOf('Fusio\\Engine\\ResponseInterface', $response); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals([], $response->getHeaders()); $this->assertEquals($body, $response->getBody()); }
public function __invoke($value) { $writer = new Writer\Json(); if (is_array($value)) { $value = Record::fromArray($value); } elseif ($value instanceof \stdClass) { $value = Record::fromArray((array) $value); } if ($value instanceof RecordInterface) { return $writer->write($value); } else { return '{}'; } }
/** * @expectedException PSX\Validate\ValidationException * @expectedExceptionMessage /title contains an invalid value */ public function testHandleValidatorError() { $action = new Processor(); $action->setProcessor(Environment::getService('processor')); $parameters = $this->getParameters(['process' => $this->getProcess()]); $body = Record::fromArray(['news_id' => 2, 'title' => 'b$ar']); $request = $this->getRequest(null, [], [], [], $body); $response = $action->handle($request, $parameters, $this->getContext()); $body = new \stdClass(); $body->error = 'foo'; $this->assertInstanceOf('Fusio\\Engine\\ResponseInterface', $response); $this->assertEquals(500, $response->getStatusCode()); $this->assertEquals([], $response->getHeaders()); $this->assertEquals($body, $response->getBody()); }
public function testHandle() { $action = new SqlExecute(); $action->setConnection(Environment::getService('connection')); $action->setConnector(Environment::getService('connector')); $action->setTemplateFactory(Environment::getService('template_factory')); $action->setResponse(Environment::getService('response')); $parameters = $this->getParameters(['connection' => 1, 'sql' => 'INSERT INTO app_news (title, content, date) VALUES ({{ body.get("title")|prepare }}, {{ body.get("content")|prepare }}, {{ "2015-02-27 19:59:15"|prepare }})']); $body = Record::fromArray(['title' => 'lorem', 'content' => 'ipsum']); $response = $action->handle($this->getRequest('POST', [], [], [], $body), $parameters, $this->getContext()); $body = []; $body['success'] = true; $body['message'] = 'Execution was successful'; $this->assertInstanceOf('Fusio\\Engine\\ResponseInterface', $response); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals([], $response->getHeaders()); $this->assertEquals($body, $response->getBody()); $row = Environment::getService('connection')->fetchAssoc('SELECT * FROM app_news ORDER BY id DESC'); $expect = ['id' => 3, 'title' => 'lorem', 'content' => 'ipsum', 'date' => '2015-02-27 19:59:15']; $this->assertEquals($expect, $row); }
public function testHandle() { $action = new MongoUpdate(); $action->setConnection(Environment::getService('connection')); $action->setConnector(Environment::getService('connector')); $action->setTemplateFactory(Environment::getService('template_factory')); $action->setResponse(Environment::getService('response')); $parameters = $this->getParameters(['connection' => 3, 'propertyName' => 'foo', 'collection' => 'app_news', 'criteria' => '{"id": {{ request.uriFragment("id") }}}', 'document' => '{{ request.body|json }}']); $body = Record::fromArray(['id' => 2, 'title' => 'lorem', 'content' => 'ipsum', 'date' => '2015-02-27 19:59:15']); $response = $action->handle($this->getRequest('POST', ['id' => 2], [], [], $body), $parameters, $this->getContext()); $body = []; $body['success'] = true; $body['message'] = 'Execution was successful'; $this->assertInstanceOf('Fusio\\Engine\\ResponseInterface', $response); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals([], $response->getHeaders()); $this->assertEquals($body, $response->getBody()); $row = $this->collection->findOne(['id' => 2]); $expect = ['id' => 2, 'title' => 'lorem', 'content' => 'ipsum', 'date' => '2015-02-27 19:59:15']; unset($row['_id']); $this->assertEquals($expect, $row); }
public function testHandle() { $http = $this->getMock('PSX\\Http', array('request')); $http->expects($this->once())->method('request')->with($this->callback(function ($request) { /** @var \PSX\Http\RequestInterface $request */ $this->assertInstanceOf('PSX\\Http\\RequestInterface', $request); $this->assertJsonStringEqualsJsonString('{"foo":"bar"}', (string) $request->getBody()); return true; }))->will($this->returnValue(new Response(200))); $action = new HttpRequest(); $action->setHttp($http); $action->setTemplateFactory(Environment::getService('template_factory')); $action->setResponse(Environment::getService('response')); $parameters = $this->getParameters(['url' => 'http://127.0.0.1/bar', 'body' => '{{ request.body|json }}']); $body = Record::fromArray(['foo' => 'bar']); $response = $action->handle($this->getRequest('POST', [], [], [], $body), $parameters, $this->getContext()); $body = ['success' => true, 'message' => 'Request successful']; $this->assertInstanceOf('Fusio\\Engine\\ResponseInterface', $response); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals([], $response->getHeaders()); $this->assertEquals($body, $response->getBody()); }
protected function getWelcomeConfig() { $config = [Record::fromArray(['active' => true, 'status' => 4, 'name' => '1', 'methods' => [Record::fromArray(['active' => true, 'public' => true, 'name' => 'GET', 'action' => 1, 'response' => 1])]])]; return serialize($config); }
/** * @expectedException PSX\Validate\ValidationException * @expectedExceptionMessage /author/name contains an invalid value */ public function testHandleInvalidBodyAuthorName() { $body = Record::fromArray(['id' => 1, 'title' => 'foo', 'author' => Record::fromArray(['name' => '!bar'])]); $request = $this->getRequest(null, ['bar' => 'foo'], ['foo' => 'bar'], [], $body); $this->handle($request, $body); }