Skip to content

Commit

Permalink
dev: pass symfony http request in environments without superglobals
Browse files Browse the repository at this point in the history
  • Loading branch information
ofabel committed Jul 14, 2024
1 parent a466a9b commit 41acd14
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ class Request
/**
* Request constructor.
*/
public function __construct()
public function __construct(HttpRequest $request = null)
{
if (null === $this->request) {
if (null === $request) {
$this->request = HttpRequest::createFromGlobals();
} else {
$this->request = $request;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Tus/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ class Server extends AbstractTus
*
* @throws \ReflectionException
*/
public function __construct($cacheAdapter = 'file')
public function __construct($cacheAdapter = 'file', HttpRequest $request = null)
{
$this->request = new Request();
$this->request = new Request($request);
$this->response = new Response();
$this->middleware = new Middleware();
$this->uploadDir = \dirname(__DIR__, 2) . '/' . 'uploads';
Expand Down
27 changes: 27 additions & 0 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TusPhp\Test;

use Mockery as m;
use TusPhp\Request;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
Expand All @@ -26,6 +27,32 @@ public function setUp(): void
parent::setUp();
}

/**
* @test
*
* @covers ::__construct
*/
public function it_sets_and_gets_the_same_http_request(): void
{
$httpRequestMock = m::mock(HttpRequest::class);

$request = new Request($httpRequestMock);

$this->assertSame($httpRequestMock, $request->getRequest());
}

/**
* @test
*
* @covers ::__construct
*/
public function it_creates_the_http_request_when_passing_null(): void
{
$request = new Request(null);

$this->assertInstanceOf(HttpRequest::class, $request->getRequest());
}

/**
* @test
*
Expand Down
37 changes: 27 additions & 10 deletions tests/Tus/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use TusPhp\Exception\ConnectionException;
use TusPhp\Exception\OutOfRangeException;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
use Symfony\Component\HttpFoundation\Response as HttpResponse;

/**
Expand Down Expand Up @@ -108,6 +109,22 @@ public function it_gets_a_request(): void
$this->assertInstanceOf(Request::class, $this->tusServer->getRequest());
}

/**
* @test
*
* @covers ::__construct
* @covers ::getRequest
*/
public function it_sets_and_gets_the_same_http_request(): void
{
$httpRequestMock = m::mock(HttpRequest::class);

$tusServer = new TusServer('file', $httpRequestMock);

$this->assertInstanceOf(HttpRequest::class, $tusServer->getRequest()->getRequest());
$this->assertSame($httpRequestMock, $tusServer->getRequest()->getRequest());
}

/**
* @test
*
Expand Down Expand Up @@ -226,7 +243,7 @@ public function it_sends_412_if_pre_condition_fails(): void
->server
->set('REQUEST_METHOD', 'HEAD');

$requestMock = m::mock(Request::class, ['file'])->makePartial();
$requestMock = m::mock(Request::class, [null])->makePartial();
$requestMock
->getRequest()
->headers
Expand Down Expand Up @@ -274,7 +291,7 @@ public function it_passes_pre_condition_check_if_requests_matches_tus_resumable_
->server
->set('REQUEST_METHOD', 'HEAD');

$requestMock = m::mock(Request::class, ['file'])->makePartial();
$requestMock = m::mock(Request::class, [null])->makePartial();
$requestMock
->getRequest()
->headers
Expand Down Expand Up @@ -402,7 +419,7 @@ public function it_overrides_http_method(): void

$tusServerMock->__construct('file');

$requestMock = m::mock(Request::class, ['file'])->makePartial();
$requestMock = m::mock(Request::class, [null])->makePartial();
$requestMock
->getRequest()
->headers
Expand Down Expand Up @@ -804,7 +821,7 @@ public function it_returns_413_for_uploads_larger_than_max_size(): void
'REQUEST_URI' => '/files',
]);

$requestMock = m::mock(Request::class, ['file'])->makePartial();
$requestMock = m::mock(Request::class, [null])->makePartial();
$requestMock
->getRequest()
->headers
Expand Down Expand Up @@ -854,7 +871,7 @@ public function it_calls_concatenation_for_final_upload(): void
'REQUEST_URI' => '/files',
]);

$requestMock = m::mock(Request::class, ['file'])->makePartial();
$requestMock = m::mock(Request::class, [null])->makePartial();
$requestMock
->getRequest()
->headers
Expand Down Expand Up @@ -916,7 +933,7 @@ public function it_handles_post_for_partial_request(): void
'REQUEST_URI' => '/files',
]);

$requestMock = m::mock(Request::class, ['file'])->makePartial();
$requestMock = m::mock(Request::class, [null])->makePartial();
$requestMock
->getRequest()
->headers
Expand Down Expand Up @@ -1040,7 +1057,7 @@ public function it_handles_post_request(): void
'REQUEST_URI' => '/files',
]);

$requestMock = m::mock(Request::class, ['file'])->makePartial();
$requestMock = m::mock(Request::class, [null])->makePartial();
$requestMock
->getRequest()
->headers
Expand Down Expand Up @@ -1163,7 +1180,7 @@ public function it_handles_concatenation_request(): void
'location' => $location,
];

$requestMock = m::mock(Request::class, ['file'])->makePartial();
$requestMock = m::mock(Request::class, [null])->makePartial();
$requestMock
->getRequest()
->headers
Expand Down Expand Up @@ -1328,7 +1345,7 @@ public function it_throws_460_for_checksum_mismatch_in_concatenation_request():
['file_path' => $filePath . 'file_b', 'offset' => 20],
];

$requestMock = m::mock(Request::class, ['file'])->makePartial();
$requestMock = m::mock(Request::class, [null])->makePartial();
$requestMock
->getRequest()
->headers
Expand Down Expand Up @@ -3176,7 +3193,7 @@ public function it_verifies_upload_size(): void
{
$this->assertTrue($this->tusServerMock->verifyUploadSize());

$requestMock = m::mock(Request::class, ['file'])->makePartial();
$requestMock = m::mock(Request::class, [null])->makePartial();
$requestMock
->getRequest()
->headers
Expand Down

0 comments on commit 41acd14

Please sign in to comment.