From 34e21ce8035cb05d825a09f417c1385cc6e9b344 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 9 Feb 2016 13:55:13 -0500 Subject: [PATCH] CRM-17867 - Api test fixes --- Civi/API/Request.php | 6 +- tests/phpunit/Civi/API/KernelTest.php | 2 +- tests/phpunit/Civi/API/RequestTest.php | 115 +----------------- .../Subscriber/TransactionSubscriberTest.php | 18 +-- .../phpunit/api/v3/SyntaxConformanceTest.php | 4 +- 5 files changed, 20 insertions(+), 125 deletions(-) diff --git a/Civi/API/Request.php b/Civi/API/Request.php index 49816b52ec..6358d34a85 100644 --- a/Civi/API/Request.php +++ b/Civi/API/Request.php @@ -73,7 +73,11 @@ class Request { return $apiRequest; case 4: - $apiCall = call_user_func(array("Civi\\Api4\\$entity", $action)); + $callable = array("Civi\\Api4\\$entity", $action); + if (!is_callable($callable)) { + throw new Exception\NotImplementedException("API ($entity, $action) does not exist (join the API team and implement it!)"); + } + $apiCall = call_user_func($callable); $apiRequest['id'] = self::$nextId++; unset($params['version']); foreach ($params as $name => $param) { diff --git a/tests/phpunit/Civi/API/KernelTest.php b/tests/phpunit/Civi/API/KernelTest.php index 89b792fcc9..00168d5a30 100644 --- a/tests/phpunit/Civi/API/KernelTest.php +++ b/tests/phpunit/Civi/API/KernelTest.php @@ -6,7 +6,7 @@ use \Symfony\Component\EventDispatcher\EventDispatcher; /** */ class KernelTest extends \CiviUnitTestCase { - const MOCK_VERSION = 99; + const MOCK_VERSION = 3; /** * @var array(int => array('name' => string $eventName, 'type' => string $className)) diff --git a/tests/phpunit/Civi/API/RequestTest.php b/tests/phpunit/Civi/API/RequestTest.php index 42fdaee79f..24e5c616b5 100644 --- a/tests/phpunit/Civi/API/RequestTest.php +++ b/tests/phpunit/Civi/API/RequestTest.php @@ -5,112 +5,6 @@ namespace Civi\API; */ class RequestTest extends \CiviUnitTestCase { - /** - * @return array - */ - public function v4options() { - $cases = array(); // array(0 => $requestParams, 1 => $expectedOptions, 2 => $expectedData, 3 => $expectedChains) - $cases[] = array( - array('version' => 4), // requestParams - array(), // expectedOptions - array(), // expectedData - array(), // expectedChains - ); - $cases[] = array( - array('version' => 4, 'debug' => TRUE), // requestParams - array('debug' => TRUE), // expectedOptions - array(), // expectedData - array(), // expectedChains - ); - $cases[] = array( - array('version' => 4, 'format.is_success' => TRUE), // requestParams - array('format' => 'is_success'), // expectedOptions - array(), // expectedData - array(), // expectedChains - ); - $cases[] = array( - array( - 'version' => 4, - 'option.limit' => 15, - 'option.foo' => array('bar'), - 'options' => array('whiz' => 'bang'), - 'optionnotreally' => 'data', - ), // requestParams - array('limit' => 15, 'foo' => array('bar'), 'whiz' => 'bang'), // expectedOptions - array('optionnotreally' => 'data'), // expectedData - array(), // expectedChains - ); - $cases[] = array( - array( - 'version' => 4, - 'return' => array('field1', 'field2'), - 'return.field3' => 1, - 'return.field4' => 0, - 'returnontreally' => 'data', - ), // requestParams - array('return' => array('field1', 'field2', 'field3')), // expectedOptions - array('returnontreally' => 'data'), // expectedData - array(), // expectedChains - ); - $cases[] = array( - array('version' => 4, 'foo' => array('bar'), 'whiz' => 'bang'), // requestParams - array(), // expectedOptions - array('foo' => array('bar'), 'whiz' => 'bang'), // expectedData - array(), // expectedChains - ); - $cases[] = array( - array('version' => 4, 'api.foo.bar' => array('whiz' => 'bang')), // requestParams - array(), // expectedOptions - array(), // expectedData - array('api.foo.bar' => array('whiz' => 'bang')), // expectedChains - ); - $cases[] = array( - array( - 'version' => 4, - 'option.limit' => 15, - 'options' => array('whiz' => 'bang'), - 'somedata' => 'data', - 'moredata' => array('woosh'), - 'return.field1' => 1, - 'return' => array('field2'), - 'api.first' => array('the first call'), - 'api.second' => array('the second call'), - ), // requestParams - array('limit' => 15, 'whiz' => 'bang', 'return' => array('field1', 'field2')), // expectedOptions - array('somedata' => 'data', 'moredata' => array('woosh')), // expectedData - array('api.first' => array('the first call'), 'api.second' => array('the second call')), // expectedChains - ); - return $cases; - } - - /** - * @param array $inputParams - * @param $expectedOptions - * @param $expectedData - * @param $expectedChains - * @dataProvider v4options - */ - public function testCreateRequest_v4Options($inputParams, $expectedOptions, $expectedData, $expectedChains) { - $apiRequest = Request::create('MyEntity', 'MyAction', $inputParams, NULL); - $this->assertEquals($expectedOptions, $apiRequest['options']->getArray()); - $this->assertEquals($expectedData, $apiRequest['data']->getArray()); - $this->assertEquals($expectedChains, $apiRequest['chains']); - } - - /** - * @expectedException \API_Exception - */ - public function testCreateRequest_v4BadEntity() { - Request::create('Not!Valid', 'create', array('version' => 4), NULL); - } - - /** - * @expectedException \API_Exception - */ - public function testCreateRequest_v4BadAction() { - Request::create('MyEntity', 'bad!action', array('version' => 4), NULL); - } - /** * @return array */ @@ -132,10 +26,6 @@ class RequestTest extends \CiviUnitTestCase { array('u_f_match', 'get Something', 3), array('UFMatch', 'get_something', 3), ); - $cases[] = array( - array('MyEntity', 'MyAction', 4), - array('MyEntity', 'myAction', 4), - ); return $cases; } @@ -156,6 +46,7 @@ class RequestTest extends \CiviUnitTestCase { */ public function invalidEntityActionPairs() { $cases = array(); + $cases[] = array('Not!Valid', 'create', 4); $cases[] = array('My+Entity', 'MyAction', 4); $cases[] = array('My Entity', 'MyAction', 4); $cases[] = array('2MyEntity', 'MyAction', 4); @@ -167,11 +58,11 @@ class RequestTest extends \CiviUnitTestCase { /** * @dataProvider invalidEntityActionPairs - * @expectedException \API_Exception + * @expectedException \Civi\API\Exception\NotImplementedException * @param $inEntity * @param $inAction * @param $inVersion - * @throws \API_Exception + * @throws \Civi\API\Exception\NotImplementedException */ public function testCreateRequest_InvalidEntityAction($inEntity, $inAction, $inVersion) { Request::create($inEntity, $inAction, array('version' => $inVersion), NULL); diff --git a/tests/phpunit/Civi/API/Subscriber/TransactionSubscriberTest.php b/tests/phpunit/Civi/API/Subscriber/TransactionSubscriberTest.php index 5fb9a392a9..9ac2e23476 100644 --- a/tests/phpunit/Civi/API/Subscriber/TransactionSubscriberTest.php +++ b/tests/phpunit/Civi/API/Subscriber/TransactionSubscriberTest.php @@ -67,15 +67,15 @@ class TransactionSubscriberTest extends \CiviUnitTestCase { FALSE, ); - $r[] = array(4, 'Widget', 'get', array(), FALSE, FALSE, FALSE); - $r[] = array(4, 'Widget', 'create', array(), TRUE, FALSE, FALSE); - - $r[] = array(4, 'Widget', 'create', array('is_transactional' => TRUE), TRUE, FALSE, FALSE); - $r[] = array(4, 'Widget', 'create', array('is_transactional' => FALSE), FALSE, FALSE, FALSE); - $r[] = array(4, 'Widget', 'create', array('is_transactional' => 'nest'), TRUE, FALSE, TRUE); - - $r[] = array(4, 'Widget', 'create', array('options' => array('force_rollback' => TRUE)), TRUE, TRUE, TRUE); - $r[] = array(4, 'Widget', 'create', array('options' => array('force_rollback' => FALSE)), TRUE, FALSE, FALSE); + // $r[] = array(4, 'Widget', 'get', array(), FALSE, FALSE, FALSE); + // $r[] = array(4, 'Widget', 'create', array(), TRUE, FALSE, FALSE); + // + // $r[] = array(4, 'Widget', 'create', array('is_transactional' => TRUE), TRUE, FALSE, FALSE); + // $r[] = array(4, 'Widget', 'create', array('is_transactional' => FALSE), FALSE, FALSE, FALSE); + // $r[] = array(4, 'Widget', 'create', array('is_transactional' => 'nest'), TRUE, FALSE, TRUE); + // + // $r[] = array(4, 'Widget', 'create', array('options' => array('force_rollback' => TRUE)), TRUE, TRUE, TRUE); + // $r[] = array(4, 'Widget', 'create', array('options' => array('force_rollback' => FALSE)), TRUE, FALSE, FALSE); return $r; } diff --git a/tests/phpunit/api/v3/SyntaxConformanceTest.php b/tests/phpunit/api/v3/SyntaxConformanceTest.php index 1381f3e22a..238fdea644 100644 --- a/tests/phpunit/api/v3/SyntaxConformanceTest.php +++ b/tests/phpunit/api/v3/SyntaxConformanceTest.php @@ -716,7 +716,7 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase { } $result = civicrm_api($Entity, 'Get', array()); $this->assertEquals(1, $result['is_error']); - $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']); + $this->assertContains("Unknown api version", $result['error_message']); } /** @@ -1354,7 +1354,7 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase { } $result = civicrm_api($Entity, 'Delete', array()); $this->assertEquals(1, $result['is_error']); - $this->assertContains("Mandatory key(s) missing from params array", $result['error_message']); + $this->assertContains("Unknown api version", $result['error_message']); } /** -- 2.25.1