Permalink
Browse files

Add unit tests for controller/responses

  • Loading branch information...
MitchellMcKenna committed Aug 21, 2017
1 parent d9bc0e8 commit e122e1d8a76b599cce106dec4cf9585dc612c42a
@@ -0,0 +1,84 @@
<?php
namespace Tests\Unit\App\Http\Controllers;
use App\Http\Controllers\OrderController;
use App\Http\Requests\OrderCreateRequest;
use App\Http\Requests\OrderUpdateRequest;
use App\Http\Responses\OrderCollectionResponse;
use App\Http\Responses\OrderResponse;
use App\Order;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Tests\TestCase;
class OrderControllerTest extends TestCase
{
use RefreshDatabase;
/** @var OrderController */
protected $controller;
protected function setUp()
{
parent::setUp();
$this->controller = new OrderController();
}
public function testIndex()
{
factory(Order::class, 2)->create();
$request = new Request();
$response = $this->controller->index($request);
$this->assertInstanceOf(OrderCollectionResponse::class, $response);
}
public function testShow()
{
$order = factory(Order::class)->create();
$response = $this->controller->show($order);
$this->assertInstanceOf(OrderResponse::class, $response);
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
}
public function testStore()
{
$order = factory(Order::class)->make();
$request = OrderCreateRequest::create('/api/orders', 'POST', [], [], [], [], json_encode([
'data' => [
'type' => 'order',
'attributes' => ['order_id' => $order->order_id, 'quantity' => $order->quantity],
'relationships' => ['product' => ['data' => ['id' => $order->product->id]]]
]
]));
$response = $this->controller->store($request);
$this->assertInstanceOf(OrderResponse::class, $response);
$this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode());
$this->assertDatabaseHas('orders', ['order_id' => $order->order_id]);
}
public function testUpdate()
{
$order = factory(Order::class)->create();
$newOrderId = 12345;
$request = OrderUpdateRequest::create("/api/orders/{$order->id}", 'PATCH', [], [], [], [], json_encode([
'data' => ['attributes' => ['order_id' => $newOrderId]]
]));
$response = $this->controller->update($request, $order);
$this->assertInstanceOf(OrderResponse::class, $response);
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
$this->assertDatabaseHas('orders', ['id' => $order->id, 'order_id' => $newOrderId]);
}
public function testDestroy()
{
$order = factory(Order::class)->create();
$response = $this->controller->destroy($order);
$this->assertEquals(Response::HTTP_NO_CONTENT, $response->getStatusCode());
$this->assertDatabaseMissing('orders', ['id' => $order->id]);
}
}
@@ -0,0 +1,83 @@
<?php
namespace Tests\Unit\App\Http\Controllers;
use App\Http\Controllers\ProductController;
use App\Http\Requests\ProductCreateRequest;
use App\Http\Requests\ProductUpdateRequest;
use App\Http\Responses\ProductCollectionResponse;
use App\Http\Responses\ProductResponse;
use App\Product;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Tests\TestCase;
class ProductControllerTest extends TestCase
{
use RefreshDatabase;
/** @var ProductController */
protected $controller;
/** @var Product */
protected $product;
protected function setUp()
{
parent::setUp();
$this->controller = new ProductController();
}
public function testIndex()
{
factory(Product::class, 2)->create();
$request = new Request();
$response = $this->controller->index($request);
$this->assertInstanceOf(ProductCollectionResponse::class, $response);
}
public function testShow()
{
$product = factory(Product::class)->create();
$response = $this->controller->show($product);
$this->assertInstanceOf(ProductResponse::class, $response);
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
}
public function testStore()
{
$product = factory(Product::class)->make();
$request = ProductCreateRequest::create('/api/products', 'POST', [], [], [], [], json_encode([
'data' => ['attributes' => ['name' => $product->name]]
]));
$response = $this->controller->store($request);
$this->assertInstanceOf(ProductResponse::class, $response);
$this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode());
$this->assertDatabaseHas('products', ['name' => $product->name]);
}
public function testUpdate()
{
$product = factory(Product::class)->create();
$newName = "Updated Name";
$request = ProductUpdateRequest::create("/api/products/{$product->id}", 'PATCH', [], [], [], [], json_encode([
'data' => ['attributes' => ['name' => $newName]]
]));
$response = $this->controller->update($request, $product);
$this->assertInstanceOf(ProductResponse::class, $response);
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
$this->assertDatabaseHas('products', ['id' => $product->id, 'name' => $newName]);
}
public function testDestroy()
{
$product = factory(Product::class)->create();
$response = $this->controller->destroy($product);
$this->assertEquals(Response::HTTP_NO_CONTENT, $response->getStatusCode());
$this->assertDatabaseMissing('products', ['id' => $product->id]);
}
}
@@ -0,0 +1,70 @@
<?php
namespace Tests\Unit\App\Http\Controllers;
use App\Http\Controllers\TopSellersController;
use App\Http\Requests\TopSellersRequest;
use App\Order;
use App\Product;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class TopSellersControllerTest extends TestCase
{
use RefreshDatabase;
/** @var TopSellersController */
protected $controller;
protected function setUp()
{
parent::setUp();
$this->controller = new TopSellersController();
}
public function testIndexDefaultsToOnlyIncludeOrdersInLast24Hours()
{
$topSeller = factory(Product::class)->create();
// Create 1 before
factory(Order::class)->create(['product_id' => $topSeller->id, 'created_at' => Carbon::now()->subHours(25)]);
// Create 2 during
factory(Order::class, 2)->create(
['product_id' => $topSeller->id, 'quantity' => 4]
);
$response = $this->controller->index(new TopSellersRequest());
$this->assertEquals(8, $response->getOriginalContent()['data'][0]['meta']['quantity']);
}
public function testIndexOnlyIncludesOrdersBetweenBeginAndEndQueryParams()
{
$begin = Carbon::create(2010);
$end = Carbon::create(2012);
$topSeller = factory(Product::class)->create();
// Create 1 before
factory(Order::class)->create(['product_id' => $topSeller->id, 'created_at' => Carbon::create(2009)]);
// Create 2 during
factory(Order::class, 2)->create(
['product_id' => $topSeller->id, 'quantity' => 5, 'created_at' => Carbon::create(2011)]
);
// Create 1 after
factory(Order::class)->create(['product_id' => $topSeller->id, 'created_at' => Carbon::create(2013)]);
$request = new TopSellersRequest(['begin' => $begin->timestamp, 'end' => $end->timestamp]);
$response = $this->controller->index($request);
$this->assertEquals(10, $response->getOriginalContent()['data'][0]['meta']['quantity']);
}
public function testIndexPagination()
{
factory(Order::class)->create(['quantity' => 100]);
$secondPlace = factory(Order::class)->create(['quantity' => 50]);
$request = new TopSellersRequest(['page' => 2, 'limit' => 1]);
$response = $this->controller->index($request);
$this->assertEquals($secondPlace->product->id, $response->getOriginalContent()['data'][0]['id']);
}
}
@@ -0,0 +1,68 @@
<?php
namespace Tests\Unit\App\Http\Responses;
use App\Http\Responses\OrderResponse;
use App\Order;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Response;
use Tests\TestCase;
class OrderResponseTest extends TestCase
{
use RefreshDatabase;
/** @var OrderResponse */
protected $response;
/** @var Order */
protected $order;
protected function setUp()
{
parent::setUp();
$this->order = factory(Order::class)->create();
}
public function testSetsStatusCode()
{
$response = new OrderResponse($this->order, Response::HTTP_CREATED);
$this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode());
}
public function testSetsContentAndFormatsCorrectly()
{
$response = new OrderResponse($this->order);
$this->assertJsonStringEqualsJsonString($response->getContent(), json_encode([
'data' => [
'type' => 'order',
'id' => (string) $this->order->id,
'attributes' => [
'order_id' => $this->order->order_id,
'quantity' => $this->order->quantity,
'created_at' => $this->order->created_at->getTimestamp(),
'updated_at' => $this->order->updated_at->getTimestamp()
],
'relationships' => [
'product' => [
'data' => [
'type' => 'product',
'id' => (string) $this->order->product_id
]
]
]
],
'included' => [[
'type' => 'product',
'id' => (string) $this->order->product->id,
'attributes' => [
'name' => $this->order->product->name,
'created_at' => $this->order->product->created_at->getTimestamp(),
'updated_at' => $this->order->product->updated_at->getTimestamp()
]
]]
]));
}
}
@@ -0,0 +1,50 @@
<?php
namespace Tests\Unit\App\Http\Responses;
use App\Http\Responses\ProductResponse;
use App\Product;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Response;
use Tests\TestCase;
class ProductResponseTest extends TestCase
{
use RefreshDatabase;
/** @var ProductResponse */
protected $response;
/** @var Product */
protected $product;
protected function setUp()
{
parent::setUp();
$this->product = factory(Product::class)->create();
}
public function testSetsStatusCode()
{
$response = new ProductResponse($this->product, Response::HTTP_CREATED);
$this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode());
}
public function testSetsContentAndFormatsCorrectly()
{
$response = new ProductResponse($this->product);
$this->assertJsonStringEqualsJsonString($response->getContent(), json_encode([
'data' => [
'type' => 'product',
'id' => (string) $this->product->id,
'attributes' => [
'name' => $this->product->name,
'created_at' => $this->product->created_at->getTimestamp(),
'updated_at' => $this->product->updated_at->getTimestamp()
]
]
]));
}
}

0 comments on commit e122e1d

Please sign in to comment.