Skip to content

Commit

Permalink
Fix setMockContainer?
Browse files Browse the repository at this point in the history
  • Loading branch information
keithbauer committed Oct 24, 2024
1 parent 6bb0c4f commit d3e6162
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 41 deletions.
2 changes: 1 addition & 1 deletion tests/Controller/CheckinRequestControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testCreatesCheckOutModelFromRequest()

$response = $controller->createCheckinRequest();

self::assertInstanceOf('Slim\Http\Response', $response);
self::assertInstanceOf('GuzzleHttp\Psr7\Response', $response);
}

/**
Expand Down
55 changes: 16 additions & 39 deletions tests/Mocks/MockService.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php
namespace NYPL\Services\Test\Mocks;

use Aura\Di\Injection\InjectionFactory;
use Aura\Di\Resolver\Resolver;
use Aura\Di\Resolver\Reflector;
use NYPL\Services\ServiceContainer;
use NYPL\Starter\Service;
use Slim\Http\Environment;
use Slim\Http\Request;
use Slim\Http\Response;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\ServerRequest;

/**
* Class MockService
Expand All @@ -14,53 +16,28 @@
*/
class MockService
{
public static $mockEnvironment;
public static $mockContainer;

/**
* Initialize mock Slim request.
*
* @param array $serverParams
* @param array $data
*/
public static function setMockEnvironment(array $serverParams = [], array $data = [])
{
if (empty($serverParams)) {
$serverParams = [
'REQUEST_METHOD' => 'POST',
'REQUEST_URI' => '/',
];
}

self::$mockEnvironment = Environment::mock($serverParams);

if (!empty($data)) {
$_POST = $data;
}

self::$mockContainer['request'] = Request::createFromEnvironment(self::$mockEnvironment);
self::$mockContainer['response'] = new Response();
}

/**
* Set a concrete Container class to pass to controllers.
*
* TODO create true mock class.
*/
public static function setMockContainer()
{
self::$mockContainer = new Service();
$params = [
'X-NYPL-Identity' =>
'{"token":"blah","identity":{"sub":null,"scope":"openid offline_access api read:hold_request"}}'
$reflector = new Reflector();
$resolver = new Resolver($reflector);
$injectionFactory = new InjectionFactory($resolver);
self::$mockContainer = new ServiceContainer($injectionFactory);

$headers = [
'X-NYPL-Identity' =>
'{"token":"blah","identity":{"sub":null,"scope":"openid offline_access api read:hold_request"}}'
];
$body = '{"itemBarcode": "1234567890123", "owningInstitutionId": "NYPL", "cancelRequestId": "1234567890", "jobId": "991873slx938"}';
self::$mockContainer->set("request", new ServerRequest('POST', '/', $headers, $body));
self::$mockContainer->set("response", new Response(200));

if ($params) {
foreach ($params as $name => $value) {
self::$mockContainer['request']->withAddedHeader($name, $value);
}
}
self::$mockContainer['response'];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Mocks/mock.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ DB_USERNAME=
DB_PASSWORD=
SLACK_TOKEN=
DEFAULT_STREAM=TestStream
USE_JOB_SERVICE=0
USE_JOB_SERVICE=false

0 comments on commit d3e6162

Please sign in to comment.