CustomerRewardsRESTAPI/Tests/Controller/API/CustomerControllerTest.php

110 lines
4.1 KiB
PHP
Raw Normal View History

2024-05-06 13:19:09 -05:00
<?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
*
2024-05-06 13:19:09 -05:00
* @author Mike Howard
*/
2024-05-06 13:19:09 -05:00
define('PD', 'D:\DEV\Git Repository\CustomerRewardsRESTAPI');
require_once PD . "/Controller/API/BaseController.php";
require_once PD . "/Controller/API/CustomerController.php";
2024-05-06 13:19:09 -05:00
class CustomerControllerTest extends TestCase {
private readonly HttpClientInterface $httpClient;
protected $url = 'http://localhost/CustomerRewardsRESTAPI/index.php/customer/process/select';
#[\Override]
public function setUp(): void {
$this->httpClient= HttpClient::create();
}
2024-05-06 13:19:09 -05:00
#[\Override]
public function tearDown(): void {
//nothing to do
}
2024-05-06 13:19:09 -05:00
/*
* [
* {
* "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]]);
$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' => 1]]);
2024-05-06 13:19:09 -05:00
$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(1, $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]);
}
}