TestCase.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. use PHPUnit\DbUnit\TestCase;
  3. /**
  4. * TestCase for testing a database
  5. *
  6. * @package Kohana/UnitTest
  7. * @author Kohana Team
  8. * @author BRMatt <matthew@sigswitch.com>
  9. * @copyright (c) Kohana Team
  10. * @license https://koseven.ga/LICENSE.md
  11. */
  12. abstract class Kohana_Unittest_Database_TestCase extends TestCase{
  13. /**
  14. * Make sure PHPUnit backs up globals
  15. * @var boolean
  16. */
  17. protected $backupGlobals = FALSE;
  18. /**
  19. * A set of unittest helpers that are shared between normal / database
  20. * testcases
  21. * @var Kohana_Unittest_Helpers
  22. */
  23. protected $_helpers = NULL;
  24. /**
  25. * A default set of environment to be applied before each test
  26. * @var array
  27. */
  28. protected $environmentDefault = [];
  29. /**
  30. * The kohana database connection that PHPUnit should use for this test
  31. * @var string
  32. */
  33. protected $_database_connection = 'default';
  34. /**
  35. * Creates a predefined environment using the default environment
  36. *
  37. * Extending classes that have their own setUp() should call
  38. * parent::setUp()
  39. */
  40. public function setUp()
  41. {
  42. $this->_helpers = new Kohana_Unittest_Helpers;
  43. $this->setEnvironment($this->environmentDefault);
  44. return parent::setUp();
  45. }
  46. /**
  47. * Restores the original environment overriden with setEnvironment()
  48. *
  49. * Extending classes that have their own tearDown()
  50. * should call parent::tearDown()
  51. */
  52. public function tearDown()
  53. {
  54. $this->_helpers->restore_environment();
  55. return parent::tearDown();
  56. }
  57. /**
  58. * Creates a connection to the unittesting database
  59. *
  60. * @return PDO
  61. */
  62. public function getConnection()
  63. {
  64. // Get the unittesting db connection
  65. $config = Kohana::$config->load('database.'.$this->_database_connection);
  66. if(strtolower($config['type']) !== 'pdo')
  67. {
  68. $config['connection']['dsn'] = strtolower($config['type']).':'.
  69. 'host='.$config['connection']['hostname'].';'.
  70. 'dbname='.$config['connection']['database'];
  71. }
  72. $pdo = new PDO(
  73. $config['connection']['dsn'],
  74. $config['connection']['username'],
  75. $config['connection']['password']
  76. );
  77. return $this->createDefaultDBConnection($pdo, $config['connection']['database']);
  78. }
  79. /**
  80. * Gets a connection to the unittest database
  81. *
  82. * @return Kohana_Database The database connection
  83. */
  84. public function getKohanaConnection()
  85. {
  86. return Database::instance(Kohana::$config->load('unittest')->db_connection);
  87. }
  88. /**
  89. * Removes all kohana related cache files in the cache directory
  90. */
  91. public function cleanCacheDir()
  92. {
  93. return Kohana_Unittest_Helpers::clean_cache_dir();
  94. }
  95. /**
  96. * Helper function that replaces all occurences of '/' with
  97. * the OS-specific directory separator
  98. *
  99. * @param string $path The path to act on
  100. * @return string
  101. */
  102. public function dirSeparator($path)
  103. {
  104. return Kohana_Unittest_Helpers::dir_separator($path);
  105. }
  106. /**
  107. * Allows easy setting & backing up of enviroment config
  108. *
  109. * Option types are checked in the following order:
  110. *
  111. * * Server Var
  112. * * Static Variable
  113. * * Config option
  114. *
  115. * @param array $environment List of environment to set
  116. */
  117. public function setEnvironment(array $environment)
  118. {
  119. return $this->_helpers->set_environment($environment);
  120. }
  121. /**
  122. * Check for internet connectivity
  123. *
  124. * @return boolean Whether an internet connection is available
  125. */
  126. public function hasInternet()
  127. {
  128. return Kohana_Unittest_Helpers::has_internet();
  129. }
  130. }