ChadoRecordTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace Tests;
  3. use PHPUnit\Exception;
  4. use Faker\Factory;
  5. use StatonLab\TripalTestSuite\DBTransaction;
  6. use StatonLab\TripalTestSuite\TripalTestCase;
  7. module_load_include('inc', 'tripal_chado', 'includes/api/ChadoRecord');
  8. class ChadoRecordTest extends TripalTestCase {
  9. use DBTransaction;
  10. /**
  11. * Data provider. A variety of chado records.
  12. *
  13. * @return array
  14. */
  15. public function recordProvider() {
  16. //table, factory or NULL, record_id or NULL
  17. $faker = \Faker\Factory::create();
  18. $analysis = [
  19. 'name' => $faker->word,
  20. 'description' => $faker->text,
  21. 'program' => $faker->word,
  22. 'programversion' => $faker->word,
  23. ];
  24. $organism = [
  25. 'genus' => $faker->word,
  26. 'species' => $faker->word,
  27. 'common_name' => $faker->word,
  28. ];
  29. return [
  30. ['analysis', $analysis],
  31. ['organism', $organism],
  32. ];
  33. }
  34. /**
  35. * Tests that the class can be initiated with or without a record specified
  36. *
  37. * @group api
  38. * @group chado
  39. * @group wip
  40. * @dataProvider recordProvider
  41. */
  42. public function testInitClass($table, $values) {
  43. $record = new \ChadoRecord($table);
  44. $this->assertNotNull($record);
  45. $chado_record = factory('chado.' . $table)->create($values);
  46. $record_column = $table.'_id';
  47. $record = new \ChadoRecord($table, $chado_record->$record_column);
  48. $this->assertNotNull($record);
  49. }
  50. /**
  51. * @group api
  52. * @group chado
  53. * @group wip
  54. * @throws \Exception
  55. * @dataProvider recordProvider
  56. */
  57. public function testGetTable($table, $values) {
  58. $record = new \ChadoRecord($table);
  59. $this->assertEquals($table, $record->getTable());
  60. }
  61. /**
  62. * @group wip
  63. * @group api
  64. * @group chado
  65. * @dataProvider recordProvider
  66. *
  67. * @throws \Exception
  68. */
  69. public function testGetID($table, $values) {
  70. $chado_record = factory('chado.' . $table)->create();
  71. $record_column = $table.'_id';
  72. $id = $chado_record->$record_column;
  73. $record = new \ChadoRecord($table, $id);
  74. $returned_id = $record->getID();
  75. $this->assertEquals($id, $returned_id);
  76. }
  77. /**
  78. * @group api
  79. * @group wip
  80. * @group chado
  81. * @dataProvider recordProvider
  82. *
  83. *
  84. */
  85. public function testGetValues($table, $values) {
  86. $chado_record = factory('chado.' . $table)->create($values);
  87. $record_column = $table.'_id';
  88. $id = $chado_record->$record_column;
  89. $record = new \ChadoRecord($table, $id);
  90. $values = $record->getValues();
  91. $this->assertNotEmpty($values);
  92. foreach ($values as $key => $value) {
  93. $this->assertArrayHasKey($key, $values);
  94. $this->assertEquals($value, $values[$key]);
  95. }
  96. }
  97. /**
  98. * @group api
  99. * @group wip
  100. * @group chado
  101. * @dataProvider recordProvider
  102. *
  103. */
  104. public function testGetValue($table, $values) {
  105. $chado_record = factory('chado.' . $table)->create($values);
  106. $record_column = $table.'_id';
  107. $id = $chado_record->$record_column;
  108. $record = new \ChadoRecord($table, $id);
  109. foreach ($values as $key => $value) {
  110. $returned_value = $record->getValue($key);
  111. $this->assertEquals($value, $returned_value);
  112. }
  113. }
  114. /**
  115. * @group wip
  116. * @group chado
  117. * @group api
  118. * @dataProvider recordProvider
  119. */
  120. public function testFind($table, $values) {
  121. $chado_record = factory('chado.' . $table)->create($values);
  122. $record_column = $table.'_id';
  123. $id = $chado_record->$record_column;
  124. $record = new \ChadoRecord($table);
  125. $record->setValues($values);
  126. $found = $record->find();
  127. $this->assertNotNull($found);
  128. $this->assertEquals(1, $found);
  129. }
  130. /**
  131. * Check that the find method throws an exception when it cant find anything.
  132. *
  133. * @throws \Exception
  134. */
  135. public function testFindFail() {
  136. $table = 'organism';
  137. $record = new \ChadoRecord($table);
  138. $record->setValue($table . '_id', 'unfindable');
  139. $this->expectException(Exception);
  140. $found = $record->find();
  141. }
  142. /**
  143. * @param $table
  144. * @param $values
  145. *
  146. * @throws \Exception
  147. */
  148. public function testSetandGetValue($table, $values) {
  149. $record = new \ChadoRecord($table);
  150. $record->setValues($values);
  151. $vals = $record->getValues();
  152. foreach ($vals as $val_key => $val) {
  153. $this->assertEquals($values[$val_key], $val, "The getValues did not match what was provided for setValues");
  154. }
  155. }
  156. }