Skip to content
Snippets Groups Projects
Commit 617a3419 authored by Andrew Hoffmann's avatar Andrew Hoffmann
Browse files

Merge branch 'ECOM-221-Mock' into 'master'

Added Interface and Mock classes, Unit Test

This takes advantage of the interfaces to put into place a mock and a soap implementation and unit tests for Product and ProductService

Please review: @ahoffmann @npblair @weizhong-wang 

See merge request !3
parents 066eab32 b031ff6b
No related branches found
No related tags found
1 merge request!3Added Interface and Mock classes, Unit Test
......@@ -58,4 +58,14 @@ $request = new edu\wisc\services\cbs\InputParameters($P_ITEM_NUMBER, $P_ITEM_DES
$response = $service->ITEM_CREATE($request);
// Code here to extract data from response
unlink($request, $response);
```
\ No newline at end of file
```
### Setting up your development environment for this project
####Intellij:
1. Install [PHP](http://php.net/manual/en/install.php)
2. Install [Composer](https://getcomposer.org/doc/00-intro.md)
3. Optional: PHP Debugging with [XDebug](https://xdebug.org/download.php)
4. Settings>Languages & Frameworks>PHP Select your PHP interpreter (...) and select the path to your php.exe
5. Execute the command `composer install`
5. PHP> PHPUnit> Use custom autoloader and Select the path to the autoloader.php in vendor directory
\ No newline at end of file
......@@ -16,13 +16,14 @@
},
"require": {
"php": "^5.3.0",
"php": "^5.3.0"
},
"require-dev": {
"phing/phing": "2.11.0",
"wsdl2phpgenerator/wsdl2phpgenerator": "3.2.0",
"phpdocumentor/phpdocumentor": "2.*",
"phpunit/phpunit": "5.2.12"
"phpunit/phpunit": "5.2.12",
"mockery/mockery": "0.9.4"
},
"extra": {
"branch-alias": {
......
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="unit">
<directory suffix="Test.php">./src/test</directory>
</testsuite>
<testsuite name="integration">
<directory suffix="IT.php">./src/test</directory>
</testsuite>
<testsuite name="all">
<directory suffix="Test.php">./src/test</directory>
<directory suffix="IT.php">./src/test</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src/main</directory>
</whitelist>
</filter>
<logging>
<log type="junit" target="reports/phpunit.xml"/>
</logging>
</phpunit>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="edu\wisc\services\cbs\MockTechstoreUnitTest" file="C:\Users\alundholm\git\cbs-techstore-client-php\src\test\MockTechstoreUnitTest.php" tests="2" assertions="6" failures="0" errors="0" time="0.045004">
<testcase name="testCreateProduct" class="edu\wisc\services\cbs\MockTechstoreUnitTest" file="C:\Users\alundholm\git\cbs-techstore-client-php\src\test\MockTechstoreUnitTest.php" line="13" assertions="5" time="0.029003"/>
<testcase name="testCreateProductWithException" class="edu\wisc\services\cbs\MockTechstoreUnitTest" file="C:\Users\alundholm\git\cbs-techstore-client-php\src\test\MockTechstoreUnitTest.php" line="31" assertions="1" time="0.016001"/>
</testsuite>
</testsuites>
......@@ -7,7 +7,16 @@ namespace edu\wisc\services\cbs;
*
* @author Andrew Lundholm
*/
class cbsServiceFactory {
class DOIT_SOA_ITEM_ServiceFactory
{
/**
* DOIT_SOA_ITEM_ServiceFactory constructor.
*/
public function __construct()
{
}
/**
* Returns a new instance of {@link edu\wisc\services\cbs\DOIT_SOA_ITEM_Service}. Encapsulates
......@@ -19,9 +28,10 @@ class cbsServiceFactory {
* @param string $wsdl
* @return edu\wisc\services\cbs\DOIT_SOA_ITEM_Service
*/
public static function getInstance($username, $password, $wsdl = null) {
public function getInstance($username, $password, $wsdl = null)
{
$service = null;
if(is_null($wsdl)) {
if (is_null($wsdl)) {
$service = new DOIT_SOA_ITEM_Service();
} else {
$service = new DOIT_SOA_ITEM_Service(array(), $wsdl);
......
<?php
namespace edu\wisc\services\cbs;
/**
* Exception thrown by {@link MockTechstoreCbsClient}
*/
class MockCbsClientException extends \Exception
{
}
<?php
/**
* created by alundholm
*/
namespace edu\wisc\services\cbs;
/**
* Class MockTechstoreCbsClient
* @package edu\wisc\services\cbs
*/
class MockTechstoreCbsClient implements ProductService
{
/** @var bool */
private $isFailing;
/**
* MockTechstoreCbsClient constructor.
* @param bool $isFailing if true, all calls will thrown an exception
*/
public function __construct($isFailing = false)
{
$this->isFailing = $isFailing;
}
/**
* {@inheritDoc}
* @throws MockCbsClientException
*/
public function createProduct(Product $product)
{
if ($this->isFailing) {
throw new MockCbsClientException("The mock client has been configured to fail.");
}
return $product;
}
}
\ No newline at end of file
<?php
namespace edu\wisc\services\cbs;
/**
* Intermediate representation of a "Product", independent of system (e.g. Magento, CBS).
*
......@@ -8,12 +10,33 @@
class Product
{
/** @var string */
private $itemNumber;
/** @var string */
private $name;
/** @var string */
private $description;
/** @var string */
private $cost;
/**
* Product constructor.
* @param string $itemNumber
* @param string $name
* @param string $description
* @param string $cost
*/
public function __construct($itemNumber, $name, $description, $cost)
{
$this->itemNumber = $itemNumber;
$this->name = $name;
$this->description = $description;
$this->cost = $cost;
}
/**
* @return mixed
* @return string
*/
public function getItemNumber()
{
......@@ -21,7 +44,7 @@ class Product
}
/**
* @param mixed $itemNumber
* @param string $itemNumber
*/
public function setItemNumber($itemNumber)
{
......@@ -29,7 +52,7 @@ class Product
}
/**
* @return mixed
* @return string
*/
public function getName()
{
......@@ -37,7 +60,7 @@ class Product
}
/**
* @param mixed $name
* @param string $name
*/
public function setName($name)
{
......@@ -45,7 +68,7 @@ class Product
}
/**
* @return mixed
* @return string
*/
public function getDescription()
{
......@@ -53,7 +76,7 @@ class Product
}
/**
* @param mixed $description
* @param string $description
*/
public function setDescription($description)
{
......@@ -61,7 +84,7 @@ class Product
}
/**
* @return mixed
* @return string
*/
public function getCost()
{
......@@ -69,7 +92,7 @@ class Product
}
/**
* @param mixed $cost
* @param string $cost
*/
public function setCost($cost)
{
......
<?php
namespace edu\wisc\services\cbs;
/**
* Service interface for managing products.
*
......
<?php
namespace edu\wisc\services\cbs;
/**
* Class SoapTechstoreClient uses SOAP to create a product in the CBS database.
*/
class SoapTechstoreClient implements ProductService
{
/** @var DOIT_SOA_ITEM_Service * */
private $doitSoaItemService;
/**
* SoapTechstoreClient constructor.
* @param string $username
* @param string $password
*/
public function __construct($username, $password)
{
$serviceFactory = new DOIT_SOA_ITEM_ServiceFactory();
$this->doitSoaItemService = $serviceFactory->getInstance($username, $password);
}
/**
* {@inheritDoc}
*/
public function createProduct(Product $product)
{
$request = new InputParameters(null, null, null, null, null, null, null, null, null, null, null, null, null,
null, null, null, null);
$response = $this->doitSoaItemService->ITEM_CREATE($request);
return new Product(
$response->getP_ITEM_NO(),
$response->getP_ITEM_ID(),
$response->getP_STATUS(),
$response->$response->getP_ITEM_NO()
);
}
}
\ No newline at end of file
<?php
namespace edu\wisc\services\cbs;
/**
* Unit tests for {@link MockTechstoreCbsClient}
*
* @author Andrew Lundholm
*/
class MockTechstoreUnitTest extends \PHPUnit_Framework_TestCase
{
public function testCreateProduct()
{
$product = new Product("4229", "Test Mouse", "Test mouse used for clicking", "19.95");
$client = new MockTechstoreCbsClient();
$result = $client->createProduct($product);
$this->assertInstanceOf(Product::class, $result);
$this->assertTrue($result->getItemNumber() == "4229");
$this->assertTrue($result->getName() == "Test Mouse");
$this->assertTrue($result->getCost() == "19.95");
$this->assertTrue($result->getDescription() == "Test mouse used for clicking");
}
/**
* @expectedException edu\wisc\services\cbs\MockCbsClientException
*/
public function testCreateProductWithException()
{
$product = new Product("4229", "Test Mouse", "Test mouse used for clicking", "19.95");
$client = new MockTechstoreCbsClient(true);
$result = $client->createProduct($product);
}
}
\ No newline at end of file
<?php
use edu\wisc\services\cbs\DOIT_SOA_ITEM_ServiceFactory;
/**
* Integration tests for {@link DOIT_SOA_ITEM_Service}
*
......@@ -45,7 +46,7 @@ class cbsIntegrationTest extends PHPUnit_Framework_TestCase
* Call getPresentTerm, confirm successful response.
*/
public function testGetPresentTerm() {
$service = edu\wisc\services\cbs\cbsServiceFactory::getInstance(static::getConfigurationValue('cbs.username'), static::getConfigurationValue('cbs.password'));
$service = DOIT_SOA_ITEM_ServiceFactory::getInstance(static::getConfigurationValue('cbs.username'), static::getConfigurationValue('cbs.password'));
$request = new edu\wisc\services\cbs\InputParameters(null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null);
$response = $service->ITEM_CREATE($request);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment