Last active
February 2, 2022 11:29
-
-
Save IftekherSunny/c1386c6d3c362d9c5642e9056951ac54 to your computer and use it in GitHub Desktop.
Laravel File Upload API Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Http\UploadedFile; | |
use Illuminate\Foundation\Testing\WithoutMiddleware; | |
use Illuminate\Foundation\Testing\DatabaseMigrations; | |
class SubscribersTest extends TestCase | |
{ | |
use DatabaseMigrations, WithoutMiddleware; | |
/** | |
* @test | |
* | |
* POST: /api/subscribers/csv/import | |
*/ | |
public function it_should_import_subscribers_from_csv_file() | |
{ | |
$file = $this->getUploadableFile(base_path("tests/fixtures/subscribers/dummySubscribers.csv")); | |
$response = $this->actingAs($this->user) | |
->call('POST', '/api/subscribers/csv/import', [], [], ['csv' => $file], []); | |
$responseData = json_decode($response->getContent()); | |
$this->assertEquals($responseData->message, 'Subscribers has been imported successfully'); | |
$this->assertEquals($responseData->status_code, 200); | |
$this->assertResponseOk(); | |
$this->seeInDatabase('subscribers', [ | |
'email' => '[email protected]' | |
]); | |
$this->seeInDatabase('subscribers', [ | |
'email' => '[email protected]' | |
]); | |
} | |
/** | |
* Get uploadable file. | |
* | |
* @return UploadedFile | |
*/ | |
protected function getUploadableFile($file) | |
{ | |
$dummy = file_get_contents($file); | |
file_put_contents(base_path("tests/" . basename($file)), $dummy); | |
$path = base_path("tests/" . basename($file)); | |
$original_name = 'subscribers.csv'; | |
$mime_type = 'text/csv'; | |
$size = 111; | |
$error = null; | |
$test = true; | |
$file = new UploadedFile($path, $original_name, $mime_type, $size, $error, $test); | |
return $file; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great job