Begin building unit tests

This commit is contained in:
sctn4elk 2024-05-06 13:19:09 -05:00
parent c05bf0559a
commit f0a4699807
7 changed files with 1039 additions and 169 deletions

View File

@ -1,24 +1,25 @@
<?php
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
*/
/**
* Description of BaseController
*
* @author SCTN4
* @author Mike Howard
*/
class BaseController {
public $name;
public function __construct() {
$this->name = 'BaseController';
}
public static function create() { return new self(); }
static function create() { return new self(); }
/**
* __call magic method.
* The __call() method is invoked automatically when a non-existing method or inaccessible method is called.
*/
public function __call($name, $arguments)
{
$this->sendOutput('', array('HTTP/1.1 404 Not Found'));
$this->sendOutput('', array('HTTP/1.1 404 Non-Existant method or inaccessible method called'));
}
/**
* Get URI elements.

View File

@ -1,36 +1,76 @@
<?php
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/SeleniumTests/SeleneseTest.php to edit this template
*/
define('PD', 'D:\DEV\Git Repository\CustomerRewardsRESTAPI');
require_once PD . "/Controller/API/BaseController.php";
/**
* Description of BaseControllerTest
*
* @author SCTN4
*/
class BaseControllerTest extends PHPUnit_Framework_TestCase {
class BaseControllerTest extends TestCase {
/**
* @var \RemoteWebDriver
*/
protected $webDriver;
public function setUp() {
$capabilities = array(\WebDriverCapabilityType::BROWSER_NAME => 'firefox');
$this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities);
protected $base;
#[\Override]
public function setUp(): void {
$this->base = new BaseController();
}
public function tearDown() {
$this->webDriver->close();
#[\Override]
public function tearDown(): void {
$this->base = null;
}
public function testConstruct(): void
{
$this->assertSame('BaseController', $this->base->name);
}
public function testCreate(): void
{
$controllerObj = BaseController::create();
$this->assertSame('BaseController', $controllerObj->name);
}
#[Depends('testConstruct')]
public function testCall(): void
{
$this->base->mymethod = 'test';
$this->assertSame('test', $this->base->mymethod);
}
protected $url = 'http://www.netbeans.org/';
public function testSimple() {
$this->webDriver->get($this->url);
// checking that page title contains word 'NetBeans'
$this->assertContains('NetBeans', $this->webDriver->getTitle());
public function testGetUriSegments(): void
{
// Stop here and mark this test as incomplete.
$this->markTestIncomplete(
'This test has not been implemented yet.',
);
}
public function testGetQueryStringParams(): void
{
// Stop here and mark this test as incomplete.
$this->markTestIncomplete(
'This test has not been implemented yet.',
);
}
public function testGetServerRequestMethod(): void
{
// Stop here and mark this test as incomplete.
$this->markTestIncomplete(
'This test has not been implemented yet.',
);
}
public function testSendOutput(): void
{
// Stop here and mark this test as incomplete.
$this->markTestIncomplete(
'This test has not been implemented yet.',
);
}
}

View File

@ -1,36 +1,109 @@
<?php
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/SeleniumTests/SeleneseTest.php to edit this template
*/
<?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 SCTN4
* @author Mike Howard
*/
class CustomerControllerTest extends PHPUnit_Framework_TestCase {
/**
* @var \RemoteWebDriver
*/
protected $webDriver;
define('PD', 'D:\DEV\Git Repository\CustomerRewardsRESTAPI');
require_once PD . "/Controller/API/BaseController.php";
require_once PD . "/Controller/API/CustomerController.php";
public function setUp() {
$capabilities = array(\WebDriverCapabilityType::BROWSER_NAME => 'firefox');
$this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities);
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();
}
public function tearDown() {
$this->webDriver->close();
#[\Override]
public function tearDown(): void {
//nothing to do
}
protected $url = 'http://www.netbeans.org/';
/*
* [
* {
* "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]]);
public function testSimple() {
$this->webDriver->get($this->url);
// checking that page title contains word 'NetBeans'
$this->assertContains('NetBeans', $this->webDriver->getTitle());
$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]);
}
}

3
Tests/bootstrap.php Normal file
View File

@ -0,0 +1,3 @@
<?php
require_once __DIR__ . '/../vendor/autoload.php';

View File

@ -8,13 +8,18 @@
},
"authors": [
{
"name": "sctn4elk",
"name": "Mike Howard",
"email": "sctn4elk@gmail.com"
}
],
"require-dev": {
"phpunit/phpunit": "11",
"vitexsoftware/phpunit-skeleton-generator": "*",
"php-webdriver/webdriver": "*"
"vitexsoftware/phpunit-skeleton-generator": "*",
"php-webdriver/webdriver": "*",
"phpunit/phpunit-selenium": "^1.3",
"symfony/test-pack": "^1.0"
},
"require": {
"symfony/http-client": "^7.0"
}
}

972
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -24,7 +24,7 @@ $uri_pos = 2;
/* When testing with UniServer the URI array placement position will be
* incremented by 1 due to the folder addition
* http://localhost/CustomerRewards/index.php/customer/process/select?limit=5
* http://localhost/CustomerRewardsRESTAPI/index.php/customer/process/select?limit=5
* uri[1] = folder
* uri[2] = index
* uri[3] = module