zendframework / zend-test

Test component from Zend Framework
BSD 3-Clause "New" or "Revised" License
18 stars 38 forks source link

Console Contents Assertion Error #37

Open wandersonwhcr opened 7 years ago

wandersonwhcr commented 7 years ago

Hi,

If I use "console write" in objects of Zend\Mvc\Console\Controller\AbstractConsoleController, when testing, it outputs the content and assertConsoleOutputContents don't recognize the output.

Example:


namespace Foo\Bar\Console;

use Zend\Mvc\Console\Controller\AbstractConsoleController;

class Baz extends AbstractConsoleController
{
    public function quxAction()
    {
        $this->getConsole()->writeLine('Hello');
    }
}
<?php

namespace FooTest\Bar\Console;

use Zend\Test\PHPUnit\Controller\AbstractConsoleControllerTestCase as TestCase;

class BazTest extends TestCase
{
    protected function setUp()
    {
        $this->setApplicationConfig(include './config/autoload.php');
        parent::setUp();
    }

    public function testQuxAction()
    {
        $this->dispatch('foo-bar baz-qux');

        $this->assertConsoleOutputContains('Hello');
    }
}

It outputs:

$ php vendor/bin/phpunit --filter FooTest\\Bar\\Console\\BazTest::testQuxAction
PHPUnit 5.5.3 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.0.10-1~dotdeb+8.1 with Xdebug 2.4.1
Configuration: /vagrant/phpunit.xml

F                                                                   1 / 1 (100%)Hello

Time: 2.46 seconds, Memory: 8.00MB

There was 1 failure:

1) FooTest\Bar\Console\BazTest::testQuxAction
Failed asserting output CONTAINS content "Hello", actual content is ""

/vagrant/vendor/zendframework/zend-test/src/PHPUnit/Controller/AbstractConsoleControllerTestCase.php:32
/vagrant/test/FooTest/Bar/Console/BazTest.php:19

FAILURES!
Tests: 1, Assertions: 0, Failures: 1.
wandersonwhcr commented 7 years ago

Checking the source code, the method "assertConsoleOutputContains" gets the content of Reponse object and not the Console object.

I'll create a mock for Console object.

IMHO, we need to add another assertion method.

ty :+1:

adamlundrigan commented 7 years ago

Confirmed, though I think this is more a tradeoff in applying ZF2's MVC model to the console paradigm than an outright bug. In order for your output to be picked up by assertConsoleOutputContains you need to return the string from the controller action rather than calling writeLine on the console adapter directly. assertConsoleOutputContains only checks the controller's Response object and not the contents of the Console adapter also composed into the controller. The console adapters do not buffer so the test helper cannot introspect the output.

Also an obligatory note: zend-mvc-console is deprecated in favor of zf-console.

weierophinney commented 4 years ago

This repository has been closed and moved to laminas/laminas-test; a new issue has been opened at https://github.com/laminas/laminas-test/issues/4.