Created
October 19, 2016 21:22
-
-
Save Cayan/716b3c3682378a0ebe973ea416699a9f to your computer and use it in GitHub Desktop.
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\Foundation\Testing\DatabaseMigrations; | |
use Illuminate\Foundation\Testing\DatabaseTransactions; | |
class UserCRUDTest extends TestCase | |
{ | |
use DatabaseMigrations, DatabaseTransactions; | |
protected $prefix = ''; | |
protected function setUp() | |
{ | |
parent::setUp(); | |
factory(App\User::class)->create(); | |
factory(App\User::class)->create(); | |
} | |
/** | |
* Fetching all users. | |
* | |
* @return void | |
*/ | |
public function testIndex() | |
{ | |
$response = $this->call('GET', $this->prefix . '/'); | |
$content = json_decode($response->getContent(), true); | |
$this->assertInternalType('array', $content, 'Invalid JSON'); | |
$actual = count($content); | |
$expected = 2; | |
$this->assertEquals($expected, $actual); | |
} | |
public function testRead() | |
{ | |
$response = $this->call('GET', $this->prefix . '/2'); | |
$content = json_decode($response->getContent(), true); | |
$this->assertInternalType('array', $content, 'Invalid JSON'); | |
$actual = $response->getStatusCode(); | |
$expected = \Illuminate\Http\Response::HTTP_OK; | |
$this->assertEquals($expected, $actual); | |
$actual = $content['id']; | |
$expected = 2; | |
$this->assertEquals($expected, $actual); | |
} | |
public function testCreate() | |
{ | |
$password = str_random(20); | |
$params = array_merge(factory(App\User::class)->make()->toArray(), [ | |
'password' => $password, | |
'password_confirmation' => $password | |
]); | |
$response = $this->call( | |
'POST', | |
$this->prefix . '/', | |
$params, | |
[], | |
[], | |
$this->transformHeadersToServerVars([ | |
'Accept' => 'application/json', | |
'CONTENT_TYPE' => 'x-www-form-urlencoded' | |
]) | |
); | |
$actual = $response->getStatusCode(); | |
$expected = \Illuminate\Http\Response::HTTP_NO_CONTENT; | |
$this->assertEquals($expected, $actual); | |
$response = $this->call('GET', $this->prefix . '/'); | |
$content = json_decode($response->getContent(), true); | |
$this->assertInternalType('array', $content, 'Invalid JSON'); | |
$actual = count($content); | |
$expected = 3; | |
$this->assertEquals($expected, $actual); | |
} | |
public function testUpdate() | |
{ | |
$newName = 'Daniel Pinto'; | |
$response = $this->call('PUT', $this->prefix . '/', [ | |
'id' => 2, | |
'name' => $newName | |
], [ | |
'Accept' => 'application/json', | |
'CONTENT_TYPE' => 'x-www-form-urlencoded' | |
]); | |
$actual = $response->getStatusCode(); | |
$expected = \Illuminate\Http\Response::HTTP_NO_CONTENT; | |
$this->assertEquals($expected, $actual); | |
$response = $this->call('GET', $this->prefix . '/2'); | |
$content = json_decode($response->getContent(), true); | |
$this->assertInternalType('array', $content, 'Invalid JSON'); | |
$actual = $content['name']; | |
$expected = $newName; | |
$this->assertEquals($expected, $actual); | |
} | |
public function testDelete() | |
{ | |
$response = $this->call('DELETE', $this->prefix . '/', ['id' => 2]); | |
$actual = $response->getStatusCode(); | |
$expected = \Illuminate\Http\Response::HTTP_NO_CONTENT; | |
$this->assertEquals($expected, $actual); | |
$response = $this->call('GET', $this->prefix . '/'); | |
$content = json_decode($response->getContent(), true); | |
$this->assertInternalType('array', $content, 'Invalid JSON'); | |
$actual = count($content); | |
$expected = 1; | |
$this->assertEquals($expected, $actual); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment