From 8c1a0eba281e485389b548cc67ac81a460335b78 Mon Sep 17 00:00:00 2001 From: Thomas ZILLIOX Date: Fri, 27 Mar 2026 00:51:22 +0100 Subject: [PATCH] Fix PHPUnit deprecation warnings in integration tests --- tests/Integration/NotifierTest.php | 95 ++++++++++++----------------- tests/Integration/ProcessorTest.php | 67 ++++++++++++-------- 2 files changed, 80 insertions(+), 82 deletions(-) diff --git a/tests/Integration/NotifierTest.php b/tests/Integration/NotifierTest.php index 38fbdcc2..b4aa9249 100644 --- a/tests/Integration/NotifierTest.php +++ b/tests/Integration/NotifierTest.php @@ -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() diff --git a/tests/Integration/ProcessorTest.php b/tests/Integration/ProcessorTest.php index 11b34da4..d7ca088b 100644 --- a/tests/Integration/ProcessorTest.php +++ b/tests/Integration/ProcessorTest.php @@ -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( @@ -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()