PHPackages                             postcon/behat-shell-extension - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. postcon/behat-shell-extension

ActiveBehat-extension[Utility &amp; Helpers](/categories/utility)

postcon/behat-shell-extension
=============================

A Behat Extension for executing shell commands remote and locally

529.3k—2.9%3[1 PRs](https://github.com/Postcon/BehatShellExtension/pulls)PHPCI failing

Since Feb 6Pushed 5y ago5 watchersCompare

[ Source](https://github.com/Postcon/BehatShellExtension)[ Packagist](https://packagist.org/packages/postcon/behat-shell-extension)[ RSS](/packages/postcon-behat-shell-extension/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (2)Used By (0)

BehatShellExtension [![Build Status](https://camo.githubusercontent.com/5dff015a103559ae81c66417de00b2d7567acd952890fd4c24c13e0216926624/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f506f7374636f6e2f42656861745368656c6c457874656e73696f6e2e706e67)](http://travis-ci.org/Postcon/BehatShellExtension)
=====================================================================================================================================================================================================================================================================================================================

[](#behatshellextension-)

Behat extension for executing shell commands within features. The shell commands can be run on remote servers using ssh or locally without network. Additionally, local files can be deployed to directories on remote servers.

Installation
------------

[](#installation)

Using [composer](https://getcomposer.org/download/):

```
composer require postcon/behat-shell-extension dev-master

```

Usage
-----

[](#usage)

```
# run.feature

Feature: Running commands
  In order to run useful integration tests
  As a tester
  I want to execute shell commands and check their output

  Scenario: Run command on the default shell/server and define expected output
    When I run "pwd"
    Then It should pass
    And I see
    """
    /tmp
    """

  Scenario: Run command on the default shell/server and define expected output in inline-style
    When I run "pwd"
    Then It should pass
    And I see "/tmp"

  Scenario: Run command on the shell/server "app"
    When I run "app/console --env=prod do:mi:mi" on "app"
    Then It should pass
```

```
# copy.feature

Feature: Copy file
  In order to prepare integration tests
  As a tester
  I want to copy files to directories (on remote servers)

  Scenario: Copy a file to /tmp directory on default server (or at the local filesystem)
    Given I copy file "test.txt" to "/tmp"
    And I run "cat /tmp/test.txt"
    Then it should pass
    And I see
    """
    content of test.txt
    """

  Scenario: Copy a file to /tmp directory on "app" server
    Given I copy file "test.txt" to "/tmp" on "app"
    And I run "cat /tmp/test.txt" on "app"
    Then it should pass
    And I see
    """
    content of test.txt
    """
```

Configuration
-------------

[](#configuration)

To use the BehatShellExtension, it needs to be configured in the `behat.yml` (or `behat.yml.dist`). Each server or shell, you want invoke commands on, must be specified.

### Local shell

[](#local-shell)

Following example shows the minimal configuration for a local shell.

```
# behat.yml
extensions:
  ShellExtension:
    default:
      type: local
```

It is possible, to give two additional configuration parameters: the command execution`base_dir` and the `timeout` (in seconds; if the commands does not terminate within this timeout, it gets stopped and the behat feature fails).

```
# behat.yml
extensions:
  ShellExtension:
    default:
      type: local
      base_dir: /tmp
      timeout: 10
```

### Remote server / ssh

[](#remote-server--ssh)

For accessing a remote server via ssh, a minimal configuration is like this:

```
# behat.yml
extensions:
  ShellExtension:
    ...
    app:
      type: remote
      ssh_hostname: user@shell.example.com
```

The `ssh_hostname` specifies the name of the ssh server and the username. Using additional parameters, the ssh connection can be configured and the *ssh* and *scp* binaries can be specified:

```
# behat.yml
extensions:
  ShellExtension:
    ...
    app:
      type: remote
      base_dir: /tmp
      ssh_hostname: user@shell.example.com
      ssh_options: -i ~/.ssh/id_rsa
      ssh_command: /usr/bin/ssh
      scp_command: /usr/bin/scp
      timeout: 20
```

If we have this feature example

```
Scenario:
  Given I copy file "test.txt" to "/tmp" on "app"
  And I run "cat /tmp/test.txt" on "app"

```

then the resulting commands would be this:

```
/usr/bin/scp -i ~/.ssh/id_rsa 'test.txt' 'user@shell.example.com:/tmp'
/usr/bin/ssh -i ~/.ssh/id_rsa user@shell.example.com 'cd /tmp ; cat /tmp/test.txt'

```

### Docker

[](#docker)

To execute commands in a *[docker](https://docs.docker.com/) container*, the following minimal configuration is appropriate:

```
# behat.yml
extensions:
  ShellExtension:
    ...
    app:
      type: docker
      docker_containername: app
```

Here, we assume to have a docker container like this:

`docker run --name=app -d nginx`

A more extensive configuration is this:

```
# behat.yml
extensions:
  ShellExtension:
    ...
    app:
      type: docker
      base_dir: /tmp
      docker_containername: app
      docker_command: /usr/local/bin/docker
      docker_options: -u user
      timeout: 20
```

Here, the location of the *docker executable* is given and *options*, if needed.

If we have this feature example

```
Scenario:
  Given I copy file "test.txt" to "/tmp" on "app"
  And I run "cat /tmp/test.txt" on "app"

```

then the resulting commands would be this:

```
/usr/local/bin/docker cp 'test.txt' app:'/tmp'
/usr/local/bin/docker exec -u user app /bin/bash -c 'cd /tmp ; cat /tmp/test.txt'

```

### Docker-Compose

[](#docker-compose)

By changing the parameter `docker_command`, instead of a docker container, a *[docker-compose](https://docs.docker.com/compose/) service* can be used:

```
# behat.yml
extensions:
  ShellExtension:
    app:
      type: docker
      base_dir: /tmp
      docker_containername: app
      docker_command: /usr/local/bin/docker-compose
      docker-options: -T
      timeout: 20
```

It is important to specify `docker-options: -T` to »Disable pseudo-tty allocation«.

Here, we assume to have a docker-compose configuration like this:

```
# docker-compose.yml
version: '2'
services:
  app:
    image: php:7.1-fpm
```

Right now, **it is not possible to copy** files into a running docker-compose service (i.e. a command `docker-compose cp` is missing).

Internal implementation
-----------------------

[](#internal-implementation)

A command string `$command` is executed on a shell with `type: local` gets invoked in following way:

```
$process = new Process($command, $serverConfig['base_dir']);
$process->setTimeout($serverConfig['timeout']);
$process->run();
```

A remote executed command string `$command` is executed this way:

```
if ($serverConfig['base_dir']) {
    $command = sprintf('cd %s ; %s', $serverConfig['base_dir'], $command);
}
$command = sprintf(
    '%s %s %s %s',
    $serverConfig['ssh_command'],
    $serverConfig['ssh_options'],
    $serverConfig['ssh_hostname'],
    escapeshellarg($command)
);

// e.g. ssh -i ~/.ssh/id_rsa user@shell.example.com 'cd /var/www ; app/console --env=prod do:mi:mi'

$process = new Process($command);
$process->setTimeout($serverConfig['timeout']);
$process->run();
```

When using docker, a command string `$command` is executed this way:

```
if ($serverConfig['base_dir']) {
    $command = sprintf('cd %s ; %s', $serverConfig['base_dir'], $command);
}

$command = sprintf(
    '%s exec %s /bin/bash -c %s',
    $serverConfig['docker_command'],
    $serverConfig['docker_containername'],
    escapeshellarg($command)
);

// e.g. docker exec container /bin/bash -c 'cd /var/www ; app/console --env=prod do:mi:mi'

$process = new Process($command);
$process->setTimeout($serverConfig['timeout']);
$process->run();
```

License
-------

[](#license)

All contents of this package are licensed under the [MIT license](LICENSE).

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 94.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/5d3765b40a043c8c7c8ead0ae3cb9691ab6c053d272ec9db1d9bcec0218fb69a?d=identicon)[DrSchimke](/maintainers/DrSchimke)

![](https://www.gravatar.com/avatar/43a51254a0d2da97e28dd6790c3b939deea824cc42412b9d9a289200626d406a?d=identicon)[puhbooh](/maintainers/puhbooh)

---

Top Contributors

[![DrSchimke](https://avatars.githubusercontent.com/u/3299009?v=4)](https://github.com/DrSchimke "DrSchimke (34 commits)")[![hrba](https://avatars.githubusercontent.com/u/2995275?v=4)](https://github.com/hrba "hrba (1 commits)")[![puhbooh](https://avatars.githubusercontent.com/u/3405154?v=4)](https://github.com/puhbooh "puhbooh (1 commits)")

### Embed Badge

![Health badge](/badges/postcon-behat-shell-extension/health.svg)

```
[![Health](https://phpackages.com/badges/postcon-behat-shell-extension/health.svg)](https://phpackages.com/packages/postcon-behat-shell-extension)
```

###  Alternatives

[google/cloud-core

Google Cloud PHP shared dependency, providing functionality useful to all components.

346132.9M112](/packages/google-cloud-core)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
