peridot-php / peridot

Event driven BDD test framework for PHP
http://peridot-php.github.io/
MIT License
328 stars 27 forks source link

Warning about an invalid amount of parameters to a method call is not apparent #158

Closed JoelLarson closed 8 years ago

JoelLarson commented 9 years ago

This test case demonstrates that a PHP call will silently ignore the E_WARNING that is thrown by PHP due to PhpClass->add() not having enough arguments to be satisfied. This has lead to some unintentional side effects while testing.

<?php

class PhpClass
{
    public function add($name, $email, $password, $knownPerson = false)
    {
        return [
            'name' => $name,
            'email' => $email,
            'password' => $password,
            'knownPerson' => $knownPerson
        ];
    }
}

describe('PhpClass', function () {
    context('when calling a method with too few parameters', function () {
        it('should bail and give an error', function () {

            $phpClass = new PhpClass();

            $output = $phpClass->add();

            expect($output)->to->equal([
                'name' => null,
                'email' => null,
                'password' => null,
                'knownPerson' => false
            ]);

        });
    });
});

Output:

vagrant@localhost:/var/www/project$ ./vendor/bin/peridot tests/spec/Error.spec.php 

  PhpClass
    when calling a method with too few parameters
      ✓ should bail and give an error

  1 passing (9 ms)

The test error was picked up and accepted once I added an event handler on the Emitter "error" event. It might be worth adding some kind of functionality that has a basic error_handling event attached that can be overwritten easily?

brianium commented 9 years ago

Certainly related to #157. This kind of thing makes me lean towards a peridot-strict plugin, or a --strict setting. Or as you suggest above, maybe the Runner could expose a strict setting that can be easily enabled via config?

JoelLarson commented 9 years ago

I'll just reference both here since this issue/ticket is more important to me.

The biggest problem here is that I've had quite a few tests break after applying my emitter event hook. Problems are undefined constants being ignored, invalid amounts of arguments in code being ignored and given NULL, having user errors not being populated.

Practically everything in PHP that does not throw an exception but is an notice/warning/error ends with the test passing. I will agree that notices are normally fine to ignore, but there are a lot of scenarios where warnings (like invalid amounts of arguments) are very valuable.

JoelLarson commented 9 years ago

I think it would be fine to just provide a very basic amount of detection for types of errors that can be easily overwritten. Then maybe provide a peridot-strict to add additional advanced functionality to really buckle down on possible errors that are preventable.

brianium commented 9 years ago

I see your point. I agree that a plugin would be a little annoying for something so essential. I don't believe Evenement has a way to control priority short of removing listeners - which might be fine? I might need a little help answering a couple of questions:

Events are a great way to handle configuration, I am just unsure of what makes sense as a default case, and what part of the API should be responsible for managing things like error levels and differentiating between them. Does it make sense to take a cue from PHPUnit on the matter?

brianium commented 9 years ago

I had a feature slated for 2.0 mentioned in #132 - a little related in that it would give the feedback, but would not fail the test.