Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Console Contents Assertion Error #37

Open
wandersonwhcr opened this issue Sep 29, 2016 · 3 comments
Open

Console Contents Assertion Error #37

wandersonwhcr opened this issue Sep 29, 2016 · 3 comments
Labels

Comments

@wandersonwhcr
Copy link

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
Copy link
Author

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 👍

@adamlundrigan
Copy link

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
Copy link
Member

This repository has been closed and moved to laminas/laminas-test; a new issue has been opened at laminas/laminas-test#4.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants