Silverstripe 3.7.6, PHP 7.3, PHPUnit 4.8
We have a bunch of existing unit tests that use SapphireTest->setUpOnce() and SapphireTest->tearDownOnce(). The latter is used to destroy test databases once complete. Which is how we found that these methods were not firing.
Simple test that simulates
<?php
class MyTest extends SapphireTest {
protected $usesDatabase = true;
public function setUp()
{
echo "setup \n";
parent::setUp();
}
public function tearDown()
{
echo "teardown \n";
parent::tearDown();
}
public function setUpOnce()
{
echo "setup once \n";
parent::setUpOnce();
}
public function tearDownOnce()
{
echo "teardown once \n";
parent::tearDownOnce();
}
function testSomething()
{
// My test
}
}
Output when test is run:
PHPUnit 4.8.36 by Sebastian Bergmann and contributors.
.setup teardown
Time: 6.38 seconds, Memory: 80.50MB
OK (1 test, 0 assertions)
Is there something that needs to be done in the test so these functions are fired at the start and end of the whole test class per docblock?
UPDATE:
See convo below. It was suggested that I use PHPUnit's setUpBeforeClass and tearDownAfterClass methods instead. These fired as expected. However they are static methods so I had no access to $this to use supporting SapphireTest or custom methods in the class. So I had to use these PHPUnit methods to instantiate an instance of the test class they sit in, then call my existing setUpOnce and tearDownOnce methods. I used late static binding's static() keyword here as I might move these calls into a parent class for my tests all to use. A bit hacky, but it seems to work.
Assuming my test code above, below is the addition to make:
public static function setUpBeforeClass() {
$object = new static();
$object->setUpOnce();
}
public static function tearDownAfterClass() {
$object = new static();
$object->tearDownOnce();
}