PHPUnit #expectedException does not respect namesapce imports - phpunit

I found that PHPUnit's annotation #expectedException does not want to read class namespace paths from use statements (I'm using psr-0 for autoloading).
Take this as an example:
<?php
namespace Outrace\Battleship\Tests;
use Outrace\Battleship\Collection\MastCollection;
use Outrace\Battleship\Exception\CollectionOverflowException;
class MastCollectionTest extends \PHPUnit_Framework_TestCase
{
/**
* #expectedException CollectionOverflowException
*/
public function testAcceptOnlyMasts()
{
$notMastObject = new \stdClass();
$mastCollection = new MastCollection();
$mastCollection->attach($notMastObject);
}
}
The test, when run, will result in this error:
ReflectionException: Class CollectionOverflowException does not exist
To remedy the situation, I tried adding autoload-dev to my compose.json and dumping autoload file again:
"autoload-dev": {
"classmap": [
"src/Outrace/Battleship/Exception/"
]
},
or with psr-4:
"autoload-dev": {
"psr-4": {
"Outrace\\Battleship\\Tests\\": "src/Outrace/Battleship/Tests/",
"Outrace\\Battleship\\Exception\\": "src/Outrace/Battleship/Exception/"
}
},
None of the above would solve the problem, the error would persist.
However, the test would work well if the annotation references a fullu qualified name of the exception class:
/**
* #expectedException Outrace\Battleship\Exception\CollectionOverflowException
*/
public function testAcceptOnlyMasts()
Is this a limitation of PHPUnit or am I doing something wrong here?

This is a limitation with how phpunit works.
Internally it uses php's ReflectionClass which expects the FQCN of the exception. It just takes the string you give it in the annotation.
TestCase.php has the following when checking exceptions $reflector = new ReflectionClass($this->expectedException); and the expectedException property is populated either from the annotation or a call to setExpectedException().
You can use simplified names if you use the setExpectedException() method as you could then do something such as
<?php
namespace Outrace\Battleship\Tests;
use Outrace\Battleship\Collection\MastCollection;
use Outrace\Battleship\Exception\CollectionOverflowException;
class MastCollectionTest extends \PHPUnit_Framework_TestCase
{
public function testAcceptOnlyMasts()
{
$this->setExpectedException(CollectionOverflowException::class);
$notMastObject = new \stdClass();
$mastCollection = new MastCollection();
$mastCollection->attach($notMastObject);
}
}

Related

PhpUnit does not identify TypeError with string numbers and Abstract Class

