Skip to content

Commit 17e88e7

Browse files
committed
add php tests
1 parent 20c2121 commit 17e88e7

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

run-tests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
phpunit --bootstrap shell_lib.php tests/*

shell_lib.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static function php()
6767
*/
6868
class LogHelper
6969
{
70-
static $log_file;
70+
public static $log_file;
7171

7272
public static function init($log_file)
7373
{
@@ -119,7 +119,7 @@ class DeployApplication
119119
/** @var boolean */
120120
private $hasAccess;
121121

122-
function __construct($securityKey, $project_root = '.', $log_file = 'git-deploy-log.txt')
122+
public function __construct($securityKey, $project_root = '.', $log_file = 'git-deploy-log.txt')
123123
{
124124
$this->securityKey = $securityKey;
125125
$this->log_file = getcwd() . '/' . $log_file;
@@ -184,7 +184,9 @@ private function checkSecurity()
184184
LogHelper::log('ACCESS IS OBTAINED');
185185
$this->hasAccess = true;
186186
} else {
187-
LogHelper::log('DENY << ://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
187+
LogHelper::log(
188+
'DENY << ://' . ($_SERVER['HTTP_HOST'] ?? 'unknown-domain') . ($_SERVER['REQUEST_URI'] ?? '')
189+
);
188190
}
189191
}
190192
}

tests/DeployApplicationTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use optimistex\deploy\DeployApplication;
5+
6+
class DeployApplicationTest extends TestCase
7+
{
8+
private $fileName = 'git-deploy-log.txt';
9+
10+
public function setUp()
11+
{
12+
parent::setUp();
13+
@unlink($this->fileName);
14+
}
15+
16+
public function tearDown()
17+
{
18+
@unlink($this->fileName);
19+
parent::tearDown();
20+
}
21+
22+
public function testKeyIsEmpty()
23+
{
24+
$app = new DeployApplication('123', '.', $this->fileName);
25+
26+
$this->assertFileNotExists($this->fileName);
27+
$app->run(['echo 111']);
28+
29+
$this->assertFileNotExists($this->fileName);
30+
$app->begin();
31+
$app->execute(['echo 111']);
32+
$app->end();
33+
$this->assertFileNotExists($this->fileName);
34+
}
35+
36+
public function testKeyInvalid()
37+
{
38+
$_GET['key'] = 'sdfg';
39+
$_SERVER['HTTP_HOST'] = 'test.domain';
40+
$app = new DeployApplication('123', '.', $this->fileName);
41+
42+
$this->assertFileNotExists($this->fileName);
43+
$app->execute(['echo testing_echo']);
44+
$this->assertFileExists($this->fileName);
45+
$log = file_get_contents($this->fileName);
46+
$this->assertRegExp('/====+.+DENY.+test.domain/s', $log);
47+
}
48+
49+
public function testKeyValid1()
50+
{
51+
$_GET['key'] = '123';
52+
$_SERVER['HTTP_HOST'] = 'test.domain';
53+
$app = new DeployApplication('123', '.', $this->fileName);
54+
55+
$this->assertFileNotExists($this->fileName);
56+
$app->execute();
57+
$this->assertFileExists($this->fileName);
58+
$log = file_get_contents($this->fileName);
59+
$this->assertRegExp('/.+ACCESS IS OBTAINED.+Executing shell commands.+\$ git branch.+\$ git pull/si', $log);
60+
}
61+
62+
public function testKeyValid2()
63+
{
64+
$_GET['key'] = '123';
65+
$_SERVER['HTTP_HOST'] = 'test.domain';
66+
$app = new DeployApplication('123', '.', $this->fileName);
67+
68+
$this->assertFileNotExists($this->fileName);
69+
$app->execute(['echo testing_echo']);
70+
$this->assertFileExists($this->fileName);
71+
$log = file_get_contents($this->fileName);
72+
$this->assertRegExp('/.+ACCESS IS OBTAINED.+Executing shell commands.+\$ echo testing_echo.+testing_echo/si', $log);
73+
}
74+
}

0 commit comments

Comments
 (0)