aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/AppFramework/Utility
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/AppFramework/Utility')
-rw-r--r--tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php84
-rw-r--r--tests/lib/AppFramework/Utility/SimpleContainerTest.php143
-rw-r--r--tests/lib/AppFramework/Utility/TimeFactoryTest.php50
3 files changed, 189 insertions, 88 deletions
diff --git a/tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php b/tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php
index 5452fb853b9..00ae4792824 100644
--- a/tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php
+++ b/tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php
@@ -1,24 +1,9 @@
<?php
/**
- * ownCloud - App Framework
- *
- * @author Bernhard Posselt
- * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
- *
- * You should have received a copy of the GNU Affero General Public
- * License along with this library. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\AppFramework\Utility;
@@ -54,6 +39,16 @@ class MiddleController extends BaseController {
public function test3() {
}
+
+ /**
+ * @psalm-param int<-4, 42> $rangedOne
+ * @psalm-param int<min, max> $rangedTwo
+ * @psalm-param int<1, 6>|null $rangedThree
+ * @psalm-param ?int<-70, -30> $rangedFour
+ * @return void
+ */
+ public function test4(int $rangedOne, int $rangedTwo, ?int $rangedThree, ?int $rangedFour) {
+ }
}
class EndController extends MiddleController {
@@ -63,7 +58,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase {
/**
* @Annotation
*/
- public function testReadAnnotation() {
+ public function testReadAnnotation(): void {
$reader = new ControllerMethodReflector();
$reader->reflect(
'\Test\AppFramework\Utility\ControllerMethodReflectorTest',
@@ -76,10 +71,10 @@ class ControllerMethodReflectorTest extends \Test\TestCase {
/**
* @Annotation(parameter=value)
*/
- public function testGetAnnotationParameterSingle() {
+ public function testGetAnnotationParameterSingle(): void {
$reader = new ControllerMethodReflector();
$reader->reflect(
- __CLASS__,
+ self::class,
__FUNCTION__
);
@@ -89,10 +84,10 @@ class ControllerMethodReflectorTest extends \Test\TestCase {
/**
* @Annotation(parameter1=value1, parameter2=value2,parameter3=value3)
*/
- public function testGetAnnotationParameterMultiple() {
+ public function testGetAnnotationParameterMultiple(): void {
$reader = new ControllerMethodReflector();
$reader->reflect(
- __CLASS__,
+ self::class,
__FUNCTION__
);
@@ -105,7 +100,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase {
* @Annotation
* @param test
*/
- public function testReadAnnotationNoLowercase() {
+ public function testReadAnnotationNoLowercase(): void {
$reader = new ControllerMethodReflector();
$reader->reflect(
'\Test\AppFramework\Utility\ControllerMethodReflectorTest',
@@ -121,7 +116,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase {
* @Annotation
* @param int $test
*/
- public function testReadTypeIntAnnotations() {
+ public function testReadTypeIntAnnotations(): void {
$reader = new ControllerMethodReflector();
$reader->reflect(
'\Test\AppFramework\Utility\ControllerMethodReflectorTest',
@@ -142,7 +137,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase {
/**
* @requires PHP 7
*/
- public function testReadTypeIntAnnotationsScalarTypes() {
+ public function testReadTypeIntAnnotationsScalarTypes(): void {
$reader = new ControllerMethodReflector();
$reader->reflect(
'\Test\AppFramework\Utility\ControllerMethodReflectorTest',
@@ -160,7 +155,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase {
* @Annotation
* @param double $test something special
*/
- public function testReadTypeDoubleAnnotations() {
+ public function testReadTypeDoubleAnnotations(): void {
$reader = new ControllerMethodReflector();
$reader->reflect(
'\Test\AppFramework\Utility\ControllerMethodReflectorTest',
@@ -172,9 +167,9 @@ class ControllerMethodReflectorTest extends \Test\TestCase {
/**
* @Annotation
- * @param string $foo
+ * @param string $foo
*/
- public function testReadTypeWhitespaceAnnotations() {
+ public function testReadTypeWhitespaceAnnotations(): void {
$reader = new ControllerMethodReflector();
$reader->reflect(
'\Test\AppFramework\Utility\ControllerMethodReflectorTest',
@@ -187,7 +182,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase {
public function arguments($arg, $arg2 = 'hi') {
}
- public function testReflectParameters() {
+ public function testReflectParameters(): void {
$reader = new ControllerMethodReflector();
$reader->reflect(
'\Test\AppFramework\Utility\ControllerMethodReflectorTest',
@@ -200,7 +195,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase {
public function arguments2($arg) {
}
- public function testReflectParameters2() {
+ public function testReflectParameters2(): void {
$reader = new ControllerMethodReflector();
$reader->reflect(
'\Test\AppFramework\Utility\ControllerMethodReflectorTest',
@@ -211,7 +206,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase {
}
- public function testInheritance() {
+ public function testInheritance(): void {
$reader = new ControllerMethodReflector();
$reader->reflect('Test\AppFramework\Utility\EndController', 'test');
@@ -219,7 +214,7 @@ class ControllerMethodReflectorTest extends \Test\TestCase {
}
- public function testInheritanceOverride() {
+ public function testInheritanceOverride(): void {
$reader = new ControllerMethodReflector();
$reader->reflect('Test\AppFramework\Utility\EndController', 'test2');
@@ -228,10 +223,31 @@ class ControllerMethodReflectorTest extends \Test\TestCase {
}
- public function testInheritanceOverrideNoDocblock() {
+ public function testInheritanceOverrideNoDocblock(): void {
$reader = new ControllerMethodReflector();
$reader->reflect('Test\AppFramework\Utility\EndController', 'test3');
$this->assertFalse($reader->hasAnnotation('Annotation'));
}
+
+ public function testRangeDetection(): void {
+ $reader = new ControllerMethodReflector();
+ $reader->reflect('Test\AppFramework\Utility\EndController', 'test4');
+
+ $rangeInfo1 = $reader->getRange('rangedOne');
+ $this->assertSame(-4, $rangeInfo1['min']);
+ $this->assertSame(42, $rangeInfo1['max']);
+
+ $rangeInfo2 = $reader->getRange('rangedTwo');
+ $this->assertSame(PHP_INT_MIN, $rangeInfo2['min']);
+ $this->assertSame(PHP_INT_MAX, $rangeInfo2['max']);
+
+ $rangeInfo3 = $reader->getRange('rangedThree');
+ $this->assertSame(1, $rangeInfo3['min']);
+ $this->assertSame(6, $rangeInfo3['max']);
+
+ $rangeInfo3 = $reader->getRange('rangedFour');
+ $this->assertSame(-70, $rangeInfo3['min']);
+ $this->assertSame(-30, $rangeInfo3['max']);
+ }
}
diff --git a/tests/lib/AppFramework/Utility/SimpleContainerTest.php b/tests/lib/AppFramework/Utility/SimpleContainerTest.php
index 8caaf517f5c..33800c7376f 100644
--- a/tests/lib/AppFramework/Utility/SimpleContainerTest.php
+++ b/tests/lib/AppFramework/Utility/SimpleContainerTest.php
@@ -3,29 +3,16 @@
declare(strict_types=1);
/**
- * ownCloud - App Framework
- *
- * @author Bernhard Posselt
- * @copyright 2014 Bernhard Posselt <dev@bernhard-posselt.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
- *
- * You should have received a copy of the GNU Affero General Public
- * License along with this library. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\AppFramework\Utility;
use OC\AppFramework\Utility\SimpleContainer;
+use OCP\AppFramework\QueryException;
+use Psr\Container\NotFoundExceptionInterface;
interface TestInterface {
}
@@ -34,29 +21,40 @@ class ClassEmptyConstructor implements IInterfaceConstructor {
}
class ClassSimpleConstructor implements IInterfaceConstructor {
- public $test;
- public function __construct($test) {
- $this->test = $test;
+ public function __construct(
+ public $test,
+ ) {
}
}
class ClassComplexConstructor {
- public $class;
- public $test;
- public function __construct(ClassSimpleConstructor $class, $test) {
- $this->class = $class;
- $this->test = $test;
+ public function __construct(
+ public ClassSimpleConstructor $class,
+ public $test,
+ ) {
+ }
+}
+
+class ClassNullableUntypedConstructorArg {
+ public function __construct(
+ public $class,
+ ) {
+ }
+}
+class ClassNullableTypedConstructorArg {
+ public function __construct(
+ public ?\Some\Class $class,
+ ) {
}
}
interface IInterfaceConstructor {
}
class ClassInterfaceConstructor {
- public $class;
- public $test;
- public function __construct(IInterfaceConstructor $class, $test) {
- $this->class = $class;
- $this->test = $test;
+ public function __construct(
+ public IInterfaceConstructor $class,
+ public $test,
+ ) {
}
}
@@ -70,41 +68,61 @@ class SimpleContainerTest extends \Test\TestCase {
- public function testRegister() {
+ public function testRegister(): void {
$this->container->registerParameter('test', 'abc');
$this->assertEquals('abc', $this->container->query('test'));
}
+ /**
+ * Test querying a class that is not registered without autoload enabled
+ */
+ public function testNothingRegistered(): void {
+ try {
+ $this->container->query('something really hard', false);
+ $this->fail('Expected `QueryException` exception was not thrown');
+ } catch (\Throwable $exception) {
+ $this->assertInstanceOf(QueryException::class, $exception);
+ $this->assertInstanceOf(NotFoundExceptionInterface::class, $exception);
+ }
+ }
- public function testNothingRegistered() {
- $this->expectException(\OCP\AppFramework\QueryException::class);
- $this->container->query('something really hard');
+ /**
+ * Test querying a class that is not registered with autoload enabled
+ */
+ public function testNothingRegistered_autoload(): void {
+ try {
+ $this->container->query('something really hard');
+ $this->fail('Expected `QueryException` exception was not thrown');
+ } catch (\Throwable $exception) {
+ $this->assertInstanceOf(QueryException::class, $exception);
+ $this->assertInstanceOf(NotFoundExceptionInterface::class, $exception);
+ }
}
- public function testNotAClass() {
- $this->expectException(\OCP\AppFramework\QueryException::class);
+ public function testNotAClass(): void {
+ $this->expectException(QueryException::class);
$this->container->query('Test\AppFramework\Utility\TestInterface');
}
- public function testNoConstructorClass() {
+ public function testNoConstructorClass(): void {
$object = $this->container->query('Test\AppFramework\Utility\ClassEmptyConstructor');
$this->assertTrue($object instanceof ClassEmptyConstructor);
}
- public function testInstancesOnlyOnce() {
+ public function testInstancesOnlyOnce(): void {
$object = $this->container->query('Test\AppFramework\Utility\ClassEmptyConstructor');
$object2 = $this->container->query('Test\AppFramework\Utility\ClassEmptyConstructor');
$this->assertSame($object, $object2);
}
- public function testConstructorSimple() {
+ public function testConstructorSimple(): void {
$this->container->registerParameter('test', 'abc');
$object = $this->container->query(
'Test\AppFramework\Utility\ClassSimpleConstructor'
@@ -114,7 +132,7 @@ class SimpleContainerTest extends \Test\TestCase {
}
- public function testConstructorComplex() {
+ public function testConstructorComplex(): void {
$this->container->registerParameter('test', 'abc');
$object = $this->container->query(
'Test\AppFramework\Utility\ClassComplexConstructor'
@@ -125,7 +143,7 @@ class SimpleContainerTest extends \Test\TestCase {
}
- public function testConstructorComplexInterface() {
+ public function testConstructorComplexInterface(): void {
$this->container->registerParameter('test', 'abc');
$this->container->registerService(
'Test\AppFramework\Utility\IInterfaceConstructor', function ($c) {
@@ -140,7 +158,7 @@ class SimpleContainerTest extends \Test\TestCase {
}
- public function testOverrideService() {
+ public function testOverrideService(): void {
$this->container->registerService(
'Test\AppFramework\Utility\IInterfaceConstructor', function ($c) {
return $c->query('Test\AppFramework\Utility\ClassSimpleConstructor');
@@ -155,13 +173,13 @@ class SimpleContainerTest extends \Test\TestCase {
$this->assertTrue($object instanceof ClassEmptyConstructor);
}
- public function testRegisterAliasParamter() {
+ public function testRegisterAliasParamter(): void {
$this->container->registerParameter('test', 'abc');
$this->container->registerAlias('test1', 'test');
$this->assertEquals('abc', $this->container->query('test1'));
}
- public function testRegisterAliasService() {
+ public function testRegisterAliasService(): void {
$this->container->registerService('test', function () {
return new \StdClass;
}, true);
@@ -174,7 +192,7 @@ class SimpleContainerTest extends \Test\TestCase {
$this->container->query('test'), $this->container->query('test1'));
}
- public function sanitizeNameProvider() {
+ public static function sanitizeNameProvider(): array {
return [
['ABC\\Foo', 'ABC\\Foo'],
['\\ABC\\Foo', '\\ABC\\Foo'],
@@ -183,10 +201,8 @@ class SimpleContainerTest extends \Test\TestCase {
];
}
- /**
- * @dataProvider sanitizeNameProvider
- */
- public function testSanitizeName($register, $query) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('sanitizeNameProvider')]
+ public function testSanitizeName($register, $query): void {
$this->container->registerService($register, function () {
return 'abc';
});
@@ -194,15 +210,17 @@ class SimpleContainerTest extends \Test\TestCase {
}
- public function testConstructorComplexNoTestParameterFound() {
- $this->expectException(\OCP\AppFramework\QueryException::class);
+ public function testConstructorComplexNoTestParameterFound(): void {
+ $this->expectException(QueryException::class);
$object = $this->container->query(
'Test\AppFramework\Utility\ClassComplexConstructor'
);
+ /* Use the object to trigger DI on PHP >= 8.4 */
+ get_object_vars($object);
}
- public function testRegisterFactory() {
+ public function testRegisterFactory(): void {
$this->container->registerService('test', function () {
return new \StdClass();
}, false);
@@ -210,7 +228,7 @@ class SimpleContainerTest extends \Test\TestCase {
$this->container->query('test'), $this->container->query('test'));
}
- public function testRegisterAliasFactory() {
+ public function testRegisterAliasFactory(): void {
$this->container->registerService('test', function () {
return new \StdClass();
}, false);
@@ -222,4 +240,21 @@ class SimpleContainerTest extends \Test\TestCase {
$this->assertNotSame(
$this->container->query('test'), $this->container->query('test1'));
}
+
+ public function testQueryUntypedNullable(): void {
+ $this->expectException(QueryException::class);
+
+ $object = $this->container->query(
+ ClassNullableUntypedConstructorArg::class
+ );
+ /* Use the object to trigger DI on PHP >= 8.4 */
+ get_object_vars($object);
+ }
+
+ public function testQueryTypedNullable(): void {
+ /** @var ClassNullableTypedConstructorArg $service */
+ $service = $this->container->query(ClassNullableTypedConstructorArg::class);
+
+ self::assertNull($service->class);
+ }
}
diff --git a/tests/lib/AppFramework/Utility/TimeFactoryTest.php b/tests/lib/AppFramework/Utility/TimeFactoryTest.php
new file mode 100644
index 00000000000..276b2d6da4f
--- /dev/null
+++ b/tests/lib/AppFramework/Utility/TimeFactoryTest.php
@@ -0,0 +1,50 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+namespace Test\AppFramework\Utility;
+
+use OC\AppFramework\Utility\TimeFactory;
+
+class TimeFactoryTest extends \Test\TestCase {
+ protected TimeFactory $timeFactory;
+
+ protected function setUp(): void {
+ $this->timeFactory = new TimeFactory();
+ }
+
+ public function testNow(): void {
+ $now = $this->timeFactory->now();
+ self::assertSame('UTC', $now->getTimezone()->getName());
+ }
+
+ public function testNowWithTimeZone(): void {
+ $timezone = new \DateTimeZone('Europe/Berlin');
+ $withTimeZone = $this->timeFactory->withTimeZone($timezone);
+
+ $now = $withTimeZone->now();
+ self::assertSame('Europe/Berlin', $now->getTimezone()->getName());
+ }
+
+ public function testGetTimeZone(): void {
+ $expected = new \DateTimeZone('Europe/Berlin');
+ $actual = $this->timeFactory->getTimeZone('Europe/Berlin');
+ self::assertEquals($expected, $actual);
+ }
+
+ public function testGetTimeZoneUTC(): void {
+ $expected = new \DateTimeZone('UTC');
+ $actual = $this->timeFactory->getTimeZone();
+ self::assertEquals($expected, $actual);
+ }
+
+ public function testGetTimeZoneInvalid(): void {
+ $this->expectException(\Exception::class);
+ $this->timeFactory->getTimeZone('blubblub');
+ }
+}