Reputation: 51219
I wrote unit tests for my controller. My class is
class ApiControllerTest extends TestCase
and it contains test methods like this
public function testAgeDistribution()
{
$response = $this->action(...,
['datasetName'=>'AgeDistribution',
'min_longitude'=>-80.60, 'max_longitude'=>-78.60,
'min_latitude'=>43.20, 'max_latitude'=>44,
'zoom'=>12
]);
$this->assertResponseOk();
$json = json_decode($response->content());
$this->checkMainThings($json, 'AgeDistribution', 'Population', 7, 100, 7);
}
All methods are similar but with different parameters and checks.
In the beginning of handler function I have a row
$start_memory = memory_get_usage();
And I see (in debugger) that each new test it has more and more memory used.
In other words, memory is not freed between tests.
How to free memory in PHP or what potential error I have in tested methods?
Upvotes: 2
Views: 239
Reputation: 6402
PHPUnit doesn't clean up after itself. One option is to extend TestCase and free up memory in the tearDown
:
abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
{
public function tearDown()
{
parent::tearDown();
$refl = new ReflectionObject($this);
foreach ($refl->getProperties() as $prop) {
if (!$prop->isStatic() && 0 !== strpos($prop->getDeclaringClass()->getName(), 'PHPUnit_')) {
$prop->setAccessible(true);
$prop->setValue($this, null);
}
}
}
}
Upvotes: 2