Skip to content

Commit

Permalink
Add autoload-dev and remove all require_once in tests
Browse files Browse the repository at this point in the history
All non-namespaced classes in JsonMapperTest are made part of the PSR0 autoloading.
Exception still made for the "Zoo" collection of classes.
  • Loading branch information
SvenRtbg authored and cweiske committed Mar 17, 2024
1 parent dddcdc0 commit 49f1fb2
Show file tree
Hide file tree
Showing 55 changed files with 44 additions and 146 deletions.
11 changes: 11 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
"autoload": {
"psr-0": {"JsonMapper": "src/"}
},
"autoload-dev": {
"psr-4": {
"namespacetest\\": "tests/support/namespacetest",
"othernamespace\\": "tests/support/othernamespace",
"Enums\\": "tests/support/Enums"
},
"psr-0": {
"JsonMapperTest_": "tests/support",
"": ["tests/support/Zoo"]
}
},
"authors": [
{
"name": "Christian Weiske",
Expand Down
10 changes: 0 additions & 10 deletions tests/ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@

use namespacetest\model\MyArrayObject;

require_once 'JsonMapperTest/Array.php';
require_once 'JsonMapperTest/ArrayAccessCollection.php';
require_once 'JsonMapperTest/Broken.php';
require_once 'JsonMapperTest/Simple.php';
require_once 'JsonMapperTest/VariadicArray.php';
require_once 'JsonMapperTest/Zoo/Animal.php';
require_once 'JsonMapperTest/Zoo/Zoo.php';
require_once 'JsonMapperTest/Zoo/Cat.php';
require_once 'JsonMapperTest/Zoo/Fish.php';

/**
* Unit tests for JsonMapper's array handling
*
Expand Down
8 changes: 1 addition & 7 deletions tests/Array_PHP74_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,11 @@
*/
class Array_PHP74_Test extends TestCase
{
protected function setUp(): void
{
require_once 'JsonMapperTest/PHP74_Array.php';
require_once 'JsonMapperTest/ArrayValueForStringProperty.php';
}

public function testJsonMapper()
{
$json = json_decode('{"files": ["test.txt"]}');
$jsonMapper = new \JsonMapper();
$array = $jsonMapper->map($json, new PHP74_Array());
$array = $jsonMapper->map($json, new JsonMapperTest_PHP74Array());
self::assertCount(1, $array->files);
}

Expand Down
8 changes: 1 addition & 7 deletions tests/Array_PHP80_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,12 @@
*/
class Array_PHP80_Test extends TestCase
{
protected function setUp(): void
{
require_once 'JsonMapperTest/PHP80_Array.php';
require_once 'JsonMapperTest/ArrayValueForStringProperty.php';
}

public function testJsonMapper()
{
$json = json_decode('{"files": [{"value":"test.txt"}]}');
$jsonMapper = new \JsonMapper();
$jsonMapper->bIgnoreVisibility = true;
$array = $jsonMapper->map($json, PHP80_Array::class);
$array = $jsonMapper->map($json, JsonMapperTest_PHP80Array::class);
self::assertCount(1, $array->getFiles());
self::assertInstanceOf(JsonMapperTest_ArrayValueForStringProperty::class, $array->getFiles()[0]);
}
Expand Down
5 changes: 0 additions & 5 deletions tests/ClassMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
* @license OSL-3.0 http://opensource.org/licenses/osl-3.0
* @link https://github.com/cweiske/jsonmapper
*/
require_once 'JsonMapperTest/Simple.php';
require_once 'JsonMapperTest/Object.php';
require_once 'JsonMapperTest/PlainObject.php';
require_once 'JsonMapperTest/ValueObject.php';
require_once 'JsonMapperTest/ComplexObject.php';

/**
* Unit tests for JsonMapper's classMap
Expand Down
7 changes: 0 additions & 7 deletions tests/Enums_PHP81_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ class Enums_PHP81_Test extends \PHPUnit\Framework\TestCase
{
const TEST_DATA = '{"stringBackedEnum": "foo", "intBackedEnum": 2}';

protected function setUp(): void
{
require_once 'Enums/IntBackedEnum.php';
require_once 'Enums/ObjectWithEnum.php';
require_once 'Enums/StringBackedEnum.php';
}

/**
* Test for PHP8.1 enums.
*/
Expand Down
2 changes: 0 additions & 2 deletions tests/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

use PHPUnit\Framework\TestCase;

require_once 'JsonMapperTest/EventObject.php';

/**
* Unit tests for JsonMapper's object handling (events)
*
Expand Down
6 changes: 0 additions & 6 deletions tests/JsonMapperTest/PrivateWithSetterSub.php

This file was deleted.

11 changes: 0 additions & 11 deletions tests/MixedType_PHP80_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@ class MixedType_PHP80_Test extends \PHPUnit\Framework\TestCase
const TEST_DATA_COMPLEX = '{"data": { "id": 123, "name": "Test User" }}';
const TEST_DATA_SIMPLE = '{"data": 123}';

/**
* Sets up test cases loading required classes.
*
* This is in setUp and not at the top of this file to ensure this is only
* executed with PHP 8.0 (due to the `@requires` tag).
*/
protected function setUp(): void
{
require_once 'namespacetest/PhpMixedType.php';
}

/**
* Test for PHP 8.0 mixed type containing an object.
*/
Expand Down
2 changes: 0 additions & 2 deletions tests/NameMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use PHPUnit\Framework\TestCase;

require_once 'JsonMapperTest/Simple.php';

class NameMappingTest extends TestCase
{
public function testItSetKeysIfReturnedByUndefinedPropertyHandler(): void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<?php
namespace namespacetest;
require_once __DIR__ . '/Unit.php';
require_once __DIR__ . '/UnitData.php';
require_once __DIR__ . '/model/MyArrayObject.php';
require_once __DIR__ . '/model/User.php';
require_once __DIR__ . '/model/UserList.php';
require_once __DIR__ . '/../othernamespace/Foo.php';

use namespacetest\UnitData;

class NamespaceTest extends \PHPUnit\Framework\TestCase
{
Expand Down
7 changes: 0 additions & 7 deletions tests/ObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@
* @license OSL-3.0 http://opensource.org/licenses/osl-3.0
* @link https://github.com/cweiske/jsonmapper
*/
require_once 'JsonMapperTest/Simple.php';
require_once 'JsonMapperTest/Object.php';
require_once 'JsonMapperTest/PlainObject.php';
require_once 'JsonMapperTest/ValueObject.php';
require_once 'JsonMapperTest/ComplexObject.php';
require_once 'JsonMapperTest/ObjectConstructor.php';
require_once 'JsonMapperTest/ObjectConstructorOptional.php';

/**
* Unit tests for JsonMapper's object handling
Expand Down
17 changes: 3 additions & 14 deletions tests/Object_PHP71_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@
*/
class Object_PHP71_Test extends \PHPUnit\Framework\TestCase
{
/**
* Sets up test cases loading required classes.
*
* This is in setUp and not at the top of this file to ensure this is only
* executed with PHP 7.1 (due to the `@requires` tag).
*/
protected function setUp(): void
{
require_once 'JsonMapperTest/PlainObject.php';
require_once 'JsonMapperTest/PHP7_Object.php';
}
/**
* Test for PHP7 nullable types like "?Object"
*/
Expand All @@ -42,7 +31,7 @@ public function testObjectSetterTypeNullable()
$jm = new JsonMapper();
$sn = $jm->map(
json_decode('{"typeNullableObject":null}'),
new JsonMapperTest_PHP7_Object()
new JsonMapperTest_PHP7Object()
);
$this->assertNull($sn->typeNullableObject);
}
Expand All @@ -53,11 +42,11 @@ public function testObjectSetterTypeNullable()
public function testObjectSetterDocblockInvalidNull()
{
$this->expectException(JsonMapper_Exception::class);
$this->expectExceptionMessage('JSON property "nonNullableObject" in class "JsonMapperTest_PHP7_Object" must not be NULL');
$this->expectExceptionMessage('JSON property "nonNullableObject" in class "JsonMapperTest_PHP7Object" must not be NULL');
$jm = new JsonMapper();
$sn = $jm->map(
json_decode('{"nonNullableObject":null}'),
new JsonMapperTest_PHP7_Object()
new JsonMapperTest_PHP7Object()
);
}
}
Expand Down
31 changes: 12 additions & 19 deletions tests/OtherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@
* @license OSL-3.0 http://opensource.org/licenses/osl-3.0
* @link http://cweiske.de/
*/
require_once 'JsonMapperTest/Broken.php';
require_once 'JsonMapperTest/DependencyInjector.php';
require_once 'JsonMapperTest/Simple.php';
require_once 'JsonMapperTest/Logger.php';
require_once 'JsonMapperTest/PrivateWithSetter.php';
require_once 'JsonMapperTest/PrivateWithSetterSub.php';
require_once 'JsonMapperTest/ValueObject.php';

/**
* Unit tests for JsonMapper that don't fit in other categories
Expand Down Expand Up @@ -210,7 +203,7 @@ public function testPrivatePropertyWithPublicSetter()
$jm->setLogger($logger);

$json = '{"privateProperty" : 1}';
$result = $jm->map(json_decode($json), new PrivateWithSetter());
$result = $jm->map(json_decode($json), new JsonMapperTest_PrivateWithSetter());

$this->assertEquals(1, $result->getPrivateProperty());
$this->assertTrue(empty($logger->log));
Expand All @@ -219,14 +212,14 @@ public function testPrivatePropertyWithPublicSetter()
public function testPrivatePropertyWithNoSetter()
{
$this->expectException(JsonMapper_Exception::class);
$this->expectExceptionMessage('JSON property "privateNoSetter" has no public setter method in object of type PrivateWithSetter');
$this->expectExceptionMessage('JSON property "privateNoSetter" has no public setter method in object of type JsonMapperTest_PrivateWithSetter');
$jm = new JsonMapper();
$jm->bExceptionOnUndefinedProperty = true;
$logger = new JsonMapperTest_Logger();
$jm->setLogger($logger);

$json = '{"privateNoSetter" : 1}';
$result = $jm->map(json_decode($json), new PrivateWithSetter());
$result = $jm->map(json_decode($json), new JsonMapperTest_PrivateWithSetter());

$this->assertEquals(1, $result->getPrivateProperty());
$this->assertTrue(empty($logger->log));
Expand All @@ -238,7 +231,7 @@ public function testPrivatePropertyWithNoSetterButAllowed()
$jm->bIgnoreVisibility = true;

$json = '{"privateNoSetter" : 1}';
$result = $jm->map(json_decode($json), new PrivateWithSetter());
$result = $jm->map(json_decode($json), new JsonMapperTest_PrivateWithSetter());

$this->assertEquals(1, $result->getPrivateNoSetter());
}
Expand All @@ -249,22 +242,22 @@ public function testPrivatePropertyInParentClassWithNoSetterButAllowed()
$jm->bIgnoreVisibility = true;

$json = '{"privateNoSetter" : 1}';
$result = $jm->map(json_decode($json), new PrivateWithSetterSub());
$result = $jm->map(json_decode($json), new JsonMapperTest_PrivateWithSetterSub());

$this->assertEquals(1, $result->getPrivateNoSetter());
}

public function testPrivatePropertyWithPrivateSetter()
{
$this->expectException(JsonMapper_Exception::class);
$this->expectExceptionMessage('JSON property "privatePropertyPrivateSetter" has no public setter method in object of type PrivateWithSetter');
$this->expectExceptionMessage('JSON property "privatePropertyPrivateSetter" has no public setter method in object of type JsonMapperTest_PrivateWithSetter');
$jm = new JsonMapper();
$jm->bExceptionOnUndefinedProperty = true;
$logger = new JsonMapperTest_Logger();
$jm->setLogger($logger);

$json = '{"privatePropertyPrivateSetter" : 1}';
$result = $jm->map(json_decode($json), new PrivateWithSetter());
$result = $jm->map(json_decode($json), new JsonMapperTest_PrivateWithSetter());
}

public function testPrivatePropertySetterWithoutDoc()
Expand All @@ -277,7 +270,7 @@ public function testPrivatePropertySetterWithoutDoc()
$jm->bExceptionOnUndefinedProperty = true;
$jm->setLogger(new JsonMapperTest_Logger());

$result = $jm->map(json_decode('{"privatePropertySetterWithoutDoc" : 1}'), new PrivateWithSetter());
$result = $jm->map(json_decode('{"privatePropertySetterWithoutDoc" : 1}'), new JsonMapperTest_PrivateWithSetter());
$this->assertEquals(1, $result->getPrivatePropertySetterWithoutDoc());
}

Expand All @@ -291,7 +284,7 @@ public function testPrivatePropertyNullableNotNullSetterWithoutDoc()
$jm->bExceptionOnUndefinedProperty = true;
$jm->setLogger(new JsonMapperTest_Logger());

$result = $jm->map(json_decode('{"privatePropertyNullableSetterWithoutDoc" : 1}'), new PrivateWithSetter());
$result = $jm->map(json_decode('{"privatePropertyNullableSetterWithoutDoc" : 1}'), new JsonMapperTest_PrivateWithSetter());
$this->assertSame(1, $result->getPrivatePropertyNullableSetterWithoutDoc());
}

Expand All @@ -305,7 +298,7 @@ public function testPrivatePropertyNullableNullSetterWithoutDoc()
$jm->bExceptionOnUndefinedProperty = true;
$jm->setLogger(new JsonMapperTest_Logger());

$result = $jm->map(json_decode('{"privatePropertyNullableSetterWithoutDoc" : null}'), new PrivateWithSetter());
$result = $jm->map(json_decode('{"privatePropertyNullableSetterWithoutDoc" : null}'), new JsonMapperTest_PrivateWithSetter());
$this->assertNull($result->getPrivatePropertyNullableSetterWithoutDoc());
}

Expand All @@ -319,7 +312,7 @@ public function testPrivateArrayOfSimple()
json_decode(
'{"privateArrayOfSimple" : [{"pbool": true, "pint": 42}, {"pbool": false, "pint": 24}]}'
),
new PrivateWithSetter()
new JsonMapperTest_PrivateWithSetter()
);

$a = new JsonMapperTest_Simple;
Expand All @@ -343,7 +336,7 @@ public function testPrivateSetterButAllowed()
$jm->bExceptionOnUndefinedProperty = true;

$json = '{"privateSetter" : 1}';
$result = $jm->map(json_decode($json), new PrivateWithSetter());
$result = $jm->map(json_decode($json), new JsonMapperTest_PrivateWithSetter());

$this->assertEquals(1, $result->getPrivateSetter());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* @license OSL-3.0 http://opensource.org/licenses/osl-3.0
* @link http://cweiske.de/
*/
require_once __DIR__ . '/../JsonMapperTest/Simple.php';

/**
* Unit tests for JsonMapper option "bRemoveUndefinedAttributes".
Expand Down
1 change: 0 additions & 1 deletion tests/SimpleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* @license OSL-3.0 http://opensource.org/licenses/osl-3.0
* @link https://github.com/cweiske/jsonmapper
*/
require_once 'JsonMapperTest/Simple.php';

/**
* Unit tests for JsonMapper's simple type handling
Expand Down
13 changes: 0 additions & 13 deletions tests/StrictTypes_PHP74_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@ class StrictTypes_PHP74_Test extends \PHPUnit\Framework\TestCase
{
const TEST_DATA = '{"id": 123, "importedNs": {"name": "Name"}, "otherNs": {"name": "Foo"}, "withoutType": "anything", "docDefinedType": {"name": "Name"}, "nullable": "value", "fooArray": [{"name": "Foo 1"}, {"name": "Foo 2"}]}';

/**
* Sets up test cases loading required classes.
*
* This is in setUp and not at the top of this file to ensure this is only
* executed with PHP 7.4 (due to the `@requires` tag).
*/
protected function setUp(): void
{
require_once 'namespacetest/PhpStrictTypes.php';
require_once 'namespacetest/model/User.php';
require_once 'othernamespace/Foo.php';
}

/**
* Test for PHP7.4 strict types.
*/
Expand Down
11 changes: 0 additions & 11 deletions tests/UnionTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@
*/
class UnionTypesTest extends \PHPUnit\Framework\TestCase
{
/**
* Sets up test cases loading required classes.
*
* This is in setUp and not at the top of this file to ensure this is only
* executed with PHP 8.0 (due to the `@requires` tag).
*/
protected function setUp(): void
{
require_once 'JsonMapperTest/UnionTypes.php';
}

/**
* Test for "public DateTime|string $dateOrStringNative"
*/
Expand Down
10 changes: 2 additions & 8 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
if (is_dir(__DIR__ . '/../src/')) {
set_include_path(
__DIR__ . '/../src/'
. PATH_SEPARATOR . get_include_path()
);
}
require_once 'JsonMapper.php';
require_once 'JsonMapper/Exception.php';
require __DIR__ . '/../vendor/autoload.php';

?>
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 49f1fb2

Please sign in to comment.