Skip to content

Commit

Permalink
Merge pull request #181 from cyklokoalicia/php-mock
Browse files Browse the repository at this point in the history
using php-mock/php-mock-phpunit for code coverage of default php func
  • Loading branch information
sveneld authored Mar 13, 2024
2 parents 2528274 + f9d8169 commit c45d67a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 60 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
78 changes: 56 additions & 22 deletions tests/Authentication/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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(
Expand All @@ -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);
}
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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;
}
}
64 changes: 27 additions & 37 deletions tests/SmsConnector/EuroSmsConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -107,43 +110,30 @@ 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!@-_ +'));

$expectedUrl = 'http://as.eurosms.com/sms/Sender?action=send1SMSHTTP&i=Id&'
. 's=1122334455&d=1&sender=SenderNumber&number=number&msg=text%21%40-_+%2B';

$this->getFunctionMock('BikeShare\SmsConnector', 'fopen')
->expects($this->once())
->with(
$expectedUrl,
'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');
}
}

}

0 comments on commit c45d67a

Please sign in to comment.