diff --git a/src/Mocks/TripalTestCaseMock.php b/src/Mocks/TripalTestCaseMock.php new file mode 100644 index 0000000..2e134de --- /dev/null +++ b/src/Mocks/TripalTestCaseMock.php @@ -0,0 +1,8 @@ +root = $root; $this->vendor_root = __DIR__ . '/../'; - $position = strrpos($root,'/') + 1; - $module_name = substr($root,$position); + $position = strrpos($root, '/') + 1; + $module_name = substr($root, $position); $this->module_name = $module_name; print("\nmodule: ". $module_name . " \n"); $this->setUpTests(); $this->addTravis(); - + $this->append_to_gitignore(); } @@ -79,16 +79,36 @@ protected function addTravis() copy($vendor_root . "stubs/travis.yml", $root . "/.travis.yml"); //Replace the MODULE_NAME variable with the module name $this->replace_string_in_file($root . "/.travis.yml", "MODULE_NAME", $this->module_name); + } + } + /** + * + * Check if theres a gitignore in the project root. if no, create and append vendor folder. + * If yes, check if vendor is in there and append it if it isnt. + */ + protected function append_to_gitignore() + { + $root = $this->root; + $filepath = $root . "/.gitignore"; + + if (!file_exists($filepath)) { + $fh = fopen($filepath, 'w'); + fwrite($fh, "\nvendor/\n"); + } else { + if (strpos(file_get_contents($filepath), "vendor/") == false) { + $fh = fopen($filepath, 'a'); + fwrite($fh, "\nvendor/\n"); + } } } - protected function replace_string_in_file($filename, $string_to_replace, $replace_with){ + protected function replace_string_in_file($filename, $string_to_replace, $replace_with) + { $content=file_get_contents($filename); $content_chunks=explode($string_to_replace, $content); $content=implode($replace_with, $content_chunks); file_put_contents($filename, $content); } - } diff --git a/src/TripalTestCase.php b/src/TripalTestCase.php index 651b928..a70bbee 100644 --- a/src/TripalTestCase.php +++ b/src/TripalTestCase.php @@ -5,7 +5,7 @@ use PHPUnit\Framework\TestCase; use StatonLab\TripalTestSuite\Exceptions\TripalTestSuiteException; -class TripalTestCase extends TestCase +abstract class TripalTestCase extends TestCase { /** * @var bool diff --git a/stubs/TripalExampleTest.php b/stubs/TripalExampleTest.php index d2548b5..66ff979 100644 --- a/stubs/TripalExampleTest.php +++ b/stubs/TripalExampleTest.php @@ -4,13 +4,9 @@ use StatonLab\TripalTestSuite\TripalTestCase; -class MyTest extends TripalTestCase { - - -public function exampleTest() { - -$this->assertTrue(true); - -} +class TripalTestCase extends TripalTestCase { +public function testExampleTest() { + $this->assertTrue(true); + } } diff --git a/stubs/bootstrap.php b/stubs/bootstrap.php index 7e3cee6..51bede3 100644 --- a/stubs/bootstrap.php +++ b/stubs/bootstrap.php @@ -1,11 +1,2 @@ + colors="true"> ./tests/ + ./tests/bootstrap.php diff --git a/stubs/travis.yml b/stubs/travis.yml index 386d6a8..b2ad985 100644 --- a/stubs/travis.yml +++ b/stubs/travis.yml @@ -12,7 +12,7 @@ before_script: - docker pull statonlab/tripal3 script: - - docker run -it -d --rm --name tripal -v "$(pwd)":/var/www/html/sites/all/modules/custom/MODULE_NAME statonlab/tripal3 + - docker run -it -d --rm --name tripal -v "$(pwd)":/modules/MODULE_NAME statonlab/tripal3 - sleep 30 #We pause here so drush won't fail - docker exec -it tripal drush pm-enable -y MODULE_NAME - - docker exec -it tripal bash -c 'cd /var/www/html/sites/all/modules/custom/MODULE_NAME/ &&composer install && DRUPAL_ROOT="/var/www/html" ./vendor/bin/phpunit' + - docker exec -it tripal bash -c 'cd /modules/MODULE_NAME/ && composer install && DRUPAL_ROOT="/var/www/html" ./vendor/bin/phpunit' diff --git a/tests/SetUpTest.php b/tests/SetUpTest.php index 368fea0..b8cef18 100644 --- a/tests/SetUpTest.php +++ b/tests/SetUpTest.php @@ -1,40 +1,34 @@ set_up = new StatonLab\TripalTestSuite\SetUp(); } - protected function tearDown() - { - $this->set_up = null; - } /** - *Assert that the Test Set Up constructs - * - **/ + * Assert that the Test Set Up constructs + */ public function testAdd() { - $set_up = $this->set_up; - $this->assertInstanceOf("StatonLab\TripalTestSuite\SetUp", $set_up); + $this->assertInstanceOf("StatonLab\TripalTestSuite\SetUp", $this->set_up); } /** - * Tests that the setup method creates all expected folders and files exist. - * We also include a $cleanup variable to signal if the created file should be deleted after (the example env file) - * Or if it shoul be left (The tests folder, where this test lives!) - */ - + * Tests that the setup method creates all expected folders and files exist. + * We also include a $cleanup variable to signal if the created file should be deleted after (the example env file) + * Or if it should be left (The tests folder, where this test lives!) + */ public function testCreateTestDirectory() { $dir = __DIR__; @@ -43,11 +37,11 @@ public function testCreateTestDirectory() $set_up->run(); $tests = [ -[$dir . "/TripalExampleTest.php", true, "The example test was not created"], - [$dir . "/example.env", true, "The example environment file was not created."], - [$dir . "/../.travis.yml", false ,"The travis.yml file was not created"], -[$dir . "/bootstrap.php", false, "The bootstrap file was not copied to test."] - ]; + [$dir."/TripalExampleTest.php", true, "The example test was not created"], + [$dir."/example.env", true, "The example environment file was not created."], + [$dir."/../.travis.yml", false, "The travis.yml file was not created"], + [$dir."/bootstrap.php", false, "The bootstrap file was not copied to test."], + ]; foreach ($tests as $file_to_check) { $file = $file_to_check[0]; diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 7e3cee6..51bede3 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,11 +1,2 @@ getMethod('_getDrupalRoot'); $drupal_root = $method->invoke($test_case); @@ -26,8 +26,8 @@ public function should_find_drupal_root() { * @throws \ReflectionException * @test */ - public function should_find_env_file_successfully() { - $test_case = new TripalTestCase(); + public function shouldFindEnvFileSuccessfully() { + $test_case = new TripalTestCaseMock(); $method = $this->getMethod('_getEnvironmentFilePath'); $env_file_path = $method->invoke($test_case); @@ -43,8 +43,8 @@ public function should_find_env_file_successfully() { * @throws \ReflectionException * @test */ - public function should_successfully_bootstrap_drupal() { - $test_case = new TripalTestCase(); + public function shouldSuccessfullyBootstrapDrupal() { + $test_case = new TripalTestCaseMock(); $method = $this->getMethod('_bootstrapDrupal'); $method->invoke($test_case); @@ -69,7 +69,7 @@ public function should_successfully_bootstrap_drupal() { */ protected function getMethod($method_name, $class_name = '') { if(empty($class_name)) { - $class_name = TripalTestCase::class; + $class_name = TripalTestCaseMock::class; } $reflection = new \ReflectionClass($class_name); diff --git a/tests/test_module/tests/Feature/DBTransactionTest.php b/tests/test_module/tests/Feature/DBTransactionTest.php index b050eaf..a3070eb 100644 --- a/tests/test_module/tests/Feature/DBTransactionTest.php +++ b/tests/test_module/tests/Feature/DBTransactionTest.php @@ -4,7 +4,7 @@ use PHPUnit\Framework\TestCase; use StatonLab\TripalTestSuite\DBTransaction; -use StatonLab\TripalTestSuite\TripalTestCase; +use StatonLab\TripalTestSuite\Mocks\TripalTestCaseMock; class DBTransactionTest extends TestCase { @@ -17,13 +17,13 @@ class DBTransactionTest extends TestCase */ protected function setUp() { - $test_case = new TripalTestCase(); + $test_case = new TripalTestCaseMock(); $method = $this->getMethod('_bootstrapDrupal'); $method->invoke($test_case); } /** @test */ - public function should_start_a_transaction() + public function shouldStartATransaction() { $this->DBTransactionSetUp(); $this->assertInstanceOf(\DatabaseTransaction::class, $this->_transaction); @@ -31,7 +31,7 @@ public function should_start_a_transaction() } /** @test */ - public function should_fail_to_find_record_after_transaction_has_ended() + public function shouldFailToFindRecordAfterTransactionHasEnded() { $this->DBTransactionSetUp(); @@ -68,7 +68,7 @@ public function should_fail_to_find_record_after_transaction_has_ended() protected function getMethod($method_name, $class_name = '') { if (empty($class_name)) { - $class_name = TripalTestCase::class; + $class_name = TripalTestCaseMock::class; } $reflection = new \ReflectionClass($class_name); diff --git a/tests/test_module/tests/bootstrap.php b/tests/test_module/tests/bootstrap.php index 7e3cee6..9ef34c9 100644 --- a/tests/test_module/tests/bootstrap.php +++ b/tests/test_module/tests/bootstrap.php @@ -1,6 +1,7 @@