Skip to content

Commit a7effe9

Browse files
committed
Added documentation and fixed Module
1 parent 7d419bc commit a7effe9

File tree

4 files changed

+257
-2
lines changed

4 files changed

+257
-2
lines changed

README.md

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,93 @@
11
# codeception-http-mock
2-
HttpMock extension and module for Codeception.
2+
This Codeception Extension allows developers and testers to use HttpMock to mock external services when running codeception tests.
3+
4+
codeception-http-mock runs an instance of http-mock before your tests run so they can mock external services.
5+
After the tests are finished it will close the connection and turn http-mock off.
6+
7+
## See also
8+
9+
* [http-mock library](https://github.com/InterNations/http-mock)
10+
11+
## Installation
12+
13+
### Composer:
14+
15+
This project is published in packagist, so you just need to add it as a dependency in your composer.json:
16+
17+
```javascript
18+
"require": {
19+
// ...
20+
"mcustiel/codeception-http-mock": "*"
21+
}
22+
```
23+
24+
If you want to access directly to this repo, adding this to your composer.json should be enough:
25+
26+
```javascript
27+
{
28+
"repositories": [
29+
{
30+
"type": "vcs",
31+
"url": "https://github.com/mcustiel/codeception-http-mock"
32+
}
33+
],
34+
"require": {
35+
"mcustiel/codeception-http-mock": "dev-master"
36+
}
37+
}
38+
```
39+
40+
Or just download the release and include it in your path.
41+
42+
## Configuration Example
43+
44+
### Extension
45+
46+
```yaml
47+
# codeception.yml
48+
extensions:
49+
enabled:
50+
- Codeception\Extension\HttpMock
51+
config:
52+
Codeception\Extension\HttpMock:
53+
port: 18080 # defaults to http-mock default port
54+
host: name.for.my.server # defaults to http-mock default host
55+
```
56+
57+
### Module
58+
59+
```yaml
60+
# acceptance.yml
61+
modules:
62+
enabled:
63+
- HttpMock
64+
```
65+
66+
## How to use
67+
68+
### Prepare your application
69+
70+
First of all, configure your application so when it is being tested it will replace its external services with http-mock.
71+
For instance, if you make some requests to a REST service located under http://your.rest.interface, replace that url in configuration with the host yoy set up in http-mock extension configuration.
72+
73+
### Write your tests
74+
75+
```php
76+
// YourCest.php
77+
class YourCest extends \Codeception\TestCase\Test
78+
{
79+
// tests
80+
public function tryToTest(\AcceptanceTester $I)
81+
{
82+
$I->expectRequest()->when()
83+
->methodIs('GET')
84+
->pathIs('/foo')
85+
->then()
86+
->body('mocked body')
87+
->end();
88+
$I->doNotExpectAnyOtherRequest();
89+
$response = file_get_contents('http://localhost:28080/foo');
90+
$I->assertEquals('mocked body', $response);
91+
}
92+
}
93+
```

build.xml

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project name="Phing static code analysis" default="all">
4+
<!-- Properties -->
5+
<property name="dir.base" value="." />
6+
<property name="dir.tests" value="${project.basedir}/tests" />
7+
<property name="dir.tests.unit" value="${project.basedir}/tests" />
8+
<property name="dir.build" value="${project.basedir}/build" />
9+
<property name="dir.docs" value="${dir.build}/docs" />
10+
<property name="dir.docs.phpdoc" value="${dir.docs}/phpdoc" />
11+
<property name="dir.reports" value="${dir.build}/logs" />
12+
<property name="dir.reports.pdepend" value="${dir.reports}/pdepend" />
13+
<property name="dir.reports.unit" value="${dir.reports}/phpunit" />
14+
<property name="dir.reports.coverage" value="${dir.reports}/phpunit/coverage" />
15+
<property name="dir.reports.build" value="${dir.reports}/htmlreport" />
16+
17+
<!-- ============================================ -->
18+
<!-- Fileset: sources (all php files but those in test) -->
19+
<!-- ============================================ -->
20+
<fileset expandsymboliclinks="true" dir="${dir.base}" id="sources">
21+
<include name="src/**/*.php" />
22+
</fileset>
23+
24+
<!-- ============================================ -->
25+
<!-- Target: clean -->
26+
<!-- ============================================ -->
27+
<target name="clean" description="Clean up build directories.">
28+
<echo msg="Cleaning build directories ..." />
29+
<delete dir="${dir.build}" verbose="false" />
30+
</target>
31+
32+
<!-- ============================================ -->
33+
<!-- Target: prepare -->
34+
<!-- ============================================ -->
35+
<target name="prepare" description="Create build directories.">
36+
<echo msg="Creating build directories ..." />
37+
<mkdir dir="${dir.build}" />
38+
<mkdir dir="${dir.docs}" />
39+
<mkdir dir="${dir.docs.phpdoc}" />
40+
<mkdir dir="${dir.reports}" />
41+
<mkdir dir="${dir.reports.unit}" />
42+
<mkdir dir="${dir.reports.coverage}" />
43+
<mkdir dir="${dir.reports.pdepend}" />
44+
<mkdir dir="${dir.reports.build}" />
45+
</target>
46+
47+
<!-- ============================================ -->
48+
<!-- Target: all (default target) -->
49+
<!-- ============================================ -->
50+
<target name="all" depends="clean, prepare">
51+
<phingcall target="codecheck" />
52+
<phingcall target="tests" />
53+
<phingcall target="documentation" />
54+
</target>
55+
56+
<!-- ============================================ -->
57+
<!-- Target: codecheck (run all static code checks) -->
58+
<!-- ============================================ -->
59+
<target name="codecheck">
60+
<phingcall target="lint" />
61+
<phingcall target="codestyle" />
62+
<phingcall target="mess" />
63+
<phingcall target="copypaste" />
64+
<phingcall target="measure" />
65+
</target>
66+
67+
<!-- ============================================ -->
68+
<!-- Target: tests (run all tests) -->
69+
<!-- ============================================ -->
70+
<target name="tests">
71+
<!-- Now we are not running unit tests -->
72+
<!-- <phingcall target="unittests" /> -->
73+
</target>
74+
75+
<!-- ============================================ -->
76+
<!-- Target: lint (Checks code syntax) -->
77+
<!-- ============================================ -->
78+
<target name="lint">
79+
<echo msg="Running lint to check code syntax..." />
80+
<phplint>
81+
<fileset refid="sources" />
82+
</phplint>
83+
</target>
84+
85+
<!-- ============================================ -->
86+
<!-- Target: codestyle (Checks code style compliance) -->
87+
<!-- ============================================ -->
88+
<target name="codestyle">
89+
<echo msg="Running code sniffer to check PSR2 standard..." />
90+
<phpcodesniffer standard="PSR2" showSniffs="true" showWarnings="true" verbosity="0" encoding="UTF-8">
91+
<fileset refid="sources" />
92+
<formatter type="full" outfile="${dir.reports}/reportcs.txt" />
93+
<formatter type="checkstyle" outfile="${dir.reports}/checkstylecs.xml" />
94+
</phpcodesniffer>
95+
</target>
96+
97+
<!-- ============================================ -->
98+
<!-- Target: mess (Detects mess in code. Recommended rulesets: -->
99+
<!-- unusedcode,codesize,controversial,design,naming) -->
100+
<!-- ============================================ -->
101+
<target name="mess">
102+
<echo msg="Running mess detector" />
103+
<phpmd rulesets="unusedcode,codesize,controversial,design,naming">
104+
<fileset refid="sources" />
105+
<formatter type="xml" outfile="${dir.reports}/pmd.xml"/>
106+
</phpmd>
107+
</target>
108+
109+
<!-- ============================================ -->
110+
<!-- Target: copypaste (detects copy/paste in code) -->
111+
<!-- ============================================ -->
112+
<target name="copypaste">
113+
<echo msg="Running copy/paste detector..." />
114+
<phpcpd>
115+
<fileset refid="sources" />
116+
<formatter type="pmd" outfile="${dir.reports}/pmd-cpd.xml" />
117+
</phpcpd>
118+
</target>
119+
120+
<!-- ============================================ -->
121+
<!-- Target: measure (measures the code) -->
122+
<!-- ============================================ -->
123+
<target name="measure">
124+
<echo msg="Running code measurements..." />
125+
<phploc reportType="csv" reportName="phploc" reportDirectory="${dir.reports}">
126+
<fileset refid="sources" />
127+
</phploc>
128+
<phpdepend>
129+
<fileset refid="sources" />
130+
<logger type="jdepend-xml" outfile="${dir.reports}/jdepend.xml"/>
131+
<analyzer type="coderank-mode" value="method"/>
132+
</phpdepend>
133+
</target>
134+
135+
<!-- ============================================ -->
136+
<!-- Target: documentation (PHP Documentor parsing) -->
137+
<!-- ============================================ -->
138+
<target name="documentation">
139+
<phpdoc2 title="Project Documentation" destdir="${dir.docs.phpdoc}" template="responsive-twig">
140+
<fileset refid="sources" />
141+
</phpdoc2>
142+
</target>
143+
144+
<!-- ============================================ -->
145+
<!-- Target: unittests (unit testing) -->
146+
<!-- ============================================ -->
147+
<target name="unittests">
148+
<echo msg="Running unit tests..." />
149+
<coverage-setup database="${dir.reports.unit}/coverage.db">
150+
<fileset refid="sources" />
151+
</coverage-setup>
152+
<phpunit configuration="${dir.tests}/phpunit.xml" codecoverage="true">
153+
<formatter todir="${dir.reports.unit}" type="xml" />
154+
<formatter todir="${dir.reports.unit}" type="clover" />
155+
<batchtest>
156+
<fileset dir="${dir.tests.unit}" />
157+
</batchtest>
158+
</phpunit>
159+
<coverage-report outfile="${dir.reports.unit}/coverage.xml">
160+
<report todir="${dir.reports.coverage}" title="Phing unit tests run" usesorttable="true"/>
161+
</coverage-report>
162+
</target>
163+
</project>

src/Module/HttpMock.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class HttpMock extends CodeceptionModule
1313

1414
public function __construct(ModuleContainer $moduleContainer, $config = null)
1515
{
16+
parent::__construct($moduleContainer, $config);
1617
$this->diManager = new DependencyInjectionService();
1718
}
1819

tests/tests/acceptance/WelcomeCest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function tryToTest(\AcceptanceTester $I)
1919
->body('mocked body')
2020
->end();
2121
$I->doNotExpectAnyOtherRequest();
22-
$response = file_get_contents('http://localhost:28080/foo');
22+
$response = file_get_contents('http://localhost:18080/foo');
2323
$I->assertEquals('mocked body', $response);
2424
}
2525
}

0 commit comments

Comments
 (0)