Skip to content

Http mock fails with new symfony/http-foundation - 4x #46

@kordos

Description

@kordos

It fails with new http foundation. In my case it is 4.0.8
From yesterday my API tests started to failing after updating dependencies from silex 2.2.4 to 2.3.0
which installed new symfony components in 4.x version
Update info:

Package operations: 0 installs, 5 updates, 1 removal

  • Removing symfony/polyfill-php70 (v1.7.0)
  • Updating symfony/routing (v3.4.8 => v4.0.8): Loading from cache
  • Updating symfony/http-foundation (v3.4.8 => v4.0.8): Loading from cache
  • Updating symfony/event-dispatcher (v3.4.8 => v4.0.8): Loading from cache
  • Updating symfony/http-kernel (v3.4.8 => v4.0.8): Loading from cache
  • Updating silex/silex (v2.2.4 => v2.3.0): Loading from cache

In tests I get error:

Client error: GET http://localhost:8082/v1/countries/de.json resulted in a 404 Not Found
with content which I set in body: { name: Proxy-Germany, code: de }

Test code:


$I->getHttpMock()->mock
	->when()
		->methodIs('GET')
		->pathIs('/v1/countries/de.json')
	->then()
		->header('content-type', 'application/json')
		->body('{ "name": "Proxy-Germany", "code": "de" }')
	->end();
$I->getHttpMock()->setUp();

$countryCode = "de";

$I->sendGET('/v1/countries/'. $countryCode . '-proxy.json');
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContainsJson(['code' => $countryCode, 'name' => 'Proxy-Germany']);  

After many hours of investigation I noticed that I can fix it by replacing body method with callback:

$I->getHttpMock()->mock
	->when()
		->methodIs('GET')
		->pathIs('/v1/countries/de.json')
	->then()
		->header('content-type', 'application/json')
		->callback(
			function (Response $response) {
				$response->setContent('{ "name": "Proxy-Germany", "code": "de" }')
					->setStatusCode(200);
			}
		)
	->end();
$I->getHttpMock()->setUp();

$countryCode = "de";

$I->sendGET('/v1/countries/'. $countryCode . '-proxy.json');
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContainsJson(['code' => $countryCode, 'name' => 'Proxy-Germany']);

I guessing that http mock library uses some old code that isn't present in 4.x versions.

Please fix issue.
Regards,
Pawel

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions