RU
apps/cryo-studio / tests / LeaksTest.php · commit 76aa68d
<?phpnamespace Tests;/** Leaks: the delta of the 5 snapshots by allocation site, class or stack, with the line that keeps the objects. */final class LeaksTest extends CryoTestCase{    public function testTheLeakShowsTheLineThatCreatesAndTheLineThatKeeps(): void    {        $form = $this->form();        self::assertStringContainsString('hooked by line 18', $this->items($form, 'rows')[0]);        self::assertMatchesRegularExpression('/^> 17 +\$query = new Query\(\$sql\);/m', $this->text($form, 'source'));        self::assertMatchesRegularExpression('/^> 18 +self::\$log\[\] = \$query;/m', $this->text($form, 'hookSource'));        self::assertStringContainsString('Hooked by line 18: SqlLogger::log()', $this->text($form, 'detail'));        self::assertStringContainsString('Typical path (50 of the survivors): SqlLogger::$log (static_prop)  →  [399] Query', $this->text($form, 'pathInfo'));    }    public function testTheBoundedCacheIsNotALeakAndIsKeptByAnotherLine(): void    {        $form = $this->form();        $this->clickRow($form, 'rows', $this->rowWith($form, 'rows', 'PriceCache::get'));        self::assertStringNotContainsString('Leak', $this->text($form, 'detail'));        self::assertStringContainsString('kept 60 (25%)', $this->items($form, 'rows')[3]);        self::assertStringContainsString('hooked by shop_worker.php:73 handleBatch', $this->items($form, 'rows')[3]);        self::assertMatchesRegularExpression('/^> 31 +\$price = new Price/m', $this->text($form, 'source'));        self::assertMatchesRegularExpression('/^> 73 +\$order->lines\[\] = \$prices->get/m', $this->text($form, 'hookSource'));        $this->game->shot('leaks-cache');    }    public function testGroupingByClassPutsTheQueriesFirst(): void    {        $form = $this->form();        $this->clickOn($form, 'byClass');        self::assertSame('class', $form->by());        self::assertStringStartsWith('[LEAK] Query', $this->items($form, 'rows')[0]);        self::assertStringContainsString('Connection', $this->items($form, 'rows')[1]);        self::assertStringContainsString('new Query($sql)', $this->text($form, 'source'), 'the site where most Query objects were made');    }    public function testGroupingByStackShowsTheCallers(): void    {        $form = $this->form();        $this->clickOn($form, 'byStack');        self::assertSame('stack', $form->by());        self::assertStringContainsString('shop_worker.php:17 SqlLogger::log ← shop_worker.php:74 handleBatch ← shop_worker.php:95 {main}', $this->items($form, 'rows')[0]);        self::assertMatchesRegularExpression('/^> 17 /m', $this->text($form, 'source'));        $this->game->shot('leaks-stack');    }}