Skip to content

Commit 836d5d5

Browse files
committed
Initial version with http mock running
0 parents  commit 836d5d5

24 files changed

+3159
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.buildpath
2+
.project
3+
.settings

composer.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"keywords" : [
3+
"http",
4+
"mock",
5+
"http mock",
6+
"codeception",
7+
"extension"
8+
],
9+
"authors" : [{
10+
"name" : "Mariano Custiel",
11+
"email" : "jmcustiel@gmail.com",
12+
"homepage" : "https://github.com/mcustiel",
13+
"role" : "Developer"
14+
}
15+
],
16+
"autoload" : {
17+
"psr-4" : {
18+
"Codeception\\" : "src"
19+
}
20+
},
21+
"name" : "mcustiel/codeception-http-mock",
22+
"type" : "library",
23+
"description" : "Extension for HTTP Mock.",
24+
"license" : "GPL-3.0+",
25+
"require" : {
26+
"php" : ">=5.4",
27+
"codeception/codeception" : "~2.1.0",
28+
"internations/http-mock" : "dev-master",
29+
"mcustiel/php-simple-di" : ">=1.2.0"
30+
},
31+
"require-dev" : {
32+
"phing/phing" : ">=2.12.0",
33+
"squizlabs/php_codesniffer" : ">=2.3.4",
34+
"phpmd/phpmd" : ">=2.3.2",
35+
"sebastian/phpcpd" : ">=2.0.2",
36+
"pdepend/pdepend" : ">=2.0.6",
37+
"phploc/phploc" : ">=2.1.1",
38+
"phpdocumentor/phpdocumentor" : ">=2.8.5"
39+
}
40+
}

