Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
khelle committed Jun 24, 2017
1 parent 7d084e8 commit 796fb19
Show file tree
Hide file tree
Showing 30 changed files with 2,605 additions and 1 deletion.
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,32 @@ php:
- 7.1

before_install:
- sudo ln -s /home/travis/.phpenv/versions/$(phpenv version-name)/bin/phpize /usr/bin/
- sudo ln -s /home/travis/.phpenv/versions/$(phpenv version-name)/bin/php-config /usr/bin/
- export PHP_MAJOR="$(echo $TRAVIS_PHP_VERSION | cut -d '.' -f 1,2)"

install:
- travis_retry composer self-update
- travis_retry composer install --prefer-source --no-interaction --ignore-platform-reqs
- sudo apt-get -qq update
- bash build-ci/install_prereqs_$PHP_MAJOR.sh
- php -m

before_script:
- ulimit -c unlimited -S || true
- echo '/tmp/core_%e.%p' | sudo tee /proc/sys/kernel/core_pattern &> /dev/null

script:
- vendor/bin/phpunit -d memory_limit=1024M --coverage-text --coverage-clover=coverage.clover

after_script:
- if [ "$TRAVIS_PHP_VERSION" = "7.1" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
- if [ "$TRAVIS_PHP_VERSION" = "7.1" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi

after_failure:
- bash build-ci/install_failure.sh

addons:
apt:
packages:
- gdb
1 change: 1 addition & 0 deletions build-ci/ini/ssh2.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extension=ssh2.so
23 changes: 23 additions & 0 deletions build-ci/install_failure.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
#
# This source file is subject to the MIT License that is bundled
# with this package in the MIT license.

shopt -s nullglob
export LC_ALL=C

for i in /tmp/core_*.*; do
if [ -f "$i" -a "$(file "$i" | grep -o 'core file')" ]; then
gdb -q $(phpenv which php) "$i" <<EOF
set pagination 0
backtrace full
info registers
x/16i \$pc
thread apply all backtrace
quit
EOF
fi
done

$(phpenv which php) -m
$(phpenv which php) -i
24 changes: 24 additions & 0 deletions build-ci/install_php_5.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
#
# This source file is subject to the MIT License that is bundled
# with this package in the MIT license.

CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
TRAVIS_BUILD_DIR="${TRAVIS_BUILD_DIR:-$(dirname $(dirname $CURRENT_DIR))}"

install_ssh2() {
sudo apt-get install -y -qq libssh2-1-dev libssh2-1

git clone -q https://github.com/php/pecl-networking-ssh2 -b php5 /tmp/ssh2
cd /tmp/ssh2

phpize &> /dev/null
./configure &> /dev/null

make --silent -j4 &> /dev/null
make --silent install

if [ -z $(php -m | grep ssh2) ]; then
phpenv config-add "${TRAVIS_BUILD_DIR}/build-ci/ini/ssh2.ini"
fi
}
24 changes: 24 additions & 0 deletions build-ci/install_php_7.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
#
# This source file is subject to the MIT License that is bundled
# with this package in the MIT license.

CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
TRAVIS_BUILD_DIR="${TRAVIS_BUILD_DIR:-$(dirname $(dirname $CURRENT_DIR))}"

install_ssh2() {
sudo apt-get install -y -qq libssh2-1-dev libssh2-1

git clone -q https://github.com/php/pecl-networking-ssh2 -b master /tmp/ssh2
cd /tmp/ssh2

phpize &> /dev/null
./configure &> /dev/null

make --silent -j4 &> /dev/null
make --silent install

if [ -z $(php -m | grep ssh2) ]; then
phpenv config-add "${TRAVIS_BUILD_DIR}/build-ci/ini/ssh2.ini"
fi
}
28 changes: 28 additions & 0 deletions build-ci/install_php_common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash
#
# This source file is subject to the MIT License that is bundled
# with this package in the MIT license.

CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
TRAVIS_BUILD_DIR="${TRAVIS_BUILD_DIR:-$(dirname $(dirname $CURRENT_DIR))}"

pecl channel-update pecl.php.net || true
echo `whoami`":1234" | sudo chpasswd

enable_extension() {
if [ -z $(php -m | grep "${1}") ] && [ -f "${TRAVIS_BUILD_DIR}/build-ci/ini/${1}.ini" ]; then
phpenv config-add "${TRAVIS_BUILD_DIR}/build-ci/ini/${1}.ini"
fi
}

install_extension() {
INSTALLED=$(pecl list "${1}" | grep 'not installed')

if [ -z "${INSTALLED}" ]; then
printf "\n" | pecl upgrade "${1}" &> /dev/null
else
printf "\n" | pecl install "${1}" &> /dev/null
fi

enable_extension "${1}"
}
12 changes: 12 additions & 0 deletions build-ci/install_prereqs_5.6.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
#
# This source file is subject to the MIT License that is bundled
# with this package in the MIT license.

CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
TRAVIS_BUILD_DIR="${TRAVIS_BUILD_DIR:-$(dirname $(dirname $CURRENT_DIR))}"

source ${TRAVIS_BUILD_DIR}/build-ci/install_php_common.sh
source ${TRAVIS_BUILD_DIR}/build-ci/install_php_5.sh

install_ssh2
12 changes: 12 additions & 0 deletions build-ci/install_prereqs_7.0.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
#
# This source file is subject to the MIT License that is bundled
# with this package in the MIT license.

CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
TRAVIS_BUILD_DIR="${TRAVIS_BUILD_DIR:-$(dirname $(dirname $CURRENT_DIR))}"

source ${TRAVIS_BUILD_DIR}/build-ci/install_php_common.sh
source ${TRAVIS_BUILD_DIR}/build-ci/install_php_7.sh

install_ssh2
12 changes: 12 additions & 0 deletions build-ci/install_prereqs_7.1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
#
# This source file is subject to the MIT License that is bundled
# with this package in the MIT license.

CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
TRAVIS_BUILD_DIR="${TRAVIS_BUILD_DIR:-$(dirname $(dirname $CURRENT_DIR))}"

source ${TRAVIS_BUILD_DIR}/build-ci/install_php_common.sh
source ${TRAVIS_BUILD_DIR}/build-ci/install_php_7.sh

install_ssh2
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"autoload": {
"psr-4": {
"Dazzle\\SSH\\": "src/Event",
"Dazzle\\SSH\\": "src/SSH",
"Dazzle\\SSH\\Test\\": "test"
}
},
Expand Down
9 changes: 9 additions & 0 deletions test/Callback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Dazzle\SSH\Test;

class Callback
{
public function __invoke()
{}
}
160 changes: 160 additions & 0 deletions test/TModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php

namespace Dazzle\SSH\Test;

use Dazzle\Loop\Model\SelectLoop;
use Dazzle\Loop\Loop;
use Dazzle\Loop\LoopExtendedInterface;
use Dazzle\Loop\LoopInterface;
use Dazzle\SSH\Test\_Simulation\Event;
use Dazzle\SSH\Test\_Simulation\Simulation;
use Exception;

class TModule extends TUnit
{
/**
* @var string
*/
const MSG_EVENT_NAME_ASSERTION_FAILED = 'Expected event name mismatch on %s event.';

/**
* @var string
*/
const MSG_EVENT_DATA_ASSERTION_FAILED = 'Expected event data mismatch on %s event.';

/**
* @var string
*/
const MSG_EVENT_GET_ASSERTION_FAILED = "Expected event count mismatch after %s events.\nExpected event %s, got event %s.";

/**
* @var LoopExtendedInterface
*/
private $loop;

/**
* @var Simulation
*/
private $sim;

/**
*
*/
public function setUp()
{
$this->loop = null;
$this->sim = null;
}

/**
*
*/
public function tearDown()
{
unset($this->sim);
unset($this->loop);
}

/**
* @return LoopInterface|null
*/
public function getLoop()
{
return $this->loop;
}

/**
* Run test scenario as simulation.
*
* @param callable(Simulation) $scenario
* @return TModule
*/
public function simulate(callable $scenario)
{
try
{
$this->loop = new Loop(new SelectLoop);
$this->loop->erase(true);

$this->sim = new Simulation($this->loop);
$this->sim->setScenario($scenario);
$this->sim->begin();
}
catch (Exception $ex)
{
$this->fail($ex->getMessage());
}

return $this;
}

/**
* @param $events
* @param int $flags
* @return TModule
*/
public function expect($events, $flags = Simulation::EVENTS_COMPARE_IN_ORDER)
{
$expectedEvents = [];

foreach ($events as $event)
{
$data = isset($event[1]) ? $event[1] : [];
$expectedEvents[] = new Event($event[0], $data);
}

$this->assertEvents(
$this->sim->getExpectations(),
$expectedEvents,
$flags
);

return $this;
}

/**
* @param Event[] $actualEvents
* @param Event[] $expectedEvents
* @param int $flags
*/
public function assertEvents($actualEvents = [], $expectedEvents = [], $flags = Simulation::EVENTS_COMPARE_IN_ORDER)
{
$count = max(count($actualEvents), count($expectedEvents));

if ($flags === Simulation::EVENTS_COMPARE_RANDOMLY)
{
sort($actualEvents);
sort($expectedEvents);
}

for ($i=0; $i<$count; $i++)
{
if (!isset($actualEvents[$i]))
{
$this->fail(
sprintf(self::MSG_EVENT_GET_ASSERTION_FAILED, $i, $expectedEvents[$i]->name(), 'null')
);
}
else if (!isset($expectedEvents[$i]))
{
$this->fail(
sprintf(self::MSG_EVENT_GET_ASSERTION_FAILED, $i, 'null', $actualEvents[$i]->name())
);
}

$actualEvent = $actualEvents[$i];
$expectedEvent = $expectedEvents[$i];

$this->assertSame(
$expectedEvent->name(),
$actualEvent->name(),
sprintf(self::MSG_EVENT_NAME_ASSERTION_FAILED, $i)
);
$this->assertSame(
$expectedEvent->data(),
$actualEvent->data(),
sprintf(self::MSG_EVENT_DATA_ASSERTION_FAILED, $i)
);
}
}
}
Loading

0 comments on commit 796fb19

Please sign in to comment.