ChadoRecordTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace Tests;
  3. use PHPUnit\Exception;
  4. use StatonLab\TripalTestSuite\DBTransaction;
  5. use StatonLab\TripalTestSuite\TripalTestCase;
  6. module_load_include('inc', 'tripal_chado', 'includes/api/ChadoRecord');
  7. class ChadoRecordTest extends TripalTestCase {
  8. use DBTransaction;
  9. /**
  10. * Data provider. A variety of chado records.
  11. *
  12. * @return array
  13. */
  14. public function recordProvider() {
  15. //table, factory or NULL, record_id or NULL
  16. $a = factory('chado.feature')->create();
  17. $b = factory('chado.organism')->create();
  18. return [
  19. ['feature', NULL, NULL],
  20. ['feature', $a->feature_id, $a],
  21. ['organism', $b->organism_id, $b],
  22. ];
  23. }
  24. /**
  25. * Tests that the class can be initiated with or without a record specified
  26. *
  27. * @group api
  28. * @group chado
  29. * @group wip
  30. * @dataProvider recordProvider
  31. */
  32. public function testInitClass($table, $id, $factory) {
  33. $record = new \ChadoRecord($table, $id);
  34. $this->assertNotNull($record);
  35. }
  36. /**
  37. * @group api
  38. * @group chado
  39. * @group wip
  40. * @throws \Exception
  41. * @dataProvider recordProvider
  42. */
  43. public function testGetTable($table, $id, $factory) {
  44. $record = new \ChadoRecord($table, $id);
  45. $this->assertEquals($table, $record->getTable());
  46. }
  47. /**
  48. * @group wip
  49. * @group api
  50. * @group chado
  51. * @dataProvider recordProvider
  52. *
  53. * @throws \Exception
  54. */
  55. public function testGetID($table, $id, $factory) {
  56. $record = new \ChadoRecord($table, $id);
  57. $returned_id = $record->getID();
  58. if ($id) {
  59. $this->assertEquals($id, $returned_id);
  60. }
  61. else {
  62. $this->assertNull($returned_id);
  63. }
  64. }
  65. /**
  66. * @group api
  67. * @group wip
  68. * @group chado
  69. * @dataProvider recordProvider
  70. *
  71. *
  72. */
  73. public function testGetValues($table, $id, $factory) {
  74. $record = new \ChadoRecord($table, $id);
  75. if (!$id) {
  76. $returned_vals = $record->getValues();
  77. $this->assertEmpty($returned_vals);
  78. }
  79. else {
  80. $values = $record->getValues();
  81. $this->assertNotEmpty($values);
  82. foreach ($factory as $key => $value) {
  83. $this->assertArrayHasKey($key, $values);
  84. $this->assertEquals($value, $values[$key]);
  85. }
  86. }
  87. }
  88. /**
  89. * @group api
  90. * @group wip
  91. * @group chado
  92. * @dataProvider recordProvider
  93. *
  94. */
  95. public function testGetValue($table, $id, $factory) {
  96. $record = new \ChadoRecord($table, $id);
  97. if (!$id) {
  98. $returned_id = $record->getValue($table . '_id');
  99. $this->assertNull($returned_id);
  100. }
  101. else {
  102. foreach ($factory as $key => $value) {
  103. $returned_value = $record->getValue($key);
  104. $this->assertEquals($value, $returned_value);
  105. }
  106. }
  107. }
  108. /**
  109. * @group wip
  110. * @group chado
  111. * @group api
  112. * @dataProvider recordProvider
  113. */
  114. public function testFind($table, $id, $factory) {
  115. $record = new \ChadoRecord($table);
  116. $values = (array) $factory;
  117. $record->setValues($values);
  118. if ($id) {
  119. $found = $record->find();
  120. $this->assertNotNull($found);
  121. $this->assertEquals(1, $found);
  122. }
  123. else {
  124. //There isnt a record in the DB, so find should throw an exception
  125. $record->setValue($table . '_id', 'unfindable');
  126. $this->expectException(Exception);
  127. $found = $record->find();
  128. }
  129. }
  130. /**
  131. * This test will not use providers.
  132. */
  133. public function testSetValue() {
  134. $record = new \ChadoRecord('feature');
  135. $values = [];
  136. $record->setValues($values);
  137. }
  138. }