Skip to content

Commit

Permalink
Add unit test coverage for modRestService (#16381)
Browse files Browse the repository at this point in the history
Merge remote-tracking branch 'upstream/pr/16381' into 3.x

* upstream/pr/16381:
  Disable missing namespace ruleset
  Create basic Unit Test for modRestService
  • Loading branch information
Mark-H committed Feb 10, 2024
2 parents 73ae298 + ee2b7f9 commit c01ca89
Show file tree
Hide file tree
Showing 4 changed files with 140 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();
}
}
21 changes: 21 additions & 0 deletions _build/test/data/rest/Controllers/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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
* @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
*/

use MODX\Revolution\Rest\modRestController;

class modRestServiceTestIndex extends modRestController
{
public $classKey = MODX\Revolution\modResource::class;
}
23 changes: 23 additions & 0 deletions _build/test/data/rest/Controllers/Uninstantiable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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
* @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
*/

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 c01ca89

Please sign in to comment.