You are here

class HttpFoundationFactoryTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/psr-http-message-bridge/Tests/Factory/HttpFoundationFactoryTest.php \Symfony\Bridge\PsrHttpMessage\Tests\Factory\HttpFoundationFactoryTest

@author Kévin Dunglas <dunglas@gmail.com>

Hierarchy

  • class \Symfony\Bridge\PsrHttpMessage\Tests\Factory\HttpFoundationFactoryTest extends \Symfony\Bridge\PsrHttpMessage\Tests\Factory\PHPUnit_Framework_TestCase

Expanded class hierarchy of HttpFoundationFactoryTest

File

vendor/symfony/psr-http-message-bridge/Tests/Factory/HttpFoundationFactoryTest.php, line 24

Namespace

Symfony\Bridge\PsrHttpMessage\Tests\Factory
View source
class HttpFoundationFactoryTest extends \PHPUnit_Framework_TestCase {
  private $factory;
  private $tmpDir;
  public function setup() {
    $this->factory = new HttpFoundationFactory();
    $this->tmpDir = sys_get_temp_dir();
  }
  public function testCreateRequest() {
    $stdClass = new \stdClass();
    $serverRequest = new ServerRequest('1.1', array(
      'X-Dunglas-API-Platform' => '1.0',
      'X-data' => array(
        'a',
        'b',
      ),
    ), new Stream('The body'), '/about/kevin', 'GET', 'http://les-tilleuls.coop/about/kevin', array(
      'country' => 'France',
    ), array(
      'city' => 'Lille',
    ), array(
      'url' => 'http://les-tilleuls.coop',
    ), array(
      'doc1' => $this
        ->createUploadedFile('Doc 1', UPLOAD_ERR_OK, 'doc1.txt', 'text/plain'),
      'nested' => array(
        'docs' => array(
          $this
            ->createUploadedFile('Doc 2', UPLOAD_ERR_OK, 'doc2.txt', 'text/plain'),
          $this
            ->createUploadedFile('Doc 3', UPLOAD_ERR_OK, 'doc3.txt', 'text/plain'),
        ),
      ),
    ), array(
      'url' => 'http://dunglas.fr',
    ), array(
      'custom' => $stdClass,
    ));
    $symfonyRequest = $this->factory
      ->createRequest($serverRequest);
    $this
      ->assertEquals('http://les-tilleuls.coop', $symfonyRequest->query
      ->get('url'));
    $this
      ->assertEquals('doc1.txt', $symfonyRequest->files
      ->get('doc1')
      ->getClientOriginalName());
    $this
      ->assertEquals('doc2.txt', $symfonyRequest->files
      ->get('nested[docs][0]', null, true)
      ->getClientOriginalName());
    $this
      ->assertEquals('doc3.txt', $symfonyRequest->files
      ->get('nested[docs][1]', null, true)
      ->getClientOriginalName());
    $this
      ->assertEquals('http://dunglas.fr', $symfonyRequest->request
      ->get('url'));
    $this
      ->assertEquals($stdClass, $symfonyRequest->attributes
      ->get('custom'));
    $this
      ->assertEquals('Lille', $symfonyRequest->cookies
      ->get('city'));
    $this
      ->assertEquals('France', $symfonyRequest->server
      ->get('country'));
    $this
      ->assertEquals('The body', $symfonyRequest
      ->getContent());
    $this
      ->assertEquals('1.0', $symfonyRequest->headers
      ->get('X-Dunglas-API-Platform'));
    $this
      ->assertEquals(array(
      'a',
      'b',
    ), $symfonyRequest->headers
      ->get('X-data', null, false));
  }
  public function testCreateRequestWithNullParsedBody() {
    $serverRequest = new ServerRequest('1.1', array(), new Stream(), '/', 'GET', null, array(), array(), array(), array(), null, array());
    $this
      ->assertCount(0, $this->factory
      ->createRequest($serverRequest)->request);
  }
  public function testCreateRequestWithObjectParsedBody() {
    $serverRequest = new ServerRequest('1.1', array(), new Stream(), '/', 'GET', null, array(), array(), array(), array(), new \stdClass(), array());
    $this
      ->assertCount(0, $this->factory
      ->createRequest($serverRequest)->request);
  }
  public function testCreateUploadedFile() {
    $uploadedFile = $this
      ->createUploadedFile('An uploaded file.', UPLOAD_ERR_OK, 'myfile.txt', 'text/plain');
    $symfonyUploadedFile = $this
      ->callCreateUploadedFile($uploadedFile);
    $uniqid = uniqid();
    $symfonyUploadedFile
      ->move($this->tmpDir, $uniqid);
    $this
      ->assertEquals($uploadedFile
      ->getSize(), $symfonyUploadedFile
      ->getClientSize());
    $this
      ->assertEquals(UPLOAD_ERR_OK, $symfonyUploadedFile
      ->getError());
    $this
      ->assertEquals('myfile.txt', $symfonyUploadedFile
      ->getClientOriginalName());
    $this
      ->assertEquals('txt', $symfonyUploadedFile
      ->getClientOriginalExtension());
    $this
      ->assertEquals('text/plain', $symfonyUploadedFile
      ->getClientMimeType());
    $this
      ->assertEquals('An uploaded file.', file_get_contents($this->tmpDir . '/' . $uniqid));
  }

