rOpens++; $this->dbh = new \PDO('sqlite::memory:'); $this->dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $this->connected = true; return true; } }; } protected function tearDown(): void { DatabaseFactory::reset(); unset($GLOBALS['db']); } public function testNothingOpensUntilTheFirstQuery(): void { $rDb = $this->handler(); $this->assertFalse($rDb->isOpen()); $this->assertTrue($rDb->ping(), 'an unopened handle is usable, not dead'); $this->assertSame(0, $rDb->rOpens); $this->assertTrue($rDb->query('SELECT 1 AS `one`')); $this->assertSame(1, $rDb->rOpens); $this->assertInstanceOf(\PDO::class, $rDb->dbh); $rDb->query('SELECT ? AS `two`', 2); $this->assertSame(1, $rDb->rOpens, 'opened once'); $this->assertTrue($rDb->isOpen()); } public function testEscapeAndTransactionsOpenToo(): void { $rDb = $this->handler(); $this->assertSame("'x'", $rDb->escape('x')); $this->assertSame(1, $rDb->rOpens); $rTx = $this->handler(); $this->assertTrue($rTx->beginTransaction()); $this->assertSame(1, $rTx->rOpens); $rTx->rollback(); } public function testConnectLazyKeepsAnUnopenedHandle(): void { $GLOBALS['db'] = $this->handler(); DatabaseFactory::connectLazy(); $this->assertSame($GLOBALS['db'], DatabaseFactory::get()); $this->assertSame(0, $GLOBALS['db']->rOpens, 'reused, not replaced, not opened'); } public function testStreamingEndpointsUseTheLazyHandler(): void { $rRoot = dirname(__DIR__, 2) . '/src/'; foreach (['Public/stream/live.php', 'Public/stream/vod.php', 'Public/stream/timeshift.php', 'Streaming/Lifecycle/ShutdownHandler.php'] as $rFile) { $rSource = (string) file_get_contents($rRoot . $rFile); $this->assertStringContainsString('DatabaseFactory::connectLazy();', $rSource, $rFile); $this->assertStringNotContainsString('DatabaseFactory::connect();', $rSource, $rFile); } } }