В PHP есть способ проверить взаимодействия CLI?

1

Вопрос, как в названии. В методе примера у меня есть проблемы с тестированием: https://github.com/rzajac/phptools/blob/master/src/Cli/Interaction.php#L36

Теги:
phpunit

1 ответ

0
Лучший ответ

Вы можете высмеивать fgets(), если у вас есть пространства имен.

namespace My\Namespace;

class SomeClass
{
    public static function getPassword($prompt = '')
    {
        echo $prompt;
        \system('stty -echo');

        // Notice how the global function "fgets()" is called without the leading backslash 
        // (relative instead of absolute call in a namespaced environment). 
        // This will let us later mock the call
        $pass = fgets(STDIN);

        \system('stty echo');
        return $pass;
    }
}

Тестовый файл:

namespace My\Namespace {
    // And this here, does the trick: it will override the fgets()
    // function in your code *just for the namespace* where you are defining it.
    function fgets($Handle) {
        return 'Pa$$Word';      // Password Text for testing
    }

include_once './SomeClass.php';

class Test_SomeClass extends \PHPUnit_Framework_TestCase
{
    /**
     * This will test the success case.
     * @test
     */
    public function testPass()
    {
        $dummy = new \My\Namespace\SomeClass;
        $this->assertEqual('Pa$$Word', $dummy->getPassword());
    }
}

В этой статье также обсуждается пример сокета, который обрабатывает исключения.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню