src/Templates/CRUD/Tests/Unit/ServiceTest.txt
<?php
use Tests\TestCase;
use _namespace_services_\_camel_case_Service;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class _camel_case_ServiceTest extends TestCase
{
use DatabaseMigrations;
public function setUp()
{
parent::setUp();
$this->service = $this->app->make(_camel_case_Service::class);
$this->originalArray = [
// _camel_case_ table data
];
$this->editedArray = [
// _camel_case_ table data
];
$this->searchTerm = '';
}
public function testAll()
{
$response = $this->service->all();
$this->assertEquals(get_class($response), 'Illuminate\Database\Eloquent\Collection');
$this->assertTrue(is_array($response->toArray()));
$this->assertEquals(0, count($response->toArray()));
}
public function testPaginated()
{
$response = $this->service->paginated(25);
$this->assertEquals(get_class($response), 'Illuminate\Pagination\LengthAwarePaginator');
$this->assertEquals(0, $response->total());
}
public function testSearch()
{
$response = $this->service->search($this->searchTerm, 25);
$this->assertEquals(get_class($response), 'Illuminate\Pagination\LengthAwarePaginator');
$this->assertEquals(0, $response->total());
}
public function testCreate()
{
$response = $this->service->create($this->originalArray);
$this->assertEquals(get_class($response), '_namespace_model_\_camel_case_');
$this->assertEquals(1, $response->id);
}
public function testFind()
{
// create the item
$item = $this->service->create($this->originalArray);
$response = $this->service->find($item->id);
$this->assertEquals($item->id, $response->id);
}
public function testUpdate()
{
// create the item
$item = $this->service->create($this->originalArray);
$response = $this->service->update($item->id, $this->editedArray);
$this->assertDatabaseHas('_table_name_', $this->editedArray);
}
public function testDestroy()
{
// create the item
$item = $this->service->create($this->originalArray);
$response = $this->service->destroy($item->id);
$this->assertTrue($response);
}
}