Skip to content

Commit

Permalink
Added tests for the controller factory and products
Browse files Browse the repository at this point in the history
  • Loading branch information
johannez committed Sep 24, 2013
1 parent b82f753 commit e42ac71
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 2 deletions.
23 changes: 23 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite>
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<var name="chargify_domain" value="2tabs"/>
<var name="chargify_api_key" value="R3XvWkDgL8F7ENxpxL"/>
</php>
</phpunit>
10 changes: 8 additions & 2 deletions src/Chargify/Resource/AbstractResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public function __construct($data, $processData = TRUE) {
throw new \Exception(t('Cannot create a resource instance without raw data from the API.'));
}

// Set the default time zone to EST as the local system is unreliable.
date_default_timezone_set('America/New_York');

$this->data = $data;

if ($processData) {
Expand Down Expand Up @@ -66,10 +69,13 @@ public function processData() {
}
}
else {
if (isset($filter[$key]))
if (isset($filter[$key])) {
$this->{$key} = $filter[$key]($value);
else
}
else {
$this->{$key} = $value;
}

}
}
}
Expand Down
35 changes: 35 additions & 0 deletions tests/Chargify/Tests/ControllerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Chargify\Tests;
use \Chargify\Controller\Factory;
use \Chargify\Controller\Product;
use \PHPUnit_Framework_TestCase;

class ControllerFactoryTest extends PHPUnit_Framework_TestCase {

public function testDomainIsSet() {
$this->assertTrue($GLOBALS['chargify_domain'] !== 'DOMAIN', 'Domain is not set. Please update the phpunit.xml file.');
}

public function testApiKeyIsSet() {
$this->assertTrue($GLOBALS['chargify_api_key'] !== 'API_KEY', 'API key is not set. Please update the phpunit.xml file.');
}

/**
* @depends testDomainIsSet
* @depends testApiKeyIsSet
*/
public function testCanCreateController() {
$resource_types = array('component', 'coupon', 'customer', 'product', 'subscription', 'transaction');

foreach ($resource_types as $type) {
// Just take product as they are all the same.
$controller = Factory::build($type, $GLOBALS['chargify_domain'], $GLOBALS['chargify_api_key']);

$this->assertInstanceOf('Chargify\\Controller\\' . ucfirst($type), $controller, 'Can not initialize controller.');
}


}

}
79 changes: 79 additions & 0 deletions tests/Chargify/Tests/Controllers/ProductControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Chargify\Tests;
use \Chargify\Controller\Factory;
use \Chargify\Controller\Product;
use \Chargify\Resource\ProductResource;
use \PHPUnit_Framework_TestCase;

class ProductControllerTest extends PHPUnit_Framework_TestCase {

public function testGetAll() {
$controller = Factory::build('product', $GLOBALS['chargify_domain'], $GLOBALS['chargify_api_key']);
$this->assertNotEmpty($controller->getAll());
}

/**
* @depends testGetAll
*/
public function testGetByFamily() {
$controller = Factory::build('product', $GLOBALS['chargify_domain'], $GLOBALS['chargify_api_key']);
$products = $controller->getAll();

$this->assertNotEmpty($controller->getByFamily($products[0]->product_family->id));
}

/**
* @depends testGetAll
*/
public function testGetById() {
$controller = Factory::build('product', $GLOBALS['chargify_domain'], $GLOBALS['chargify_api_key']);
$products = $controller->getAll();

$response = $controller->getById($products[0]->id);

$this->assertNotNull($response);
$this->assertInstanceOf('Chargify\Resource\ProductResource', $response);
}

/**
* @depends testGetAll
*/
public function testGetByHandle() {
$controller = Factory::build('product', $GLOBALS['chargify_domain'], $GLOBALS['chargify_api_key']);
$products = $controller->getAll();

$response = $controller->getByHandle($products[0]->handle);

$this->assertNotNull($response);
$this->assertInstanceOf('Chargify\Resource\ProductResource', $response);
}

/**
* @depends testGetByFamily
*/
public function testCreateProduct() {

$controller = Factory::build('product', $GLOBALS['chargify_domain'], $GLOBALS['chargify_api_key']);
$products = $controller->getAll();

$data = array(
'product' => array(
'name' => 'Basic Plan 123',
'handle' => 'basic',
'description' => 'This is our basic plan.',
'accounting_code' => '123',
'request_credit_card' => true,
'price_in_cents' => 1000,
'interval' => 1,
'interval_unit' => 'month'
)
);

$response = $controller->create($products[0]->product_family->id, $data);

$this->assertNotNull($response);
$this->assertInstanceOf('Chargify\Resource\ProductResource', $response);
}

}

0 comments on commit e42ac71

Please sign in to comment.