src/Extension/HttpMock.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
namespace Codeception\Extension;
3+
4+
use Codeception\Platform\Extension as CodeceptionExtension;
5+
use Codeception\Extension;
6+
use InterNations\Component\HttpMock\Matcher\ExtractorFactory;
7+
use InterNations\Component\HttpMock\Server;
8+
use InterNations\Component\HttpMock\MockBuilder;
9+
use InterNations\Component\HttpMock\Matcher\MatcherFactory;
10+
use Mcustiel\DependencyInjection\DependencyInjectionService;
11+
12+
class HttpMock extends CodeceptionExtension
13+
{
14+
/**
15+
* @var array
16+
*/
17+
private $defaults = [
18+
'port' => '28080',
19+
'host' => 'localhost'
20+
];
21+
/**
22+
* @var \InterNations\Component\HttpMock\Server
23+
*/
24+
private $server;
25+
/**
26+
* @var \Mcustiel\DependencyInjection\DependencyInjectionService
27+
*/
28+
private $diManager;
29+
30+
public function __construct(array $config, array $options)
31+
{
32+
parent::__construct(array_merge($this->defaults, $config), $options);
33+
$this->diManager = new DependencyInjectionService();
34+
$this->startHttpMock();
35+
}
36+
37+
public function __destruct()
38+
{
39+
$this->stopHttpMock();
40+
}
41+
42+
private function startHttpMock()
43+
{
44+
echo "Starting http mock server on {$this->config['host']}:{$this->config['port']}" . PHP_EOL;
45+
$this->server = new Server($this->config['port'], $this->config['host']);
46+
$this->server->start();
47+
$this->setUpDependencyInjection();
48+
}
49+
50+
private function setUpDependencyInjection()
51+
{
52+
$this->diManager->register(
53+
'httpMockServer',
54+
function () {
55+
return $this->server;
56+
}
57+
);
58+
$this->diManager->register(
59+
'httpMockBuilder',
60+
function () {
61+
return new MockBuilder(new MatcherFactory(), new ExtractorFactory());
62+
}
63+
);
64+
}
65+
66+
private function stopHttpMock()
67+
{
68+
echo 'Stoping http mock server' . PHP_EOL;
69+
$this->server->stop();
70+
}
71+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
namespace Codeception\Extension;
3+
4+
use InterNations\Component\HttpMock\Server;
5+
6+
class HttpMockConnection
7+
{
8+
/**
9+
* @var \InterNations\Component\HttpMock\Server
10+
*/
11+
private static $connection;
12+
13+
/**
14+
* @param \InterNations\Component\HttpMock\Server $connection
15+
*/
16+
public static function setConnection(Server $connection)
17+
{
18+
self::$connection = $connection;
19+
}
20+
21+
/**
22+
* @return \InterNations\Component\HttpMock\Server
23+
*/
24+
public static function get()
25+
{
26+
return self::$connection;
27+
}
28+
}

src/Module/HttpMock.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
namespace Codeception\Module;
3+
4+
use Codeception\Module as CodeceptionModule;
5+
use Codeception\TestCase;
6+
use Codeception\Extension\HttpMock as HttpMockExtension;
7+
use Codeception\Module;
8+
use Codeception\Lib\ModuleContainer;
9+
use Mcustiel\DependencyInjection\DependencyInjectionService;
10+
11+
class HttpMock extends CodeceptionModule
12+
{
13+
private $diManager;
14+
15+
public function __construct(ModuleContainer $moduleContainer, $config = null)
16+
{
17+
$this->diManager = new DependencyInjectionService();
18+
}
19+
20+
/**
21+
* {@inheritDoc}
22+
* @see \Codeception\Module::_before()
23+
*/
24+
public function _before(TestCase $testCase)
25+
{
26+
parent::_before($testCase);
27+
}
28+
29+
/**
30+
* @return \InterNations\Component\HttpMock\MockBuilder
31+
*/
32+
public function expectRequest()
33+
{
34+
return $this->diManager->get('httpMockBuilder');
35+
}
36+
37+
public function doNotExpectAnyOtherRequest()
38+
{
39+
$this->diManager->get('httpMockServer')->setUp(
40+
$this->diManager->get('httpMockBuilder')->flushExpectations()
41+
);
42+
}
43+
44+
/**
45+
* {@inheritDoc}
46+
* @see \Codeception\Module::_after()
47+
*/
48+
public function _after(TestCase $testCase)
49+
{
50+
parent::_after($testCase);
51+
$error = (string)$this->diManager->get('httpMockServer')->getIncrementalErrorOutput();
52+
if ($error != '') {
53+
throw new \Exception(
54+
'HTTP mock server standard error output should be empty but is: ' .
55+
var_export($error, true)
56+
);
57+
};
58+
}
59+
}

tests/codeception.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
actor: Tester
2+
paths:
3+
tests: tests
4+
log: tests/_output
5+
data: tests/_data
6+
support: tests/_support
7+
envs: tests/_envs
8+
settings:
9+
bootstrap: _bootstrap.php
10+
colors: true
11+
memory_limit: 1024M
12+
extensions:
13+
enabled:
14+
- Codeception\Extension\RunFailed
15+
- Codeception\Extension\HttpMock
16+
config:
17+
Codeception\Extension\HttpMock:
18+
port: 18080
19+
modules:
20+
config:
21+
Db:
22+
dsn: ''
23+
user: ''
24+
password: ''
25+
dump: tests/_data/dump.sql

tests/tests/_bootstrap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
// This is global bootstrap for autoloading

tests/tests/_data/dump.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* Replace this file with actual dump of your database */
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
4+
/**
5+
* Inherited Methods
6+
* @method void wantToTest($text)
7+
* @method void wantTo($text)
8+
* @method void execute($callable)
9+
* @method void expectTo($prediction)
10+
* @method void expect($prediction)
11+
* @method void amGoingTo($argumentation)
12+
* @method void am($role)
13+
* @method void lookForwardTo($achieveValue)
14+
* @method void comment($description)
15+
* @method \Codeception\Lib\Friend haveFriend($name, $actorClass = null)
16+
*
17+
* @SuppressWarnings(PHPMD)
18+
*/
19+
class AcceptanceTester extends \Codeception\Actor
20+
{
21+
use _generated\AcceptanceTesterActions;
22+
23+
/**
24+
* Define custom actions here
25+
*/
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
4+
/**
5+
* Inherited Methods
6+
* @method void wantToTest($text)
7+
* @method void wantTo($text)
8+
* @method void execute($callable)
9+
* @method void expectTo($prediction)
10+
* @method void expect($prediction)
11+
* @method void amGoingTo($argumentation)
12+
* @method void am($role)
13+
* @method void lookForwardTo($achieveValue)
14+
* @method void comment($description)
15+
* @method \Codeception\Lib\Friend haveFriend($name, $actorClass = null)
16+
*
17+
* @SuppressWarnings(PHPMD)
18+
*/
19+
class FunctionalTester extends \Codeception\Actor
20+
{
21+
use _generated\FunctionalTesterActions;
22+
23+
/**
24+
* Define custom actions here
25+
*/
26+
}

0 commit comments

Comments
 (0)