Skip to content

Instantly share code, notes, and snippets.

@gnugat
Last active August 29, 2015 14:07
Show Gist options
  • Save gnugat/6ca6b76184246b6d60e7 to your computer and use it in GitHub Desktop.
Save gnugat/6ca6b76184246b6d60e7 to your computer and use it in GitHub Desktop.
gnugat/ripozi - ApiTestClient
<?php
namespace Gnugat\Ripozi\Test;
/**
* Assumes that the application kernel is named AppKernel and has been loaded
*/
class ApiTestClient
{
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
private $container;
/**
* @var \Symfony\Component\HttpKernel\KernelInterface
*/
private $kernel;
public function __construct()
{
$this->kernel = new \AppKernel('test', true);
$this->kernel->boot();
$this->container = $this->kernel->getContainer();
}
/**
* @param string $uri
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function get($uri)
{
$headers = array('CONTENT_TYPE' => 'application/json');
$client = $this->makeClient();
$client->request('GET', $uri, array(), array(), $headers);
return $client->getResponse();
}
/**
* @param string $uri
* @param array $data
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function post($uri, array $data)
{
$headers = array('CONTENT_TYPE' => 'application/json');
$content = json_encode($data);
$client = $this->makeClient();
$client->request('POST', $uri, array(), array(), $headers, $content);
return $client->getResponse();
}
/**
* @param string $uri
* @param array $data
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function put($uri, array $data)
{
$headers = array('CONTENT_TYPE' => 'application/json');
$content = json_encode($data);
$client = $this->makeClient();
$client->request('PUT', $uri, array(), array(), $headers, $content);
return $client->getResponse();
}
/**
* @return \Symfony\Bundle\FrameworkBundle\Client
*/
private function makeClient()
{
return $this->container->get('test.client');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment