PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 28, 2022

[FIXED] Laravel/codeception testing: how to stubbing time()

 January 28, 2022     codeception, laravel, mocking, php, stubbing     No comments   

Issue

Is it possible using Laravel mocking, stubling, (and/or Codeception\Test\Unit and/or Codeception\Stub) to make test for method z (public testZ(){...} ) for following class:

<?php

class X extends Y
{
    public function z(string $c = 'c') : array
    {
        return [$c, time()];
    }
}

So, I need time() to return fake value. Uopz & mimus are excluded options (coz it is possible that sooner or later it can be abandoned).

Is there any Laravel/php trick to make reliable test for codes that returns time() as part of returned value?


Solution

If you convert it to Carbon datetime, there is possibilities.

return [$c, now()->timestamp];

Now you can set your fake date in a test and it will return given date when used in the code

Carbon::setTestNow(Carbon::create(2021, 10, 19, 9, 30);


Answered By - mrhn
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing