From 7b512a3b2e96e198cc7564489654ea81d8097298 Mon Sep 17 00:00:00 2001 From: Sveneld Date: Wed, 13 Mar 2024 21:42:30 +0100 Subject: [PATCH] using php-mock/php-mock-phpunit for code coverage of default php functions --- composer.json | 3 +- tests/Authentication/AuthTest.php | 78 +++++++++++++++------ tests/SmsConnector/EuroSmsConnectorTest.php | 60 ++++++---------- 3 files changed, 81 insertions(+), 60 deletions(-) diff --git a/composer.json b/composer.json index 10459b6..4bf7cab 100644 --- a/composer.json +++ b/composer.json @@ -31,6 +31,7 @@ }, "require-dev": { "squizlabs/php_codesniffer": "^3.0", - "phpunit/phpunit": "^5.7" + "phpunit/phpunit": "^5.7", + "php-mock/php-mock-phpunit": "^1.1" } } diff --git a/tests/Authentication/AuthTest.php b/tests/Authentication/AuthTest.php index 65e1450..9599134 100644 --- a/tests/Authentication/AuthTest.php +++ b/tests/Authentication/AuthTest.php @@ -4,10 +4,13 @@ use BikeShare\Authentication\Auth; use BikeShare\Db\DbInterface; +use phpmock\phpunit\PHPMock; use PHPUnit\Framework\TestCase; class AuthTest extends TestCase { + use PHPMock; + /** * @phpcs:disable PSR12.Properties.ConstantVisibility */ @@ -128,6 +131,11 @@ public function testLogin() $number = 'number'; $password = 'password'; $userId = '123'; + + $this->getFunctionMock('BikeShare\Authentication', 'time') + ->expects($this->exactly(2)) + ->willReturn(9999); + $this->db->expects($this->exactly(2)) ->method('escape') ->withConsecutive( @@ -150,6 +158,21 @@ public function testLogin() null ); + $this->getFunctionMock('BikeShare\Authentication', 'setcookie') + ->expects($this->exactly(2)) + ->withConsecutive( + ['loguserid', $userId, 1219599], + ['logsession', $sessionId, 1219599] + ) + ->willReturn(true); + + $this->getFunctionMock('BikeShare\Authentication', 'header') + ->expects($this->exactly(3)) + ->withConsecutive( + ['HTTP/1.1 302 Found'], + ['Location: /'], + ['Connection: close'] + ); $this->auth->login($number, $password); } @@ -170,12 +193,19 @@ public function testisLoggedIn( if ($sessionId) { $_COOKIE["logsession"] = $sessionId; } - $this->db->expects($this->exactly(count($escapeCallParams))) + + $this->getFunctionMock('BikeShare\Authentication', 'time') + ->expects(count($escapeCallParams) > 0 ? $this->once() : $this->never()) + ->willReturn(9999); + + $this->db + ->expects($this->exactly(count($escapeCallParams))) ->method('escape') ->withConsecutive(...$escapeCallParams) ->willReturnOnConsecutiveCalls(...$escapeCallResults); - $this->db->expects(count($escapeCallParams) > 0 ? $this->exactly(1) : $this->never()) + $this->db + ->expects(count($escapeCallParams) > 0 ? $this->once() : $this->never()) ->method('query') ->withConsecutive( ["SELECT sessionId FROM sessions WHERE @@ -240,6 +270,26 @@ public function testLogout() $sessionId ); + $this->getFunctionMock('BikeShare\Authentication', 'time') + ->expects($this->exactly(3)) + ->willReturn(9999); + + $this->getFunctionMock('BikeShare\Authentication', 'setcookie') + ->expects($this->exactly(2)) + ->withConsecutive( + ['loguserid', '0', 6399, '/'], + ['logsession', '', 6399, '/'] + ) + ->willReturn(true); + + $this->getFunctionMock('BikeShare\Authentication', 'header') + ->expects($this->exactly(3)) + ->withConsecutive( + ['HTTP/1.1 302 Found'], + ['Location: /'], + ['Connection: close'] + ); + $this->db->expects($this->exactly(2)) ->method('query') ->withConsecutive( @@ -276,6 +326,10 @@ public function testRefreshSession() $sessionId ); + $this->getFunctionMock('BikeShare\Authentication', 'time') + ->expects($this->exactly(4)) + ->willReturn(9999); + $this->db->expects($this->exactly(4)) ->method('query') ->withConsecutive( @@ -296,23 +350,3 @@ public function testRefreshSession() $this->auth->refreshSession(); } } - -/** - * @phpcs:disable PSR1.Files.SideEffects - */ -namespace BikeShare\Authentication; -{ -function header($header, $replace = true, $response_code = 0) -{ -} - -function setcookie($name, $value = '', $options = 0) -{ - return true; -} - -function time() -{ - return 9999; -} -} diff --git a/tests/SmsConnector/EuroSmsConnectorTest.php b/tests/SmsConnector/EuroSmsConnectorTest.php index b3a6918..f9af912 100644 --- a/tests/SmsConnector/EuroSmsConnectorTest.php +++ b/tests/SmsConnector/EuroSmsConnectorTest.php @@ -3,10 +3,13 @@ namespace Test\BikeShare\SmsConnector; use BikeShare\SmsConnector\EuroSmsConnector; +use phpmock\phpunit\PHPMock; use PHPUnit\Framework\TestCase; class EuroSmsConnectorTest extends TestCase { + use PHPMock; + /** * @var EuroSmsConnector */ @@ -107,43 +110,26 @@ public function testRespond() public function testSend() { - $this->smsConnector->send('number', 'text'); + $this->getFunctionMock('BikeShare\SmsConnector', 'md5') + ->expects($this->once()) + ->with('Key' . 'number') + ->willReturn('123456789011223344556677889900aa'); + $this->getFunctionMock('BikeShare\SmsConnector', 'substr') + ->expects($this->once()) + ->with('123456789011223344556677889900aa', 10, 11) + ->willReturn('1122334455'); + $this->getFunctionMock('BikeShare\SmsConnector', 'urlencode') + ->expects($this->once()) + ->with('text!@-_ +') + ->willReturn(urlencode('text!@-_ +')); + $this->getFunctionMock('BikeShare\SmsConnector', 'fopen') + ->expects($this->once()) + ->with( + 'http://as.eurosms.com/sms/Sender?action=send1SMSHTTP&i=Id&s=1122334455&d=1&sender=SenderNumber&number=number&msg=text%21%40-_+%2B', + 'r' + )->willReturn(true); + + $this->smsConnector->send('number', 'text!@-_ +'); $this->expectOutputString(''); } } - -/** - * @phpcs:disable PSR1.Files.SideEffects - */ -namespace BikeShare\SmsConnector; -{ - -/** - * no need to send real request to as.eurosms.com - * TODO should be refactored to use Guzzle or similar - */ -function fopen($filename, $mode, $use_include_path = null, $context = null) -{ - $gatewayId = 'Id'; - $gatewayKey = 'Key'; - $gatewaySenderNumber = 'SenderNumber'; - $message = 'text'; - $number = 'number'; - $s = substr(md5($gatewayKey . $number), 10, 11); - $um = urlencode($message); - - $url = sprintf( - 'http://as.eurosms.com/sms/Sender?action=send1SMSHTTP&i=%s&s=%s&d=1&sender=%s&number=%s&msg=%s', - $gatewayId, - $s, - $gatewaySenderNumber, - $number, - $um - ); - - if ($filename !== $url) { - throw new \RuntimeException('Invalid URL generated'); - } -} - -}