/** * Implements DrupalEntityControllerInterface::load(). * * @param array $names * @param array $conditions * * @return array */ public function load($ids = array(), $conditions = array('show_private' => FALSE)) { static $string_keys = array('description', 'displayName', 'name', 'quota', 'quotaInterval', 'quotaTimeUnit'); static $array_keys = array('apiResources', 'attributes', 'environments', 'proxies', 'scopes'); if (!isset($conditions['show_private'])) { $conditions['show_private'] = FALSE; } $list = array(); foreach (self::getOrgs($conditions) as $org) { $api_product = new Apigee\ManagementAPI\APIProduct(devconnect_default_org_config($org)); if (empty($ids)) { try { $sub_list = $api_product->listProducts($conditions['show_private']); if (!empty($sub_list)) { foreach ($list as $p) { $this->productCache[$p->getName()] = $p; } } $list += $sub_list; } catch (Apigee\Exceptions\ResponseException $e) { } } else { foreach ($ids as $name) { if (array_key_exists($name, $this->productCache)) { $list[] = $this->productCache[$name]; } else { $my_product = clone $api_product; try { $my_product->load($name); $this->productCache[$my_product->getName()] = $my_product; if ($my_product->isPublic() || $conditions['show_private']) { $list[] = $my_product; } } catch (Apigee\Exceptions\ResponseException $e) { // do nothing } } } } } $return = array(); foreach ($list as $api_product) { $array = $api_product->toArray(); $array['isPublic'] = $api_product->isPublic(); $array['orgName'] = $api_product->getConfig()->orgName; // Ensure that non-null values exist for non-nullable fields. foreach ($string_keys as $string_key) { if (!array_key_exists($string_key, $array)) { $array[$string_key] = ''; } } foreach ($array_keys as $array_key) { if (!array_key_exists($array_key, $array)) { $array[$array_key] = array(); } } $return[$api_product->getName()] = new ApiProductEntity($array); } return $return; }
/** * @depends testApiProductList */ public function testApiProductLoad() { $api_product = new APIProduct(self::$orgConfig); try { $list = $api_product->listProducts(); } catch (\Exception $e) { $this->fail(); return; } $this->assertNotEmpty($list); // Pick a random item from the list. No need to shuffle if list has // only 1 member. if (count($list) > 1) { shuffle($list); } $item = reset($list); try { $api_product->load($item->getName()); } catch (\Exception $e) { $this->fail($e->getCode() . ': ' . $e->getMessage()); } $this->assertNotEmpty($api_product->getName()); }
public function testAppCRUD() { $developer = new Developer(self::$orgConfig); $mail = 'phpunit-' . $this->randomString() . '@example.com'; // Create a developer to test on $developer->blankValues(); $developer->setEmail($mail); $developer->setFirstName($this->randomString()); $developer->setLastName($this->randomString()); $developer->setUserName($this->randomString()); try { $developer->save(); } catch (\Exception $e) { $this->fail('Cannot save developer at create time: [' . $e->getCode() . '] ' . $e->getMessage()); } $apiproduct = new APIProduct(self::$orgConfig); $products = $apiproduct->listProducts(); shuffle($products); $product = array_shift($products); $new_product = array_shift($products); // Begin create $app = new DeveloperApp(self::$orgConfig, $mail); $app->setName('phpunit test'); $app->setAccessType('read'); $app->setCallbackUrl('http://example.com/'); $app->setApiProducts(array($product->getName())); $app->setAttribute('foo', 'bar'); try { $app->save(); } catch (\Exception $e) { $this->fail('Cannot save app at creation time'); } $this->assertNotEmpty($app->getConsumerKey()); $this->assertNotEmpty($app->getAppId()); $this->assertEquals('bar', $app->getAttribute('foo')); // End create // Begin load $app->blankValues(); try { $app->load('phpunit test'); } catch (\Exception $e) { $this->fail('Cannot load app.'); return; } $this->assertNotEmpty($app->getConsumerKey()); $this->assertEquals($app->getDeveloperId(), $developer->getDeveloperId()); // End load // Begin update $app->setAttribute('foo', 'baz'); try { $app->save(FALSE); } catch (\Exception $e) { $this->fail('Cannot save app at update time'); return; } $app->blankValues(); try { $app->load('phpunit test'); } catch (\Exception $e) { $this->fail('Cannot reload app after update'); return; } $this->assertEquals('baz', $app->getAttribute('foo')); // End update // Update key $key = $app->getConsumerKey(); $new_product_name = $new_product->getName(); $app->setApiProducts(array($new_product_name)); try { $app->save(); } catch (\Exception $e) { $this->fail('Cannot save app when updating API Products.'); } $api_products = $app->getApiProducts(); $cred_api_products = $app->getCredentialApiProducts(); if (count($api_products) != 1 || $api_products[0] != $new_product_name) { $this->fail('Failed to update API Products list'); } if (count($cred_api_products) != 1 || $cred_api_products[0]['apiproduct'] != $new_product_name) { $this->fail('Failed to update Credential API Products list'); } $this->assertEquals($key, $app->getConsumerKey(), 'Consumer Key changed when API Product changed.'); // End update key // Create key $key = $this->randomString(16); $secret = $this->randomString(16); try { $app->createKey($key, $secret); } catch (\Exception $e) { $this->fail('Cannot create key: ' . $e->getMessage()); return; } $app->blankValues(); $app->load('phpunit test'); $this->assertEquals($key, $app->getConsumerKey(), 'Consumer Key changed to our custom value.'); // End create key // Begin delete try { $app->delete(); } catch (\Exception $e) { $this->fail('Cannot delete app'); } $app->blankValues(); try { $app->load($mail); // If we succeed in the load, the developer was not deleted. $this->fail('App deletion failed.'); } catch (\Exception $e) { $this->assertEquals(404, $e->getCode()); } // End delete // Clean up try { $developer->delete(); } catch (\Exception $e) { } }