Why should we write unit test?

<?php

namespace
App\Http\Controllers;

use App\Inventory;
use Illuminate\Http\Request;

class InventoryController extends Controller
{
private $inventory;
private $request;

public function __construct(Inventory $inventory, Request $request)
{
$this->inventory = $inventory;
$this->request = $request;
}

public function create()
{

if ($this->request->has('name')) {
if ($this->inventory->create($this->request->all())) {
return response(['status' => 'success']);
}
}

return response(['status' => 'error']);

}

}
<?php

namespace
Tests\Unit\Controllers;

use App\Http\Controllers\InventoryController;
use App\Inventory;
use Illuminate\Http\Request;
use Mockery;

class InventoryControllerTest extends \Tests\TestCase
{
protected $inventoryController;
protected $request;
protected $inventory;

public function setUp()
{
parent::setUp();
$this->inventoryController = Mockery::mock(InventoryController::class)->makePartial();
$this->request = Mockery::mock(Request::class)->makePartial(); $this->inventory = Mockery::mock(Inventory::class)->makePartial();
}

public function makePropertyAccessible(&$object, $property, $value, $class)
{
$reflection = new \ReflectionClass($class);
$property = $reflection->getProperty($property);
$property->setAccessible(true);
$property->setValue($object, $value);
}

public function testIfCreateIsWorkingWhenDetailsAreCorrect()
{
$this->request->shouldReceive('all')->andReturn(['name' => 'desk']);

$this->inventory->shouldReceive('create')->andReturn(true);
$this->makePropertyAccessible($this->inventoryController, 'inventory', $this->inventory, InventoryController::class); $this->makePropertyAccessible($this->inventoryController, 'request', $this->request, InventoryController::class); $reponse = json_decode($this->inventoryController->create()->getContent(), true);

$this->assertSame($reponse, ['status' => 'success']);
}


}
$this->inventoryController = Mockery::mock(InventoryController::class)->makePartial();
method makePropertyAccessible()
public function testIfCreateIsWorkingWhenDetailsAreCorrect()
{
$this->request->shouldReceive('all')->andReturn(['name' => 'desk']);

$this->inventory->shouldReceive('create')->andReturn(true);
$this->makePropertyAccessible($this->inventoryController, 'inventory', $this->inventory, InventoryController::class);$this->makePropertyAccessible($this->inventoryController, 'request', $this->request, InventoryController::class);$reponse = json_decode($this->inventoryController->create()->getContent(), true);

$this->assertSame($reponse, ['status' => 'success']);
}


}
$this->request->shouldReceive('all')->andReturn(['name' => 'desk']);
$this->inventory->shouldReceive('create')->andReturn(true);
$this->makePropertyAccessible($this->inventoryController, 'inventory', $this->inventory, InventoryController::class);
$response = json_decode($this->inventoryController->create()->getContent(), true);

$this->assertSame($response, ['status' => 'success']);
public function create()
{

if ($this->request->has('name') && $this->request->has('price')) {
if ($this->inventory->create($this->request->all())) {

event(new NotifyAdmin($this->inventory));

return response(['status' => 'success','message' => 'item has been successfully added']);
}
}

return response(['status' => 'error','message' => 'item name or price is missing']);

}
public function testIfCreateIsWorkingWhenDetailsAreCorrect()
{
Event::fake();
$this->request->shouldReceive('all')->andReturn(['name' => 'desk','price' => 2]);
$this->inventory->shouldReceive('create')->andReturn(true); $this->makePropertyAccessible($this->inventoryController, 'inventory', $this->inventory, InventoryController::class); $this->makePropertyAccessible($this->inventoryController, 'request', $this->request, InventoryController::class); $response = json_decode($this->inventoryController->create()->getContent(), true); Event::assertDispatched(NotifyAdmin::class); $this->assertSame($response, ['status' => 'success','message' => 'item has been successfully added']);
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store