Skip to content

Instantly share code, notes, and snippets.

@IftekherSunny
Last active February 2, 2022 11:29
Show Gist options
  • Save IftekherSunny/c1386c6d3c362d9c5642e9056951ac54 to your computer and use it in GitHub Desktop.
Save IftekherSunny/c1386c6d3c362d9c5642e9056951ac54 to your computer and use it in GitHub Desktop.
Laravel File Upload API Test
<?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' => 'iftekhersunny@hotmail.com'
]);
$this->seeInDatabase('subscribers', [
'email' => 'parvez@themexpert.com'
]);
}
/**
* 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;
}
}
@singh-ip
Copy link

Great job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment