Skip to content

Commit

Permalink
Add tests for uncovered lines in DbDependency (#20052)
Browse files Browse the repository at this point in the history
  • Loading branch information
salehhashemi1992 authored Oct 27, 2023
1 parent cb8307f commit bd452da
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions tests/framework/caching/DbDependencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class DbDependencyTest extends DatabaseTestCase
*/
protected $driverName = 'sqlite';


/**
* {@inheritdoc}
*/
Expand All @@ -39,11 +38,14 @@ protected function setUp()
$db->createCommand()->insert('dependency_item', ['value' => 'initial'])->execute();
}

public function testIsChanged()
public function testQueryOneIsExecutedWhenQueryCacheEnabled()
{
$db = $this->getConnection(false);
$cache = new ArrayCache();

// Enable the query cache
$db->enableQueryCache = true;

$dependency = new DbDependency();
$dependency->db = $db;
$dependency->sql = 'SELECT [[id]] FROM {{dependency_item}} ORDER BY [[id]] DESC LIMIT 1';
Expand All @@ -56,4 +58,39 @@ public function testIsChanged()

$this->assertTrue($dependency->isChanged($cache));
}

public function testQueryOneIsExecutedWhenQueryCacheDisabled()
{
$db = $this->getConnection(false);
$cache = new ArrayCache();

// Disable the query cache
$db->enableQueryCache = false;

$dependency = new DbDependency();
$dependency->db = $db;
$dependency->sql = 'SELECT [[id]] FROM {{dependency_item}} ORDER BY [[id]] DESC LIMIT 1';
$dependency->reusable = false;

$dependency->evaluateDependency($cache);
$this->assertFalse($dependency->isChanged($cache));

$db->createCommand()->insert('dependency_item', ['value' => 'new'])->execute();

$this->assertTrue($dependency->isChanged($cache));
}

public function testMissingSqlThrowsException()
{
$this->expectException('\yii\base\InvalidConfigException');

$db = $this->getConnection(false);
$cache = new ArrayCache();

$dependency = new DbDependency();
$dependency->db = $db;
$dependency->sql = null;

$dependency->evaluateDependency($cache);
}
}

0 comments on commit bd452da

Please sign in to comment.