silexphp / Silex

[DEPRECATED -- Use Symfony instead] The PHP micro-framework based on the Symfony Components
https://silex.symfony.com
MIT License
3.58k stars 718 forks source link

Mock Application class #1347

Closed williamespindola closed 8 years ago

williamespindola commented 8 years ago

I'm trying to mock Silex\Application class but the PHPUnit returns the following error: PHPUnit_Framework_MockObject_BadMethodCallException until 5.2 version, and Warnings from 5.3. This occur because of static methods on Pimple at Lines L116 and L142. I saw that exists new versions of Pimple and has not static methods, but Silex still using 1.0

ragboyjr commented 8 years ago

Silex min dependency is 5.3.3 - https://github.com/silexphp/Silex/blob/1.0/composer.json#L18

So, any errors on 5.2 won't be fixed.

williamespindola commented 8 years ago

Hi @ragboyjr the versions is about PHPUnit not PHP ;)

ragboyjr commented 8 years ago

Ha! No problem man!

ragboyjr commented 8 years ago

Can you paste a code example?

williamespindola commented 8 years ago

Yep!

use Silex\Application;

class MyClassTest extends \PHPUnit_Framework_TestCase
{
    public function testMyMethod()
    {
        $appMock = $this->getMock('Silex\Application');
    }
}

PHPUnit 5.3

PHPUnit 5.3.2 by Sebastian Bergmann and contributors.

Runtime:       PHP 5.6.19
Configuration: .../phpunit.xml

W                                                                  1 / 1 (100%)

Time: 49 ms, Memory: 4.25Mb

There was 1 warning:

1) ...\Test\Provider\MyClassTest::testMyMethod

WARNINGS!
Tests: 1, Assertions: 0, Warnings: 1.

PHPUnit 5.2

PHPUnit 5.2.12 by Sebastian Bergmann and contributors.

Runtime:       PHP 5.6.19
Configuration: .../phpunit.xml

E                                                                   1 / 1 (100%)

Time: 50 ms, Memory: 4.25Mb

There was 1 error:

1) ...\Test\Provider\MyClassTest::testMyMethod
PHPUnit_Framework_MockObject_BadMethodCallException:

.../vendor/silex/silex/src/Silex/Application.php:72
.../tests/Provider/RouteServiceProviderTest.php:12

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.
ragboyjr commented 8 years ago

https://phpunit.de/manual/current/en/test-doubles.html. Read the big blue section about limitations.

I just did some checking and testing of my own. I don't think you'll be able to because of lines like this: https://github.com/silexphp/Silex/blob/1.0/src/Silex/Application.php#L73. It calls static methods in silex 1.0 and phpunit can't mock static methods.

You either can just instantiate the Application, or maybe use a different mocking framework like Phake or Phony

williamespindola commented 8 years ago

For now I will try with Phony. Exists some intention to improve silex to work with Pimple 3.0? May be with this upgrade this problem can be solved.

jdreesen commented 8 years ago

There's no way to make Silex v1 compatible with Pimple v2+. But Silex v2 will use Pimple v3.