With the following class
declare(strict_types=1);
abstract class IntValueObject
{
public function __construct(protected int $value)
{
}
}
and the test
declare(strict_types=1);
final class IntValueObjectTest extends TestCase
{
public function testWithNotValidValue(): void
{
$value = '1';
$this->expectException(\TypeError::class);
$this->getMockForAbstractClass(IntValueObject::class, [$value]);
}
}
return
Api\Tests\Shared\Domain\ValueObject\IntValueObjectTest::testWithNotValidValue
Failed asserting that exception of type "TypeError" is thrown.
If I change $value from '1' to 'foo' if it passes the test.
We use PHP 8, and in production, if the value '1' is passed it would give TypeError, why doesn't this happen in the test?
Thanks in advance.
ORIGIN OF THE "PROBLEM"
https://bugs.php.net/bug.php?id=81258
POSSIBLE SOLUTION
declare(strict_types=1);
final class IntValueObjectTest extends TestCase
{
public function testWithNotValidValue(): void
{
$value = '1';
$this->expectException(\TypeError::class);
new class($value) extends IntValueObject {};
}
}
One explanation I can imagine is that during the test, IntValueObject::__construct('1') is called from code that is not using declare(strict_types=1); and therefore the string '1' is being coerced to integer 1. No TypeError is thrown in that case (but it would for string 'foo' - as you describe the behaviour in your question).
The Cause
The cause is using a generated mock:
<?php declare (strict_types = 1);
...
$this->expectException(\TypeError::class);
$this->getMockForAbstractClass(IntValueObject::class, [$value]);
...
To not have a TypeError in this situation is likely unexpected to you as you have scalar strict types but still see the type-coercion of the string '1' to integer 1 for the constructors' first parameter.
The Mismatch
However the TypeError is only thrown when the code calling IntValueObject::__construct(int $value) has declare(strict_types=1).
While the test-code has declare(strict_types=1) it must not be that code where the constructor method is called - as no TypeError is thrown.
For Real
Behind the scenes $this->getMockForAbstractClass(...); uses an Instantiator from the Doctrine project which is making use of PHP reflection (meta-programming). As those methods are all internal code, declare(strict_types=1) is not effective and there is no TypeError anymore.
Compare with the following code-example:
<?php declare(strict_types=1);
class Foo {
public function __construct(int $integer) {
$this->integer = $integer;
}
}
try {
$foo = new Foo('1');
} catch (TypeError $e) {
} finally {
assert(isset($e), 'TypeError was thrown');
assert(!isset($foo), '$foo is unset');
}
$foo = (new ReflectionClass(Foo::class))->newInstance('1');
var_dump($foo);
When executed with assertions enabled, the output is the following:
object(Foo)#3 (1) {
["integer"]=>
int(1)
}
Within the try-block, the TypeError is thrown with new as you expect it.
But afterwards when instantiating with PHP reflection it is not.
(see as well https://3v4l.org/aZTJl)
The Remedy
Add a class to your test-suite that is really mocking the abstract base class and place it next to the test of it so you can easily use it:
<?hpp declare(strict_types=1);
class IntValueObjectMock extends IntValueObject
{...}
Then use IntValueObjectMock in your test:
$value = '1';
$this->expectException(\TypeError::class);
new IntValueObjectMock($value);
Alternatively use anonymous class when extending it is straight forward:
$value = '1';
$this->expectException(\TypeError::class);
new class($value) extends IntValueObject {};
Or apply type-checks on the constructor method your own, either with PHP reflection or run static code-analysis which has the benefit that it can detect such issues already without instantiating - so no additional test-code is involved.

PhpStorm PHPUnit support

class MainTest extends TestCase
{
public function testMain()
{
$stub = $this->createMock(Project\NotImplementedClass::class);
$stub->method('doSomething')
->will($this->returnCallback(function ($string) {
return strtoupper($string);
}));
$this->assertEquals('ABC', $stub->doSomething('abc'));
}
}
PhpStorm tells that method doSomething doesn't exists. I searched any plugin which can autocomplete methods. Is any plugin for this?
PHPStorm's autocomplete relies heavily on type hints. In your case - since $this->createMock() will return a PHPUnit_Framework_MockObject_MockObject which does not have the method it will complain.
What you can do is "overwrite" the type hint for the variable:
/** #var Project\NotImplementedClass|PHPUnit_Framework_MockObject_MockObject $stub */
$stub = $this->createMock(Project\NotImplementedClass::class);
or you could put the mock creation in a method with a similar #return docblock.
This will tell PHPStorm to look at both classes for autocomplete.
We use the Dynamic Return Type-plugin to improve the type hinting of PHPUnit. It's not perfect, but is easy to set up and use. The plugin let you define return types for methods based on the value of a parameter.
Add the file dynamicReturnTypeMeta.json to the root of your project with the following contents:
{
"methodCalls": [
{
"class": "\\PHPUnit_Framework_TestCase",
"method": "createMock",
"position": 0,
"mask": "%s|PHPUnit_Framework_MockObject_MockObject"
}
],
"functionCalls": []
}

how to use Symfony methods Action excluding the "Action" word

I am currently migrating an existent application to Symfony2 that has about 100 controllers with approximately 8 actions in each controller. All the current Actions are named as follow:
public function index(){}
However the default naming convention for Symfony is indexAction().
Is it possible to keep all my current actions and tell Symfony to use as it is without the "Action" word after the method name?
thank you.
Yes, this is possible. You should be able to define routes as normal, but you need to change the way the kernel finds the controller. The best way to do this is to replace/decorate/extends the service 'controller_name_converter'. This is a private service and is injected into the 'controller_resolver' service.
The source code of the class you want to replace is at 'Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser'.
Basically, the code runs like this. The 'bundle:controller:action' you specified when creating the route is saved in the cache. When a route is matched, that string is given back to the kernel, which in turn calls 'controller_resolver' which calls 'controller_name_resolver'. This class convert the string into a "namespace::method" notation.
Take a look at decorating services to get an idea of how to do it.
Here is an untested class you can work with
class ActionlessNameParser
{
protected $parser;
public function __construct(ControllerNameParser $parser)
{
$this->parser = $parser;
}
public function parse($controller)
{
if (3 === count($parts = explode(':', $controller))) {
list($bundle, $controller, $action) = $parts;
$controller = str_replace('/', '\\', $controller);
try {
// this throws an exception if there is no such bundle
$allBundles = $this->kernel->getBundle($bundle, false);
} catch (\InvalidArgumentException $e) {
return $this->parser->parse($controller);
}
foreach ($allBundles as $b) {
$try = $b->getNamespace().'\\Controller\\'.$controller.'Controller';
if (class_exists($try)) {
// You can also try testing if the action method exists.
return $try.'::'.$action;
}
}
}
return $this->parser->parse($controller);
}
public function build($controller)
{
return $this->parser->build($controller);
}
}
And replace the original service like:
actionless_name_parser:
public: false
class: My\Namespace\ActionlessNameParser
decorates: controller_name_converter
arguments: ["#actionless_name_parser.inner"]
Apparently the Action suffix is here to distinguish between internal methods and methods that are mapped to routes. (According to this question).
The best way to know for sure is to try.
// src/AppBundle/Controller/HelloController.php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class HelloController
{
/**
* #Route("/hello/{name}", name="hello")
*/
public function indexAction($name)
{
return new Response('<html><body>Hello '.$name.'!</body></html>');
}
}
Try to remove the Action from the method name and see what happens.

How to include Facebook-SDK

I'm using an example of FOSUserBundle with FOSFacebookBundle. Hereon i have build my application.
The relevant Project Structure is like following:
src\ABC\MainBundle\
src\ABC\UserBundle\
src\ABC\MainBundle\Controller\DefaultController.php
src\ABC\UserBundle\Security\User\Provider\FacebookProvider.php
vendor\facebook\php-sdk\src\base_facebook.php
Part of the FacebookProvider:
use \BaseFacebook;
use \FacebookApiException;
class FacebookProvider implements UserProviderInterface
{
protected $facebook;
public function __construct(BaseFacebook $facebook, $userManager, $validator)
{
$this->facebook = $facebook;
}
public function loadUserByUsername($username)
{
try {
$fbdata = $this->facebook->api('/me');
...
As you can see there is the Facebook-Object already available.
What i want to do now is nearly the same, but in my DefaultController:
use \BaseFacebook;
use \FacebookApiException;
class DefaultController extends BaseController
{
public function indexAction(){
$facebook = new Facebook('key', 'secret');
$fbfriends_obj = $facebook->api('/'.$fbid.'/friends');
...
But there i get the message
Fatal error: Class 'ABC\MainBundle\Controller\Facebook' not found in C:\xampp\htdocs\...\src\ABC\MainBundle\Controller\DefaultController.php on line x
Why is that? How can i access the facebook-class from inside my defaultcontroller? If its already possible for the facebookprovider, why it aint possible for my controller?
any hints will be really appreciated!
The solution to that problem is, that the facebook-class has no namespace and you have to do something like
$facebook = new \Facebook(...)
Problem is here:
use \BaseFacebook;
use \FacebookApiException;
You are importing BaseFacebook class from namespace you should use \Facebook (in Controller and FacebookProvider classes)

How to catch PHP Warning in PHPUnit

I am writing test cases and here is a question I have.
So say I am testing a simple function someClass::loadValue($value)
The normal test case is easy, but assume when passing in null or -1 the function call generates a PHP Warning, which is considered a bug.
The question is, how do I write my PHPUnit test case so that it succeeds when the functions handles null/-1 gracefully, and fail when there is a PHP Warning thrown?
PHPUnit_Util_ErrorHandler::handleError() throws one of several exception types based on the error code:
PHPUnit_Framework_Error_Notice for E_NOTICE, E_USER_NOTICE, and E_STRICT
PHPUnit_Framework_Error_Warning for E_WARNING and E_USER_WARNING
PHPUnit_Framework_Error for all others
You can catch and expect these as you would any other exception.
/**
* #expectedException PHPUnit_Framework_Error_Warning
*/
function testNegativeNumberTriggersWarning() {
$fixture = new someClass;
$fixture->loadValue(-1);
}
I would create a separate case to test when the notice/warning is expected.
For PHPUnit v6.0+ this is the up to date syntax:
use PHPUnit\Framework\Error\Notice;
use PHPUnit\Framework\Error\Warning;
use PHPUnit\Framework\TestCase;
class YourShinyNoticeTest extends TestCase
{
public function test_it_emits_a_warning()
{
$this->expectException(Warning::class);
file_get_contents('/nonexistent_file'); // This will emit a PHP Warning, so test passes
}
public function test_it_emits_a_notice()
{
$this->expectException(Notice::class);
$now = new \DateTime();
$now->whatever; // Notice gets emitted here, so the test will pass
}
}
What worked for me was modifying my phpunit.xml to have
<phpunit
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
strict="true"
>
</phpunit>
The key was to use strict="true" to get the warnings to result in a failed test.
You can also write a phpunit.xml file (on your tests dir) with this:
<phpunit
convertErrorsToExceptions="true"
convertNoticesToExceptions="false"
stopOnFailure="false">
</phpunit>
Using Netsilik/BaseTestCase (MIT License) you can test directly for triggered Errors/Warnings, without converting them to Exceptions:
composer require netsilik/base-test-case
Testing for an E_USER_NOTICE:
<?php
namespace Tests;
class MyTestCase extends \Netsilik\Testing\BaseTestCase
{
/**
* {#inheritDoc}
*/
public function __construct($name = null, array $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);
$this->_convertNoticesToExceptions = false;
$this->_convertWarningsToExceptions = false;
$this->_convertErrorsToExceptions = true;
}
public function test_whenNoticeTriggered_weCanTestForIt()
{
$foo = new Foo();
$foo->bar();
self::assertErrorTriggered(E_USER_NOTICE, 'The warning string');
}
}
Hope this helps someone in the future.
public function testFooBar(): void
{
// this is required
$this->expectWarning();
// these are optional
$this->expectWarningMessage('fopen(/tmp/non-existent): Failed to open stream: No such file or directory');
$this->expectWarningMessageMatches('/No such file or directory/');
fopen('/tmp/non-existent', 'rb');
}
Make SomeClass throw an error when input is invalid and tell phpUnit to expect an error.
One method is this:
class ExceptionTest extends PHPUnit_Framework_TestCase
{
public function testLoadValueWithNull()
{
$o = new SomeClass();
$this->setExpectedException('InvalidArgumentException');
$this->assertInstanceOf('InvalidArgumentException', $o::loadValue(null));
}
}
See documentation for more methods.

Resources