diff --git a/new_relic_insights.info b/new_relic_insights.info index 129eeae..e8f2ad2 100644 --- a/new_relic_insights.info +++ b/new_relic_insights.info @@ -14,5 +14,7 @@ files[] = src/clients_connection_new_relic_insights_query.inc files[] = src/InsightRemoteEntityQuery.inc ; Test classes. +files[] = test/src/NewRelicInsightsUnitTestBase.test files[] = test/src/InsightRemoteEntityQuery.test files[] = test/src/clients_connection_new_relic_insights_query.test +files[] = test/src/Insight.test diff --git a/test/src/Insight.test b/test/src/Insight.test new file mode 100644 index 0000000..aff9c18 --- /dev/null +++ b/test/src/Insight.test @@ -0,0 +1,87 @@ + 'value', + 'key2' => 3.14, + 'key3' => TRUE, + 'key4' => '', + ); + + public static function getInfo() { + return array( + 'name' => 'New Relic Insights: Insight Tests', + 'description' => 'Test expected behavior in the Insight entity class.', + 'group' => 'New Relic Insights', + ); + } + + public function setUp() { + parent::setUp(); + + // Manually load classes to avoid bootstrapping / checking registry. + // Order matters! + $this->require_once_insanity('class', 'Entity', __DIR__ . '/../../../entity/includes/entity.inc'); + require_once(__DIR__ . '/../../src/Insight.inc'); + require_once(__DIR__ . '/InsightWrapper.php'); + + $this->setUpEntityInfo(); + $this->insight = new InsightWrapper(array( + 'entity_data' => (object) $this->entity_data, + ), 'insight'); + } + + /** + * Tests the Insight::__isset() magic method. + */ + function testInsightPropertyIsset() { + // Ensure a non-existent key is marked as such. + $not_key = $this->randomName(); + $this->assertTrue(!isset($this->insight->$not_key), format_string('Insight "isset" magic method returns FALSE for non-existent key %key.', array( + '%key' => $not_key, + ))); + + // Iterate through defined custom entity data properties. + foreach ($this->entity_data as $key => $value) { + $this->assertTrue(isset($this->insight->$key), format_string('Insight "isset" magic method returns TRUE for existing key %key.', array( + '%key' => $key, + ))); + } + } + + /** + * Tests the Insight::__get() magic method. + */ + function testInsightPropertyGet() { + // Ensure a non-existent key is returned as NULL. + $not_key = $this->randomName(); + $this->assertIdentical($this->insight->$not_key, NULL, format_string('Insight "get" magic method returns NULL for non-existent key %key.', array( + '%key' => $not_key, + ))); + + // Iterate through defined custom entity data properties. + foreach ($this->entity_data as $key => $value) { + $this->assertIdentical($this->insight->$key, $value, format_string('Insight "get" magic method returns %value for existing key %key.', array( + '%value' => $value, + '%key' => $key, + ))); + } + } + +} diff --git a/test/src/InsightRemoteEntityQuery.test b/test/src/InsightRemoteEntityQuery.test index 917b751..56ade58 100644 --- a/test/src/InsightRemoteEntityQuery.test +++ b/test/src/InsightRemoteEntityQuery.test @@ -5,7 +5,7 @@ * Contains tests for the InsightRemoteEntityQuery class. */ -class NewRelicInsightsRemoteEntityQueryTestCase extends DrupalUnitTestCase { +class NewRelicInsightsRemoteEntityQueryTestCase extends NewRelicINsightsUnitTestBase { /** * Mock connection, to be injected into the remote entity query class. diff --git a/test/src/InsightWrapper.php b/test/src/InsightWrapper.php new file mode 100644 index 0000000..24bb820 --- /dev/null +++ b/test/src/InsightWrapper.php @@ -0,0 +1,20 @@ +entityInfo = $entity_info['insight']; + $this->idKey = $this->entityInfo['entity keys']['id']; + $this->nameKey = isset($this->entityInfo['entity keys']['name']) ? $this->entityInfo['entity keys']['name'] : $this->idKey; + $this->statusKey = empty($info['entity keys']['status']) ? 'status' : $info['entity keys']['status']; + } + + +} diff --git a/test/src/NewRelicInsightsUnitTestBase.test b/test/src/NewRelicInsightsUnitTestBase.test new file mode 100644 index 0000000..97b4fbf --- /dev/null +++ b/test/src/NewRelicInsightsUnitTestBase.test @@ -0,0 +1,66 @@ + array('remote_entity' => 'remote_entity')); + + // Inject our defined entity info from the main .module file. + $this->require_once_insanity('function', 'new_relic_insights_entity_info', __DIR__ .'/../../new_relic_insights.module'); + $this->entityInfo = new_relic_insights_entity_info(TRUE); + } + + /** + * Attempts to allow files to be included / required the same way across the + * drupal.org testbot infrastructure and local testing environments. + */ + protected function require_once_insanity($type, $target, $file) { + try { + switch ($type) { + case 'class': + if (!class_exists($target)) { + require_once($file); + } + break; + + case 'interface': + if (!interface_exists($target)) { + require_once($file); + } + break; + + case 'function': + if (!function_exists($target)) { + require_once($file); + } + break; + } + } + // If we're here, we're on d.o, and it's safe to assume the file has not yet + // been included. So just include it. + catch (PDOException $exception) { + require_once($file); + } + } + +} diff --git a/test/src/clients_connection_new_relic_insights_query.test b/test/src/clients_connection_new_relic_insights_query.test index 0404a32..42ffcf4 100644 --- a/test/src/clients_connection_new_relic_insights_query.test +++ b/test/src/clients_connection_new_relic_insights_query.test @@ -5,13 +5,7 @@ * Contains tests for the clients_connection_new_relic_insights_query class. */ -class NewRelicInsightsClientsConnectionTestCase extends DrupalUnitTestCase { - - /** - * Entity info for the Insight entity. - * @var array - */ - protected $entityInfo = array(); +class NewRelicInsightsClientsConnectionTestCase extends NewRelicINsightsUnitTestBase { /** * A fresh New Relic query client connection for tests. @@ -42,55 +36,6 @@ class NewRelicInsightsClientsConnectionTestCase extends DrupalUnitTestCase { ); } - /** - * Some insane drupal_static() acrobatics to get our tests to run without - * exception. - */ - protected function setUpEntityinfo() { - // Force our module to believe Insights is queryable. - $GLOBALS['conf']['new_relic_insights_account_id'] = TRUE; - $GLOBALS['conf']['new_relic_insights_query_key'] = 'omGWtfbBq1337='; - $system_list = &drupal_static('system_list', array()); - $system_list = array('module_enabled' => array('remote_entity' => 'remote_entity')); - - // Inject our defined entity info from the main .module file. - $this->require_once_insanity('function', 'new_relic_insights_entity_info', __DIR__ .'/../../new_relic_insights.module'); - $this->entityInfo = new_relic_insights_entity_info(TRUE); - } - - /** - * Attempts to allow files to be included / required the same way across the - * drupal.org testbot infrastructure and local testing environments. - */ - protected function require_once_insanity($type, $target, $file) { - try { - switch ($type) { - case 'class': - if (!class_exists($target)) { - require_once($file); - } - break; - - case 'interface': - if (!interface_exists($target)) { - require_once($file); - } - break; - - case 'function': - if (!function_exists($target)) { - require_once($file); - } - break; - } - } - // If we're here, we're on d.o, and it's safe to assume the file has not yet - // been included. So just include it. - catch (PDOException $exception) { - require_once($file); - } - } - public function setUp() { parent::setUp(); @@ -103,7 +48,7 @@ class NewRelicInsightsClientsConnectionTestCase extends DrupalUnitTestCase { require_once(__DIR__ . '/clients_connection_wrapper.php'); // Set up entity info and a client connection to test with. - $this->setUpEntityinfo(); + $this->setUpEntityInfo(); $this->clientConnection = new clients_connection_wrapper(array(), 'insight'); $this->clientConnection->endpoint = $this->endpoint; $this->clientConnection->configuration = $this->clientCredentials;