  /**
   * @expectedException        \Symfony\Component\HttpFoundation\File\Exception\FileException
   * @expectedExceptionMessage The file "e" could not be written on disk.
   */
  public function testCreateUploadedFileWithError() {
    $uploadedFile = $this
      ->createUploadedFile('Error.', UPLOAD_ERR_CANT_WRITE, 'e', 'text/plain');
    $symfonyUploadedFile = $this
      ->callCreateUploadedFile($uploadedFile);
    $this
      ->assertEquals(UPLOAD_ERR_CANT_WRITE, $symfonyUploadedFile
      ->getError());
    $symfonyUploadedFile
      ->move($this->tmpDir, 'shouldFail.txt');
  }
  private function createUploadedFile($content, $error, $clientFileName, $clientMediaType) {
    $filePath = tempnam($this->tmpDir, uniqid());
    file_put_contents($filePath, $content);
    return new UploadedFile($filePath, filesize($filePath), $error, $clientFileName, $clientMediaType);
  }
  private function callCreateUploadedFile(UploadedFileInterface $uploadedFile) {
    $reflection = new \ReflectionClass($this->factory);
    $createUploadedFile = $reflection
      ->getMethod('createUploadedFile');
    $createUploadedFile
      ->setAccessible(true);
    return $createUploadedFile
      ->invokeArgs($this->factory, array(
      $uploadedFile,
    ));
  }
  public function testCreateResponse() {
    $response = new Response('1.0', array(
      'X-Symfony' => array(
        '2.8',
      ),
      'Set-Cookie' => array(
        'theme=light',
        'test',
        'ABC=AeD; Domain=dunglas.fr; Path=/kevin; Expires=Wed, 13 Jan 2021 22:23:01 GMT; Secure; HttpOnly',
      ),
    ), new Stream('The response body'), 200);
    $symfonyResponse = $this->factory
      ->createResponse($response);
    $this
      ->assertEquals('1.0', $symfonyResponse
      ->getProtocolVersion());
    $this
      ->assertEquals('2.8', $symfonyResponse->headers
      ->get('X-Symfony'));
    $cookies = $symfonyResponse->headers
      ->getCookies();
    $this
      ->assertEquals('theme', $cookies[0]
      ->getName());
    $this
      ->assertEquals('light', $cookies[0]
      ->getValue());
    $this
      ->assertEquals(0, $cookies[0]
      ->getExpiresTime());
    $this
      ->assertNull($cookies[0]
      ->getDomain());
    $this
      ->assertEquals('/', $cookies[0]
      ->getPath());
    $this
      ->assertFalse($cookies[0]
      ->isSecure());
    $this
      ->assertFalse($cookies[0]
      ->isHttpOnly());
    $this
      ->assertEquals('test', $cookies[1]
      ->getName());
    $this
      ->assertNull($cookies[1]
      ->getValue());
    $this
      ->assertEquals('ABC', $cookies[2]
      ->getName());
    $this
      ->assertEquals('AeD', $cookies[2]
      ->getValue());
    $this
      ->assertEquals(strtotime('Wed, 13 Jan 2021 22:23:01 GMT'), $cookies[2]
      ->getExpiresTime());
    $this
      ->assertEquals('dunglas.fr', $cookies[2]
      ->getDomain());
    $this
      ->assertEquals('/kevin', $cookies[2]
      ->getPath());
    $this
      ->assertTrue($cookies[2]
      ->isSecure());
    $this
      ->assertTrue($cookies[2]
      ->isHttpOnly());
    $this
      ->assertEquals('The response body', $symfonyResponse
      ->getContent());
    $this
      ->assertEquals(200, $symfonyResponse
      ->getStatusCode());
  }

}

Members