Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Call to undefined method DateFormatterTest::getMock()

Update: with $stub = $this->createMock('Config'); this example works, but I get a warning:

OK, but incomplete, skipped, or risky tests! Tests: 1, Assertions: 0, Risky: 1.

In the video-tutorial this example works without any warnings. Is it possible to fix this warning?


I can't find why I am getting this error and how to fix it. This code is from a video tutorial. And in the Video it works. Maybe a typo?

Error:

c:\laragon\www\phpunit λ phpunit --colors tests\DateFormatterTest.php PHPUnit 6.0.0 by Sebastian Bergmann and contributors.

E 1 / 1 (100%)

Time: 35 ms, Memory: 4.00MB

There was 1 error:

1) DateFormatterTest::testFormattingDatesBasedOnConfig Error: Call to undefined method DateFormatterTest::getMock()

C:\laragon\www\phpunit\tests\DateFormatterTest.php:10

ERRORS! Tests: 1, Assertions: 0, Errors: 1.

Here my code:

Config.php

<?php

class Config {
    public function get() {
        return 'd-m-Y';
    }
}

DateFormatter.php

class DateFormatter { protected $config;

public function __construct (Config $config) {
    $this->config = $config;
}

public function getFormattedDate($timestamp) {
    return date($this->config->get('date.format'), $timestamp);
}

}

DateFormatterTest.php

<?php

use PHPUnit\Framework\TestCase;

require_once 'C:\laragon\www\phpunit\src\DateFormatter.php';
require_once 'C:\laragon\www\phpunit\src\Config.php';

class DateFormatterTest extends TestCase {
    public function testFormattingDatesBasedOnConfig() {
        $stub = $this->getMock('Config');

        var_dump($stub);
    }
}
like image 671
Meteor Newbie Avatar asked Mar 13 '17 11:03

Meteor Newbie


1 Answers

getMock() no longer exists in PHPUnit 6. Use createMock() or getMockBuilder() instead.

like image 180
Sebastian Bergmann Avatar answered Nov 14 '22 15:11

Sebastian Bergmann