Skip to content
Open
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
95 changes: 39 additions & 56 deletions tests/Integration/NotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,67 +211,50 @@ public function test_sendNewAlerts()

$mock->setTriggeredAlerts($alerts);

$mock->expects($this->at(0))
->method('sendAlertsPerEmailToRecipient')
->with(
$this->equalTo($alerts),
$this->isInstanceOf('\Piwik\Mail'),
$this->equalTo('test5@example.com'),
$this->equalTo($period),
$this->equalTo($idSite)
);

$mock->expects($this->at(1))
->method('sendAlertsPerEmailToRecipient')
->with(
$this->equalTo(array($alerts[0], $alerts[2])),
$this->isInstanceOf('\Piwik\Mail'),
$this->equalTo('test1@example.com'),
$this->equalTo($period),
$this->equalTo($idSite)
);

$mock->expects($this->at(2))
->method('sendAlertsPerEmailToRecipient')
->with(
$this->equalTo(array($alerts[1])),
$this->isInstanceOf('\Piwik\Mail'),
$this->equalTo('test2@example.com'),
$this->equalTo($period),
$this->equalTo($idSite)
);

$mock->expects($this->at(3))
$expectedEmailCalls = [
[$alerts, 'test5@example.com'],
[[$alerts[0], $alerts[2]], 'test1@example.com'],
[[$alerts[1]], 'test2@example.com'],
[[$alerts[3]], 'test3@example.com'],
];
$mock->expects($this->exactly(count($expectedEmailCalls)))
->method('sendAlertsPerEmailToRecipient')
->with(
$this->equalTo(array($alerts[3])),
$this->isInstanceOf('\Piwik\Mail'),
$this->equalTo('test3@example.com'),
$this->equalTo($period),
$this->equalTo($idSite)
);

$mock->expects($this->at(4))
->willReturnCallback(function ($actualAlerts, $mail, $recipient, $actualPeriod, $actualIdSite) use (&$expectedEmailCalls, $period, $idSite) {
[$expectedAlerts, $expectedRecipient] = array_shift($expectedEmailCalls);

$this->assertSame($expectedAlerts, $actualAlerts);
$this->assertInstanceOf('\Piwik\Mail', $mail);
$this->assertSame($expectedRecipient, $recipient);
$this->assertSame($period, $actualPeriod);
$this->assertSame($idSite, $actualIdSite);
});

$expectedSmsCalls = [
[[$alerts[0], $alerts[1], $alerts[3]], '+1234567890'],
[$alerts, '232'],
];
$mock->expects($this->exactly(count($expectedSmsCalls)))
->method('sendAlertsPerSmsToRecipient')
->with(
$this->equalTo(array($alerts[0], $alerts[1], $alerts[3])),
$this->isInstanceOf('\Piwik\Plugins\MobileMessaging\Model'),
$this->equalTo('+1234567890')
);
->willReturnCallback(function ($actualAlerts, $mobileMessagingModel, $phoneNumber) use (&$expectedSmsCalls) {
[$expectedAlerts, $expectedPhoneNumber] = array_shift($expectedSmsCalls);

$mock->expects($this->at(5))
->method('sendAlertsPerSmsToRecipient')
->with(
$this->equalTo($alerts),
$this->isInstanceOf('\Piwik\Plugins\MobileMessaging\Model'),
$this->equalTo('232')
);

foreach ($alerts as $index => $alert) {
$mock->expects($this->at(6 + $index))->method('markAlertAsSent')->with($this->equalTo($alert));
}
$this->assertSame($expectedAlerts, $actualAlerts);
$this->assertInstanceOf('\Piwik\Plugins\MobileMessaging\Model', $mobileMessagingModel);
$this->assertEquals($expectedPhoneNumber, $phoneNumber);
});

$expectedMarkedAlerts = $alerts;
$mock->expects($this->exactly(count($expectedMarkedAlerts)))
->method('markAlertAsSent')
->willReturnCallback(function ($actualAlert) use (&$expectedMarkedAlerts) {
$this->assertSame(array_shift($expectedMarkedAlerts), $actualAlert);
});

$mock->sendNewAlerts($period, $idSite);

$this->assertSame([], $expectedEmailCalls);
$this->assertSame([], $expectedSmsCalls);
$this->assertSame([], $expectedMarkedAlerts);
}

public function provideContainerConfig()
Expand Down
67 changes: 41 additions & 26 deletions tests/Integration/ProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,45 +488,53 @@ public function test_processAlert_shouldTriggerAlertIfMatchAndRunOnlyForGivenWeb
->getMock();

$idSite = 1;
$processorMock->expects($this->at(0))
$expectedGetValueCalls = [
[$alert, $idSite, 1, 13],
[$alert, $idSite, 13, 10],
];
$processorMock->expects($this->exactly(count($expectedGetValueCalls)))
->method('getValueForAlertInPast')
->with($this->equalTo($alert), $this->equalTo($idSite), $this->equalTo(1))
->will($this->returnValue(13));
->willReturnCallback(function ($actualAlert, $actualIdSite, $actualSubPeriodN) use (&$expectedGetValueCalls) {
[$expectedAlert, $expectedIdSite, $expectedSubPeriodN, $returnValue] = array_shift($expectedGetValueCalls);

$processorMock->expects($this->at(1))
->method('getValueForAlertInPast')
->with($this->equalTo($alert), $this->equalTo($idSite), $this->equalTo(13))
->will($this->returnValue(10));
$this->assertSame($expectedAlert, $actualAlert);
$this->assertSame($expectedIdSite, $actualIdSite);
$this->assertSame($expectedSubPeriodN, $actualSubPeriodN);

$processorMock->expects($this->never())->method('triggerAlert');
return $returnValue;
});

$processorMock->expects($this->exactly(2))
->method('getValueForAlertInPast');
$processorMock->expects($this->never())->method('triggerAlert');

$processorMock->processAlert($alert, $idSite);
$this->assertSame([], $expectedGetValueCalls);

$idSite = 2;
$processorMock = $this->getMockBuilder('Piwik\Plugins\CustomAlerts\tests\Integration\CustomProcessor')
->setMethods($methods)
->getMock();
$processorMock->expects($this->at(0))
$expectedGetValueCalls = [
[$alert, $idSite, 1, 15],
[$alert, $idSite, 13, 10],
];
$processorMock->expects($this->exactly(count($expectedGetValueCalls)))
->method('getValueForAlertInPast')
->with($this->equalTo($alert), $this->equalTo($idSite), $this->equalTo(1))
->will($this->returnValue(15));
->willReturnCallback(function ($actualAlert, $actualIdSite, $actualSubPeriodN) use (&$expectedGetValueCalls) {
[$expectedAlert, $expectedIdSite, $expectedSubPeriodN, $returnValue] = array_shift($expectedGetValueCalls);

$processorMock->expects($this->at(1))
->method('getValueForAlertInPast')
->with($this->equalTo($alert), $this->equalTo($idSite), $this->equalTo(13))
->will($this->returnValue(10));
$this->assertSame($expectedAlert, $actualAlert);
$this->assertSame($expectedIdSite, $actualIdSite);
$this->assertSame($expectedSubPeriodN, $actualSubPeriodN);

$processorMock->expects($this->exactly(2))
->method('getValueForAlertInPast');
return $returnValue;
});

$processorMock->expects($this->once())
->method('triggerAlert')
->with($this->equalTo($alert), $this->equalTo($idSite), $this->equalTo(15), $this->equalTo(10));

$processorMock->processAlert($alert, $idSite);
$this->assertSame([], $expectedGetValueCalls);
}

private function buildAlert(
Expand Down Expand Up @@ -603,20 +611,27 @@ public function test_processAlert_shouldOnlyBeTriggeredIfAlertMatches()
$processorMock = $this->getMockBuilder('Piwik\Plugins\CustomAlerts\tests\Integration\CustomProcessor')
->setMethods($methods)
->getMock();
$processorMock->expects($this->at(0))
$expectedGetValueCalls = [
[$alert, 1, 1, 15],
[$alert, 1, 8, 10],
];
$processorMock->expects($this->exactly(count($expectedGetValueCalls)))
->method('getValueForAlertInPast')
->with($this->equalTo($alert), $this->equalTo(1), $this->equalTo(1))
->will($this->returnValue(15));
->willReturnCallback(function ($actualAlert, $actualIdSite, $actualSubPeriodN) use (&$expectedGetValueCalls) {
[$expectedAlert, $expectedIdSite, $expectedSubPeriodN, $returnValue] = array_shift($expectedGetValueCalls);

$processorMock->expects($this->at(1))
->method('getValueForAlertInPast')
->with($this->equalTo($alert), $this->equalTo(1), $this->equalTo(8))
->will($this->returnValue(10));
$this->assertSame($expectedAlert, $actualAlert);
$this->assertSame($expectedIdSite, $actualIdSite);
$this->assertSame($expectedSubPeriodN, $actualSubPeriodN);

return $returnValue;
});

$processorMock->expects($this->never())
->method('triggerAlert');

$processorMock->processAlert($alert, 1);
$this->assertSame([], $expectedGetValueCalls);
}

public function test_shouldBeTriggered_ShouldFail_IfInvalidConditionGiven()
Expand Down
Loading