chore: Adapt tests to OC_API refactoring

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
pull/47685/head
Côme Chilliet 2 years ago committed by Côme Chilliet
parent e184784f86
commit 359bbce3af
  1. 84
      tests/lib/APITest.php
  2. 2
      tests/lib/AppFramework/OCS/BaseResponseTest.php
  3. 38
      tests/lib/AppFramework/OCS/V2ResponseTest.php
  4. 54
      tests/lib/OCS/ApiHelperTest.php
  5. 29
      tests/lib/OCS/MapStatusCodeTest.php

@ -1,84 +0,0 @@
<?php
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test;
use OCP\IRequest;
class APITest extends \Test\TestCase {
// Helps build a response variable
/**
* @param string $message
*/
public function buildResponse($shipped, $data, $code, $message = null) {
$resp = new \OC\OCS\Result($data, $code, $message);
$resp->addHeader('KEY', 'VALUE');
return [
'shipped' => $shipped,
'response' => $resp,
'app' => $this->getUniqueID('testapp_'),
];
}
// Validate details of the result
/**
* @param \OC\OCS\Result $result
*/
public function checkResult($result, $success) {
// Check response is of correct type
$this->assertInstanceOf(\OC\OCS\Result::class, $result);
// Check if it succeeded
/** @var \OC\OCS\Result $result */
$this->assertEquals($success, $result->succeeded());
}
/**
* @return array
*/
public function versionDataScriptNameProvider() {
return [
// Valid script name
[
'/master/ocs/v2.php',
true,
],
// Invalid script names
[
'/master/ocs/v2.php/someInvalidPathName',
false,
],
[
'/master/ocs/v1.php',
false,
],
[
'',
false,
],
];
}
/**
* @dataProvider versionDataScriptNameProvider
* @param string $scriptName
* @param bool $expected
*/
public function testIsV2($scriptName, $expected) {
$request = $this->getMockBuilder(IRequest::class)
->disableOriginalConstructor()
->getMock();
$request
->expects($this->once())
->method('getScriptName')
->willReturn($scriptName);
$this->assertEquals($expected, $this->invokePrivate(new \OC_API, 'isV2', [$request]));
}
}

@ -7,7 +7,7 @@ declare(strict_types=1);
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\AppFramework\Middleware;
namespace Test\AppFramework\OCS;
use OC\AppFramework\OCS\BaseResponse;

@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\AppFramework\OCS;
use OC\AppFramework\OCS\V2Response;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
class V2ResponseTest extends \Test\TestCase {
/**
* @dataProvider providesStatusCodes
*/
public function testStatusCodeMapper(int $expected, int $sc): void {
$response = new V2Response(new DataResponse([], $sc));
$this->assertEquals($expected, $response->getStatus());
}
public function providesStatusCodes(): array {
return [
[Http::STATUS_OK, 200],
[Http::STATUS_BAD_REQUEST, 104],
[Http::STATUS_BAD_REQUEST, 1000],
[201, 201],
[Http::STATUS_UNAUTHORIZED, OCSController::RESPOND_UNAUTHORISED],
[Http::STATUS_INTERNAL_SERVER_ERROR, OCSController::RESPOND_SERVER_ERROR],
[Http::STATUS_NOT_FOUND, OCSController::RESPOND_NOT_FOUND],
[Http::STATUS_INTERNAL_SERVER_ERROR, OCSController::RESPOND_UNKNOWN_ERROR],
];
}
}

@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\OCS;
use OC\OCS\ApiHelper;
use OCP\IRequest;
class ApiHelperTest extends \Test\TestCase {
/**
* @return array
*/
public function versionDataScriptNameProvider(): array {
return [
// Valid script name
[
'/master/ocs/v2.php', true,
],
// Invalid script names
[
'/master/ocs/v2.php/someInvalidPathName', false,
],
[
'/master/ocs/v1.php', false,
],
[
'', false,
],
];
}
/**
* @dataProvider versionDataScriptNameProvider
*/
public function testIsV2(string $scriptName, bool $expected): void {
$request = $this->getMockBuilder(IRequest::class)
->disableOriginalConstructor()
->getMock();
$request
->expects($this->once())
->method('getScriptName')
->willReturn($scriptName);
$this->assertEquals($expected, $this->invokePrivate(new ApiHelper, 'isV2', [$request]));
}
}

@ -1,29 +0,0 @@
<?php
/**
* SPDX-FileCopyrightText: 2023-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace Test\OCS;
use OCP\AppFramework\Http;
class MapStatusCodeTest extends \Test\TestCase {
/**
* @dataProvider providesStatusCodes
*/
public function testStatusCodeMapper($expected, $sc) {
$result = \OC_API::mapStatusCodes($sc);
$this->assertEquals($expected, $result);
}
public function providesStatusCodes() {
return [
[Http::STATUS_OK, 100],
[Http::STATUS_BAD_REQUEST, 104],
[Http::STATUS_BAD_REQUEST, 1000],
[201, 201],
];
}
}
Loading…
Cancel
Save