Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset all the stored profiles. #79

Merged
merged 3 commits into from Jul 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Profiler.php
Expand Up @@ -109,4 +109,16 @@ public function getProfiles()
{
return $this->profiles;
}

/**
*
* Reset all the profiles
*
* @return null
*
*/
public function resetProfiles()
{
$this->profiles = array();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be we can make this as a fluent one ?

}
}
51 changes: 0 additions & 51 deletions tests/src/AbstractExtendedPdoTest.php
Expand Up @@ -450,57 +450,6 @@ public function testRollBack($rollBackResult)
$this->assertTrue($rollBackResult);
}

public function testProfiling()
{
$this->pdo->setProfiler(new Profiler);

// leave inactive
$this->pdo->query("SELECT 1 FROM pdotest");
$profiles = $this->pdo->getProfiler()->getProfiles();
$this->assertEquals(0, count($profiles));

// activate
$this->pdo->getProfiler()->setActive(true);

$this->pdo->query("SELECT 1 FROM pdotest");

$this->pdo->exec("SELECT 2 FROM pdotest");

$this->pdo->fetchAll("SELECT 3 FROM pdotest", array('zim' => 'gir'));

$profiles = $this->pdo->getProfiler()->getProfiles();
$this->assertEquals(3, count($profiles));

// get the profiles, remove stuff that's variable
$actual = $this->pdo->getProfiler()->getProfiles();
foreach ($actual as $key => $val) {
unset($actual[$key]['duration']);
unset($actual[$key]['trace']);
}

$expect = array(
0 => array(
'function' => 'query',
'statement' => 'SELECT 1 FROM pdotest',
'bind_values' => array(),
),
1 => array(
'function' => 'exec',
'statement' => 'SELECT 2 FROM pdotest',
'bind_values' => array(),
),
2 => array(
'function' => 'perform',
'statement' => 'SELECT 3 FROM pdotest',
'bind_values' => array(
'zim' => 'gir',
),
),
);

$this->assertSame($expect, $actual);
}

public function testPlaceholdersInPdo()
{
$stm = 'SELECT * FROM pdotest WHERE id > ? AND id < :max';
Expand Down
140 changes: 140 additions & 0 deletions tests/src/ProfilerTest.php
@@ -0,0 +1,140 @@
<?php
namespace Aura\Sql;

use Aura\Sql\AbstractExtendedPdoTest;

class ProfilerTest extends AbstractExtendedPdoTest
{
protected function newExtendedPdo()
{
return new ExtendedPdo('sqlite::memory:');
}

public function testGetPdo()
{
$lazy_pdo = $this->pdo->getPdo();
$this->assertInstanceOf('PDO', $lazy_pdo);
}

public function testProfiling()
{
$this->pdo->setProfiler(new Profiler);

// leave inactive
$this->pdo->query("SELECT 1 FROM pdotest");
$profiles = $this->pdo->getProfiler()->getProfiles();
$this->assertEquals(0, count($profiles));

// activate
$this->pdo->getProfiler()->setActive(true);

$this->pdo->query("SELECT 1 FROM pdotest");

$this->pdo->exec("SELECT 2 FROM pdotest");

$this->pdo->fetchAll("SELECT 3 FROM pdotest", array('zim' => 'gir'));

$profiles = $this->pdo->getProfiler()->getProfiles();
$this->assertEquals(3, count($profiles));

// get the profiles, remove stuff that's variable
$actual = $this->pdo->getProfiler()->getProfiles();
foreach ($actual as $key => $val) {
unset($actual[$key]['duration']);
unset($actual[$key]['trace']);
}

$expect = array(
0 => array(
'function' => 'query',
'statement' => 'SELECT 1 FROM pdotest',
'bind_values' => array(),
),
1 => array(
'function' => 'exec',
'statement' => 'SELECT 2 FROM pdotest',
'bind_values' => array(),
),
2 => array(
'function' => 'perform',
'statement' => 'SELECT 3 FROM pdotest',
'bind_values' => array(
'zim' => 'gir',
),
),
);

$this->assertSame($expect, $actual);
}

public function testResetProfiles()
{
$this->pdo->setProfiler(new Profiler);

// leave inactive
$this->pdo->query("SELECT 1 FROM pdotest");
$profiles = $this->pdo->getProfiler()->getProfiles();
$this->assertEquals(0, count($profiles));

// activate
$this->pdo->getProfiler()->setActive(true);

$this->pdo->query("SELECT 1 FROM pdotest");

$profiles = $this->pdo->getProfiler()->getProfiles();
$this->assertEquals(1, count($profiles));

// get the profiles, remove stuff that's variable
$actual = $this->pdo->getProfiler()->getProfiles();
foreach ($actual as $key => $val) {
unset($actual[$key]['duration']);
unset($actual[$key]['trace']);
}

$expect = array(
0 => array(
'function' => 'query',
'statement' => 'SELECT 1 FROM pdotest',
'bind_values' => array(),
)
);

$this->assertSame($expect, $actual);

$this->pdo->getProfiler()->resetProfiles();

$this->pdo->exec("SELECT 2 FROM pdotest");

$this->pdo->fetchAll("SELECT 3 FROM pdotest", array('zim' => 'gir'));

$profiles = $this->pdo->getProfiler()->getProfiles();
$this->assertEquals(2, count($profiles));

// get the profiles, remove stuff that's variable
$actual = $this->pdo->getProfiler()->getProfiles();
foreach ($actual as $key => $val) {
unset($actual[$key]['duration']);
unset($actual[$key]['trace']);
}

$expect = array(
0 => array(
'function' => 'exec',
'statement' => 'SELECT 2 FROM pdotest',
'bind_values' => array(),
),
1 => array(
'function' => 'perform',
'statement' => 'SELECT 3 FROM pdotest',
'bind_values' => array(
'zim' => 'gir',
),
),
);

$this->assertSame($expect, $actual);
$this->pdo->getProfiler()->resetProfiles();
$profiles = $this->pdo->getProfiler()->getProfiles();
$this->assertEquals(0, count($profiles));
}
}