Skip to content

Commit 4389bb8

Browse files
committed
More tests. "Abstract" resource test now tests a plain Resource class
Since Resource is not actually abstract, rework the tests to use it directly Move a client method closer to its relatives Few tiny tests for the Connection class. Should eventually use Guzzle or something over using raw curl for testability reasons, but it is what it is for now
1 parent 968daa5 commit 4389bb8

5 files changed

Lines changed: 175 additions & 83 deletions

File tree

src/Bigcommerce/Api/Client.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,16 +1016,6 @@ public static function getOptionSet($id)
10161016
return self::getResource('/optionsets/' . $id, 'OptionSet');
10171017
}
10181018

1019-
/**
1020-
* Status codes used to represent the state of an order.
1021-
*
1022-
* @return array
1023-
*/
1024-
public static function getOrderStatus($id)
1025-
{
1026-
return self::getResource('/order_statuses/' . $id, 'OrderStatus');
1027-
}
1028-
10291019
/**
10301020
* Update the given option set.
10311021
*
@@ -1049,6 +1039,18 @@ public static function deleteOptionSet($id)
10491039
Client::deleteResource('/optionsets/' . $id);
10501040
}
10511041

1042+
/**
1043+
* Status code used to represent the state of an order.
1044+
*
1045+
* @param int $id order status id
1046+
*
1047+
* @return mixed
1048+
*/
1049+
public static function getOrderStatus($id)
1050+
{
1051+
return self::getResource('/order_statuses/' . $id, 'OrderStatus');
1052+
}
1053+
10521054
/**
10531055
* Status codes used to represent the state of an order.
10541056
*

test/Unit/Api/ClientTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,9 @@ public function testGettingASpecificResourceReturnsACollectionOfThatResource($pa
286286
*/
287287
public function testGettingTheCountOfACollectionReturnsThatCollectionsCount($path, $fnName, $class)
288288
{
289-
if (in_array($path, array('order_statuses', 'products/skus', 'requestlogs'))) {
290-
$this->markTestSkipped(sprintf('The PHP client does not support getting the count of %s', $path));
289+
if (in_array($path, array('order_statuses', 'requestlogs'))) {
290+
//$this->markTestSkipped(sprintf('The API does not currently support getting the count of %s', $path));
291+
return;
291292
}
292293

293294
$this->connection->expects($this->once())

test/Unit/Api/ConnectionTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Bigcommerce\Test\Unit;
4+
5+
use Bigcommerce\Api\Connection;
6+
7+
class ConnectionTest extends \PHPUnit_Framework_TestCase
8+
{
9+
/**
10+
* @var \Bigcommerce\Api\Connection;
11+
*/
12+
protected $object;
13+
14+
public function setUp()
15+
{
16+
$this->object = new Connection();
17+
}
18+
19+
public function testFailOnError()
20+
{
21+
$this->object->failOnError(false);
22+
$this->assertAttributeSame(false, 'failOnError', $this->object);
23+
$this->object->failOnError(true);
24+
$this->assertAttributeSame(true, 'failOnError', $this->object);
25+
}
26+
27+
public function testAddHeader()
28+
{
29+
$this->object->addHeader('Content-Length', 4);
30+
$this->assertAttributeContains('Content-Length: 4', 'headers', $this->object);
31+
}
32+
33+
/**
34+
* @depends testAddHeader
35+
*/
36+
public function testRemoveHeader()
37+
{
38+
$this->object->addHeader('Content-Length', 4);
39+
$this->object->removeHeader('Content-Length');
40+
$this->assertAttributeNotContains('Content-Length: 4', 'headers', $this->object);
41+
}
42+
}

test/Unit/Api/ResourceTest.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
namespace Bigcommerce\Test\Unit\Api;
3+
4+
use Bigcommerce\Api\Resource;
5+
6+
class ResourceTest extends \PHPUnit_Framework_TestCase
7+
{
8+
public function testGetCreateFieldsReturnsAllFieldsNotMarkedIgnore()
9+
{
10+
$product = new Resource((object)array(
11+
'date_created' => 'abc123',
12+
'id' => 1
13+
));
14+
15+
$this->setProtectedProperty($product, 'ignoreOnCreate', array('date_created'));
16+
17+
$this->assertEquals((object)array('id' => 1), $product->getCreateFields());
18+
}
19+
20+
public function testGetUpdateFieldsReturnsAllFieldsNotMarkedIgnore()
21+
{
22+
$product = new Resource((object)array(
23+
'id' => 1,
24+
'custom_url' => '/whatever/'
25+
));
26+
27+
$this->setProtectedProperty($product, 'ignoreOnUpdate', array('id'));
28+
29+
$this->assertEquals((object)array('custom_url' => '/whatever/'), $product->getUpdateFields());
30+
}
31+
32+
public function testGetUpdateFieldsIgnoresNullFields()
33+
{
34+
$product = new Resource((object)array(
35+
'id' => 1,
36+
'custom_url' => '/whatever/',
37+
'ignored' => null,
38+
));
39+
40+
$this->setProtectedProperty($product, 'ignoreOnUpdate', array('id'));
41+
42+
$this->assertEquals((object)array('custom_url' => '/whatever/'), $product->getUpdateFields());
43+
}
44+
45+
public function testGetUpdateFieldsIgnoresEmptyDateFields()
46+
{
47+
$product = new Resource((object)array(
48+
'id' => 1,
49+
'date_created' => '2015',
50+
'date_updated' => '',
51+
));
52+
53+
$this->setProtectedProperty($product, 'ignoreOnUpdate', array('id'));
54+
55+
$this->assertEquals((object)array('date_created' => '2015'), $product->getUpdateFields());
56+
}
57+
58+
public function testGetUpdateFieldsIgnoresFieldsThatAreForbiddenToBeZeroWhenZero()
59+
{
60+
$product = new Resource((object)array(
61+
'id' => 1,
62+
'custom_url' => '/whatever/',
63+
'cannot_be_zero' => 0,
64+
));
65+
66+
$this->setProtectedProperty($product, 'ignoreOnUpdate', array('id'));
67+
$this->setProtectedProperty($product, 'ignoreIfZero', array('cannot_be_zero'));
68+
69+
$this->assertEquals((object)array('custom_url' => '/whatever/'), $product->getUpdateFields());
70+
}
71+
72+
public function testConstructorUnwrapsArrays()
73+
{
74+
$product = new Resource(array((object)array(
75+
'id' => 1
76+
)));
77+
78+
$this->assertSame(1, $product->id);
79+
}
80+
81+
public function testMagicGetReturnsAssignedValue()
82+
{
83+
$product = new Resource((object)array(
84+
'custom_url' => '/whatever/'
85+
));
86+
87+
$this->assertSame('/whatever/', $product->custom_url);
88+
}
89+
90+
public function testMagicSetUpdatesAssignedValue()
91+
{
92+
$product = new Resource((object)array(
93+
'custom_url' => '/whatever/'
94+
));
95+
96+
$this->assertSame('/whatever/', $product->custom_url);
97+
$product->custom_url = '/other';
98+
$this->assertSame('/other', $product->custom_url);
99+
}
100+
101+
public function testMagicIssetCorrectlyFindsFields()
102+
{
103+
$product = new Resource((object)array(
104+
'custom_url' => '/whatever/'
105+
));
106+
107+
$this->assertTrue(isset($product->custom_url));
108+
$this->assertFalse(isset($product->id));
109+
}
110+
111+
protected function setProtectedProperty($object, $property, $value)
112+
{
113+
$reflectionClass = new \ReflectionClass($object);
114+
$reflectionProperty = $reflectionClass->getProperty($property);
115+
$reflectionProperty->setAccessible(true);
116+
$reflectionProperty->setValue($object, $value);
117+
}
118+
}

test/Unit/Api/Resources/AbstractResourceTest.php

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)