Skip to content

Commit 796fb19

Browse files
committed
Added tests
1 parent 7d084e8 commit 796fb19

30 files changed

+2605
-1
lines changed

.travis.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,32 @@ php:
99
- 7.1
1010

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

1416
install:
1517
- travis_retry composer self-update
1618
- travis_retry composer install --prefer-source --no-interaction --ignore-platform-reqs
19+
- sudo apt-get -qq update
20+
- bash build-ci/install_prereqs_$PHP_MAJOR.sh
1721
- php -m
1822

23+
before_script:
24+
- ulimit -c unlimited -S || true
25+
- echo '/tmp/core_%e.%p' | sudo tee /proc/sys/kernel/core_pattern &> /dev/null
26+
1927
script:
2028
- vendor/bin/phpunit -d memory_limit=1024M --coverage-text --coverage-clover=coverage.clover
2129

2230
after_script:
2331
- if [ "$TRAVIS_PHP_VERSION" = "7.1" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
2432
- if [ "$TRAVIS_PHP_VERSION" = "7.1" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi
33+
34+
after_failure:
35+
- bash build-ci/install_failure.sh
36+
37+
addons:
38+
apt:
39+
packages:
40+
- gdb

build-ci/ini/ssh2.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extension=ssh2.so

build-ci/install_failure.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This source file is subject to the MIT License that is bundled
4+
# with this package in the MIT license.
5+
6+
shopt -s nullglob
7+
export LC_ALL=C
8+
9+
for i in /tmp/core_*.*; do
10+
if [ -f "$i" -a "$(file "$i" | grep -o 'core file')" ]; then
11+
gdb -q $(phpenv which php) "$i" <<EOF
12+
set pagination 0
13+
backtrace full
14+
info registers
15+
x/16i \$pc
16+
thread apply all backtrace
17+
quit
18+
EOF
19+
fi
20+
done
21+
22+
$(phpenv which php) -m
23+
$(phpenv which php) -i

build-ci/install_php_5.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This source file is subject to the MIT License that is bundled
4+
# with this package in the MIT license.
5+
6+
CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
7+
TRAVIS_BUILD_DIR="${TRAVIS_BUILD_DIR:-$(dirname $(dirname $CURRENT_DIR))}"
8+
9+
install_ssh2() {
10+
sudo apt-get install -y -qq libssh2-1-dev libssh2-1
11+
12+
git clone -q https://github.com/php/pecl-networking-ssh2 -b php5 /tmp/ssh2
13+
cd /tmp/ssh2
14+
15+
phpize &> /dev/null
16+
./configure &> /dev/null
17+
18+
make --silent -j4 &> /dev/null
19+
make --silent install
20+
21+
if [ -z $(php -m | grep ssh2) ]; then
22+
phpenv config-add "${TRAVIS_BUILD_DIR}/build-ci/ini/ssh2.ini"
23+
fi
24+
}

build-ci/install_php_7.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This source file is subject to the MIT License that is bundled
4+
# with this package in the MIT license.
5+
6+
CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
7+
TRAVIS_BUILD_DIR="${TRAVIS_BUILD_DIR:-$(dirname $(dirname $CURRENT_DIR))}"
8+
9+
install_ssh2() {
10+
sudo apt-get install -y -qq libssh2-1-dev libssh2-1
11+
12+
git clone -q https://github.com/php/pecl-networking-ssh2 -b master /tmp/ssh2
13+
cd /tmp/ssh2
14+
15+
phpize &> /dev/null
16+
./configure &> /dev/null
17+
18+
make --silent -j4 &> /dev/null
19+
make --silent install
20+
21+
if [ -z $(php -m | grep ssh2) ]; then
22+
phpenv config-add "${TRAVIS_BUILD_DIR}/build-ci/ini/ssh2.ini"
23+
fi
24+
}

build-ci/install_php_common.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This source file is subject to the MIT License that is bundled
4+
# with this package in the MIT license.
5+
6+
CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
7+
TRAVIS_BUILD_DIR="${TRAVIS_BUILD_DIR:-$(dirname $(dirname $CURRENT_DIR))}"
8+
9+
pecl channel-update pecl.php.net || true
10+
echo `whoami`":1234" | sudo chpasswd
11+
12+
enable_extension() {
13+
if [ -z $(php -m | grep "${1}") ] && [ -f "${TRAVIS_BUILD_DIR}/build-ci/ini/${1}.ini" ]; then
14+
phpenv config-add "${TRAVIS_BUILD_DIR}/build-ci/ini/${1}.ini"
15+
fi
16+
}
17+
18+
install_extension() {
19+
INSTALLED=$(pecl list "${1}" | grep 'not installed')
20+
21+
if [ -z "${INSTALLED}" ]; then
22+
printf "\n" | pecl upgrade "${1}" &> /dev/null
23+
else
24+
printf "\n" | pecl install "${1}" &> /dev/null
25+
fi
26+
27+
enable_extension "${1}"
28+
}

build-ci/install_prereqs_5.6.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This source file is subject to the MIT License that is bundled
4+
# with this package in the MIT license.
5+
6+
CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
7+
TRAVIS_BUILD_DIR="${TRAVIS_BUILD_DIR:-$(dirname $(dirname $CURRENT_DIR))}"
8+
9+
source ${TRAVIS_BUILD_DIR}/build-ci/install_php_common.sh
10+
source ${TRAVIS_BUILD_DIR}/build-ci/install_php_5.sh
11+
12+
install_ssh2

build-ci/install_prereqs_7.0.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This source file is subject to the MIT License that is bundled
4+
# with this package in the MIT license.
5+
6+
CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
7+
TRAVIS_BUILD_DIR="${TRAVIS_BUILD_DIR:-$(dirname $(dirname $CURRENT_DIR))}"
8+
9+
source ${TRAVIS_BUILD_DIR}/build-ci/install_php_common.sh
10+
source ${TRAVIS_BUILD_DIR}/build-ci/install_php_7.sh
11+
12+
install_ssh2

build-ci/install_prereqs_7.1.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This source file is subject to the MIT License that is bundled
4+
# with this package in the MIT license.
5+
6+
CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
7+
TRAVIS_BUILD_DIR="${TRAVIS_BUILD_DIR:-$(dirname $(dirname $CURRENT_DIR))}"
8+
9+
source ${TRAVIS_BUILD_DIR}/build-ci/install_php_common.sh
10+
source ${TRAVIS_BUILD_DIR}/build-ci/install_php_7.sh
11+
12+
install_ssh2

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
},
3636
"autoload": {
3737
"psr-4": {
38-
"Dazzle\\SSH\\": "src/Event",
38+
"Dazzle\\SSH\\": "src/SSH",
3939
"Dazzle\\SSH\\Test\\": "test"
4040
}
4141
},

test/Callback.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Dazzle\SSH\Test;
4+
5+
class Callback
6+
{
7+
public function __invoke()
8+
{}
9+
}

test/TModule.php

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
3+
namespace Dazzle\SSH\Test;
4+
5+
use Dazzle\Loop\Model\SelectLoop;
6+
use Dazzle\Loop\Loop;
7+
use Dazzle\Loop\LoopExtendedInterface;
8+
use Dazzle\Loop\LoopInterface;
9+
use Dazzle\SSH\Test\_Simulation\Event;
10+
use Dazzle\SSH\Test\_Simulation\Simulation;
11+
use Exception;
12+
13+
class TModule extends TUnit
14+
{
15+
/**
16+
* @var string
17+
*/
18+
const MSG_EVENT_NAME_ASSERTION_FAILED = 'Expected event name mismatch on %s event.';
19+
20+
/**
21+
* @var string
22+
*/
23+
const MSG_EVENT_DATA_ASSERTION_FAILED = 'Expected event data mismatch on %s event.';
24+
25+
/**
26+
* @var string
27+
*/
28+
const MSG_EVENT_GET_ASSERTION_FAILED = "Expected event count mismatch after %s events.\nExpected event %s, got event %s.";
29+
30+
/**
31+
* @var LoopExtendedInterface
32+
*/
33+
private $loop;
34+
35+
/**
36+
* @var Simulation
37+
*/
38+
private $sim;
39+
40+
/**
41+
*
42+
*/
43+
public function setUp()
44+
{
45+
$this->loop = null;
46+
$this->sim = null;
47+
}
48+
49+
/**
50+
*
51+
*/
52+
public function tearDown()
53+
{
54+
unset($this->sim);
55+
unset($this->loop);
56+
}
57+
58+
/**
59+
* @return LoopInterface|null
60+
*/
61+
public function getLoop()
62+
{
63+
return $this->loop;
64+
}
65+
66+
/**
67+
* Run test scenario as simulation.
68+
*
69+
* @param callable(Simulation) $scenario
70+
* @return TModule
71+
*/
72+
public function simulate(callable $scenario)
73+
{
74+
try
75+
{
76+
$this->loop = new Loop(new SelectLoop);
77+
$this->loop->erase(true);
78+
79+
$this->sim = new Simulation($this->loop);
80+
$this->sim->setScenario($scenario);
81+
$this->sim->begin();
82+
}
83+
catch (Exception $ex)
84+
{
85+
$this->fail($ex->getMessage());
86+
}
87+
88+
return $this;
89+
}
90+
91+
/**
92+
* @param $events
93+
* @param int $flags
94+
* @return TModule
95+
*/
96+
public function expect($events, $flags = Simulation::EVENTS_COMPARE_IN_ORDER)
97+
{
98+
$expectedEvents = [];
99+
100+
foreach ($events as $event)
101+
{
102+
$data = isset($event[1]) ? $event[1] : [];
103+
$expectedEvents[] = new Event($event[0], $data);
104+
}
105+
106+
$this->assertEvents(
107+
$this->sim->getExpectations(),
108+
$expectedEvents,
109+
$flags
110+
);
111+
112+
return $this;
113+
}
114+
115+
/**
116+
* @param Event[] $actualEvents
117+
* @param Event[] $expectedEvents
118+
* @param int $flags
119+
*/
120+
public function assertEvents($actualEvents = [], $expectedEvents = [], $flags = Simulation::EVENTS_COMPARE_IN_ORDER)
121+
{
122+
$count = max(count($actualEvents), count($expectedEvents));
123+
124+
if ($flags === Simulation::EVENTS_COMPARE_RANDOMLY)
125+
{
126+
sort($actualEvents);
127+
sort($expectedEvents);
128+
}
129+
130+
for ($i=0; $i<$count; $i++)
131+
{
132+
if (!isset($actualEvents[$i]))
133+
{
134+
$this->fail(
135+
sprintf(self::MSG_EVENT_GET_ASSERTION_FAILED, $i, $expectedEvents[$i]->name(), 'null')
136+
);
137+
}
138+
else if (!isset($expectedEvents[$i]))
139+
{
140+
$this->fail(
141+
sprintf(self::MSG_EVENT_GET_ASSERTION_FAILED, $i, 'null', $actualEvents[$i]->name())
142+
);
143+
}
144+
145+
$actualEvent = $actualEvents[$i];
146+
$expectedEvent = $expectedEvents[$i];
147+
148+
$this->assertSame(
149+
$expectedEvent->name(),
150+
$actualEvent->name(),
151+
sprintf(self::MSG_EVENT_NAME_ASSERTION_FAILED, $i)
152+
);
153+
$this->assertSame(
154+
$expectedEvent->data(),
155+
$actualEvent->data(),
156+
sprintf(self::MSG_EVENT_DATA_ASSERTION_FAILED, $i)
157+
);
158+
}
159+
}
160+
}

0 commit comments

Comments
 (0)