Skip to content

Commit

Permalink
Create basic Unit Test for modRestService
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaLuckers committed Feb 7, 2023
1 parent d2bf8ba commit c1f13ea
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
95 changes: 95 additions & 0 deletions _build/test/Tests/Model/Rest/modRestServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

/*
* This file is part of the MODX Revolution package.
*
* Copyright (c) MODX, LLC
*
* For complete copyright and license information, see the COPYRIGHT and LICENSE
* files found in the top-level directory of this distribution.
*
* @package modx-test
*/

namespace MODX\Revolution\Tests\Model\Rest;

use MODX\Revolution\MODxTestCase;
use MODX\Revolution\Rest\modRestService;
use MODX\Revolution\Rest\modRestServiceRequest;

/**
* Tests related to the modRestService class.
*
* @package modx-test
* @subpackage modx
* @group Model
* @group Rest
* @group modRestService
*/
/**
* @runTestsInSeparateProcesses
*/
final class modRestServiceTest extends MODxTestCase
{
public function testPrepare()
{
$modRestService = new modRestService($this->modx);
$modRestService->prepare();

$this->assertInstanceOf(modRestServiceRequest::class, $modRestService->request);
}

public function testProcessWithUnfindableController()
{
$modRestService = new modRestService($this->modx, [
'exitOnResponse' => false,
'defaultAction' => 'unfindable',
]);
$modRestService->prepare();

$this->expectOutputString('{"success":false,"message":"Method not allowed","object":[],"code":405}');
$modRestService->process();
}

public function testProcessWithUninstantiableController()
{
$modRestService = new modRestService($this->modx, [
'exitOnResponse' => false,
'defaultAction' => 'uninstantiable',
'basePath' => MODX_BASE_PATH . '_build/test/data/rest/Controllers/',
'controllerClassPrefix' => 'modRestServiceTest'
]);
$modRestService->prepare();

$this->expectOutputString('{"success":false,"message":"Bad Request","object":[],"code":400}');
$modRestService->process();
}

public function testProcessWithUnsupportedHTTPMethod()
{
$modRestService = new modRestService($this->modx, [
'exitOnResponse' => false,
'basePath' => MODX_BASE_PATH . '_build/test/data/rest/Controllers/',
'controllerClassPrefix' => 'modRestServiceTest'
]);
$modRestService->prepare();
$modRestService->request->setMethod('FOO');

$this->expectOutputString('{"success":false,"message":"Unsupported HTTP method FOO","object":[],"code":405}');
$modRestService->process();
}

public function testProcess()
{
$modRestService = new modRestService($this->modx, [
'exitOnResponse' => false,
'basePath' => MODX_BASE_PATH . '_build/test/data/rest/Controllers/',
'controllerClassPrefix' => 'modRestServiceTest'
]);
$modRestService->prepare();
$modRestService->request->setMethod('GET');

$this->expectOutputRegex('/{"results":\[\],"total":[0-9]+}/');
$modRestService->process();
}
}
20 changes: 20 additions & 0 deletions _build/test/data/rest/Controllers/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the MODX Revolution package.
*
* Copyright (c) MODX, LLC
*
* For complete copyright and license information, see the COPYRIGHT and LICENSE
* files found in the top-level directory of this distribution.
*
* @package modx-test
* @subpackage data
*/

use MODX\Revolution\Rest\modRestController;

class modRestServiceTestIndex extends modRestController
{
public $classKey = MODX\Revolution\modResource::class;
}
22 changes: 22 additions & 0 deletions _build/test/data/rest/Controllers/Uninstantiable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the MODX Revolution package.
*
* Copyright (c) MODX, LLC
*
* For complete copyright and license information, see the COPYRIGHT and LICENSE
* files found in the top-level directory of this distribution.
*
* @package modx-test
* @subpackage data
*/

use MODX\Revolution\Rest\modRestController;

class modRestServiceTestUninstantiable extends modRestController
{
private function __construct()
{
}
}
1 change: 1 addition & 0 deletions _build/test/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<directory>Tests/Model/Registry</directory>
<directory>Tests/Model/Request</directory>
<directory>Tests/Model/Resource</directory>
<directory>Tests/Model/Rest</directory>
<directory>Tests/Model/Security</directory>
<directory>Tests/Model/Sources</directory>
<directory>Tests/Model/Transport</directory>
Expand Down

0 comments on commit c1f13ea

Please sign in to comment.