Skip to content
This repository was archived by the owner on Nov 29, 2021. It is now read-only.

Reset Stores on Send #75

Merged
merged 2 commits into from
May 6, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class Agent
*
* @return void
*/
public function __construct(array $config, array $sharedContext = [], EventFactoryInterface $eventFactory = null)
public function __construct(array $config, array $sharedContext = [], EventFactoryInterface $eventFactory = null, TransactionsStore $transactionsStore = null, ErrorsStore $errorsStore = null)
{
// Init Agent Config
$this->config = new Config($config);
Expand All @@ -111,8 +111,8 @@ public function __construct(array $config, array $sharedContext = [], EventFacto
$this->sharedContext['cookies'] = $this->config->get('cookies', []);

// Initialize Event Stores
$this->transactionsStore = new TransactionsStore();
$this->errorsStore = new ErrorsStore();
$this->transactionsStore = $transactionsStore ?? new TransactionsStore();
$this->errorsStore = $errorsStore ?? new ErrorsStore();

// Start Global Agent Timer
$this->timer = new Timer();
Expand Down Expand Up @@ -221,6 +221,8 @@ public function send() : bool
{
// Is the Agent enabled ?
if ($this->config->get('active') === false) {
$this->errorsStore->reset();
$this->transactionsStore->reset();
return true;
}

Expand Down
24 changes: 24 additions & 0 deletions tests/AgentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
namespace PhilKra\Tests;

use \PhilKra\Agent;
use PhilKra\Stores\ErrorsStore;
use PhilKra\Stores\TransactionsStore;
use \PhilKra\Transaction\Summary;
use PHPUnit\Framework\MockObject\MockObject;
use Yaoi\Mock;

/**
* Test Case for @see \PhilKra\Agent
Expand Down Expand Up @@ -91,4 +95,24 @@ public function testForceErrorOnUnstartedTransaction() {
$agent->stopTransaction( 'unknown' );
}

public function testClearErrorStoreOnSendWhenNotActive()
{
/** @var TransactionsStore|MockObject $transactionStore */
$transactionStore = $this->createMock(TransactionsStore::class);
/** @var ErrorsStore|MockObject $errorStore */
$errorStore = $this->createMock(ErrorsStore::class);

$agent = new Agent(
[ 'appName' => 'phpunit_1', 'active' => false ],
[],
null,
$transactionStore,
$errorStore
);

$transactionStore->expects($this->once())->method('reset');
$errorStore->expects($this->once())->method('reset');

$agent->send();
}
}