CustomerRewardsRESTAPI/Tests/Controller/API/CustomerControllerTest.php

144 lines
5.2 KiB
PHP

<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
//use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Component\HttpClient\HttpClient;
/**
* Description of CustomerControllerTest
*
* @author Mike Howard
*/
define('PD', 'D:\DEV\Git Repository\CustomerRewardsRESTAPI');
require_once PD . "/Controller/API/BaseController.php";
require_once PD . "/Controller/API/CustomerController.php";
require_once PD . "/Model/CustomerModel.php";
require_once PD . "/Model/ModelTraits.php";
require PD . "/include/bootstrap.php";
class CustomerControllerTest extends TestCase {
private readonly HttpClientInterface $httpClient;
protected $url = 'http://localhost/CustomerRewards/index.php/customer/process/select';
protected $control;
#[\Override]
public function setUp(): void {
$this->httpClient= HttpClient::create();
$this->control = new CustomerController();
}
#[\Override]
public function tearDown(): void {
//nothing to do
$this->control = null;
}
public function testConstruct(): void
{
$this->assertSame('CustomerController', $this->control->basename);
}
public function testCreate(): void
{
$controllerObj = BaseController::create();
$this->assertSame('BaseController', $controllerObj->basename);
}
#[Depends('testConstruct')]
public function testCall(): void
{
$this->control->mymethod = 'test';
$this->assertSame('test', $this->control->mymethod);
}
public function testProcessAction(): void
{
$this->markTestIncomplete(
'This test has not been implemented yet.',
);
//$this->control->customerModel->customer_id = 0;
//$this->assertSame(0, $this->control->customerModel->customer_id);
}
/*
* [
* {
* "customer_id": 0,
* "customer_name_first": "Not",
* "customer_name_last": "Member",
* "customer_email": "email@host.com",
* "customer_phone": "000-000-0000",
* "customer_birthday": "1900-01-01",
* "customer_street": "0 No Name Avenue",
* "address_city": "city",
* "address_state": "state",
* "address_zip": "00000",
* "loyalty_member": 0
* }
* ]
*/
public function testIndexSelectById(): void {
$response = $this->httpClient->request('GET', $this->url, [
'query' => ['customer_id' => 0]]);
//$this->assertSame("GET", $this->control->requestMethod);
$statusCode = $response->getStatusCode();
$this->assertSame(200, $statusCode);
$contentType = $response->getHeaders()['content-type'][0];
$this->assertSame('application/json', $contentType);
//$content = $response->getContent();
//$this->assertSame('Not', $content['customer_name_first']);
$content = $response->toArray();
$this->assertSame(0, $content[0]['customer_id']);
$this->assertSame('Not', $content[0]['customer_name_first']);
$this->assertSame('Member', $content[0]['customer_name_last']);
$this->assertSame('email@host.com', $content[0]['customer_email']);
$this->assertSame('000-000-0000', $content[0]['customer_phone']);
$this->assertSame('1900-01-01', $content[0]['customer_birthday']);
$this->assertSame('0 No Name Avenue', $content[0]['customer_street']);
$this->assertSame('city', $content[0]['address_city']);
$this->assertSame('state', $content[0]['address_state']);
$this->assertSame('00000', $content[0]['address_zip']);
$this->assertSame(0, $content[0]['loyalty_member']);
}
public function testIndexSelect(): void {
$response = $this->httpClient->request('GET', $this->url, [
'query' => ['limit' => 5]]);
$statusCode = $response->getStatusCode();
$this->assertSame(200, $statusCode);
$contentType = $response->getHeaders()['content-type'][0];
$this->assertSame('application/json', $contentType);
$content = $response->toArray();
//Check if a single array is returned
$arrayCount = count($content);
$this->assertSame(4, $arrayCount);
//Check if the array has 11 elements
$arrayElementCount = count($content[0]);
$this->assertSame(11, $arrayElementCount);
//Verify the element names match expected
$this->assertArrayHasKey('customer_id', $content[0]);
$this->assertArrayHasKey('customer_name_first', $content[0]);
$this->assertArrayHasKey('customer_name_last', $content[0]);
$this->assertArrayHasKey('customer_email', $content[0]);
$this->assertArrayHasKey('customer_phone', $content[0]);
$this->assertArrayHasKey('customer_birthday', $content[0]);
$this->assertArrayHasKey('customer_street', $content[0]);
$this->assertArrayHasKey('address_city', $content[0]);
$this->assertArrayHasKey('address_state', $content[0]);
$this->assertArrayHasKey('address_zip', $content[0]);
$this->assertArrayHasKey('loyalty_member', $content[0]);
}
}