Reputation: 476
I have a Function Which is loaded by composer autoload
"autoload": {
"files": [
"app/Helpers/SmsHelper.php",
]
},
This File Has a smsHelper Function Which Can send SMS.
<?php
if (!function_exists('sendSmsHelper')) {
function sendSmsHelper($msisdn, $message)
{
..... Codes
}
}
One Of My class use this Helper and i want to write test for this class. could some one tell me how to mock this function? because i don't want to send SMS in testing mode
Upvotes: 1
Views: 944
Reputation: 476
As I dont see any answers I decided to Wrote about some of my researches result: I know these are not the right answers maybe but will work
First Solution (not good one): if you are using a function in your class and want to writing a test for that class ! for mocking this type of functions you can set your test class NameSpace equal to the class that you want to writing test for, then you can write a function exactly the same name of specific function out of the class in your .php file.
for example : you use date() in your class and want to mock it to return your String instead of date
<?php
//namespace Tests\Unit; change this name space to that class you want to write test for
namespace yourclass namespace;
use Mockery\Mock;
function date() {
return "2019/12/17 01:00:00";
}
class yourNameTest extends TestCase
{
...... you test codes
}
Second Solution for the functions you created ( like my smsHelper ) :
I added a SwitchCase in my smsHelper function which will check if App is on Testing mode or not , if its on testing mode I'm going to return true;
function smsHelper()
{
if (!App::runningUnitTests()) {
....... my codes
} else {
return true;
}
}
Upvotes: 1