Skip to content

Test enhancement #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ env:

matrix:
include:
- php: 7.0
env: COMPOSER_ARGS=""
- php: 7.1
env: COMPOSER_ARGS=""
- php: 7.2
env: COMPOSER_ARGS="" WITH_CS="true"

- php: 7.0
env: COMPOSER_ARGS="--prefer-lowest"
- php: 7.1
env: COMPOSER_ARGS="--prefer-lowest"
- php: 7.2
Expand Down
44 changes: 40 additions & 4 deletions tests/ObjectWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class ObjectWrapperTest extends TestCase
public function testOffsetExists()
{
$object = new ObjectWrapper((object)['a' => 'b', 'c' => ['d' => 'e']]);
$this->assertTrue(isset($object['a']));
$this->assertTrue(isset($object['c']));
$this->assertFalse(isset($object['d']));
$this->assertNotEmpty($object['a']);
$this->assertNotEmpty($object['c']);
$this->assertEmpty($object['d']);
}

public function testOffsetGet()
Expand All @@ -32,13 +32,15 @@ public function testOffsetGet()
public function testOffsetSet()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Modifying ObjectWrapper is not allowed');
$object = new ObjectWrapper((object)['a' => 'b', 'c' => ['d' => 'e']]);
$object['q'] = 'e';
}

public function testOffsetUnset()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Modifying ObjectWrapper is not allowed');
$object = new ObjectWrapper((object)['a' => 'b', 'c' => ['d' => 'e']]);
unset($object['q']);
}
Expand All @@ -54,41 +56,47 @@ public function testGetRequired()
$object = new ObjectWrapper((object)['a' => 'b']);
$this->assertSame('b', $object->getRequired('a'));
$this->expectException(MissingItemException::class);
$this->expectExceptionMessage('Missing required key "c"');
$object->getRequired('c');
}

public function testGetRequiredBoolWithNoItem()
{
$object = new ObjectWrapper((object)[]);
$this->expectException(MissingItemException::class);
$this->expectExceptionMessage('Missing required key "a"');
$object->getRequiredBool('a');
}

public function testGetRequiredFloatWithNoItem()
{
$object = new ObjectWrapper((object)[]);
$this->expectException(MissingItemException::class);
$this->expectExceptionMessage('Missing required key "a"');
$object->getRequiredFloat('a');
}

public function testGetRequiredIntWithNoItem()
{
$object = new ObjectWrapper((object)[]);
$this->expectException(MissingItemException::class);
$this->expectExceptionMessage('Missing required key "a"');
$object->getRequiredInt('a');
}

public function testGetRequiredObjectWithNoItem()
{
$object = new ObjectWrapper((object)[]);
$this->expectException(MissingItemException::class);
$this->expectExceptionMessage('Missing required key "a"');
$object->getRequiredObject('a');
}

public function testGetRequiredStringWithNoItem()
{
$object = new ObjectWrapper((object)[]);
$this->expectException(MissingItemException::class);
$this->expectExceptionMessage('Missing required key "a"');
$object->getRequiredString('a');
}

Expand All @@ -97,6 +105,7 @@ public function testGetRequiredBool()
$object = new ObjectWrapper((object)['a' => true, 'b' => 'other type']);
$this->assertTrue($object->getRequiredBool('a'));
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected boolean but fot string for key "b"');
$object->getRequiredBool('b');
}

Expand All @@ -106,6 +115,7 @@ public function testGetRequiredFloat()
$this->assertSame(1.23, $object->getRequiredFloat('a'));
$this->assertSame((float)1, $object->getRequiredFloat('b'));
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected float but fot string for key "c"');
$object->getRequiredFloat('c');
}

Expand All @@ -114,6 +124,7 @@ public function testGetRequiredInt()
$object = new ObjectWrapper((object)['a' => 1, 'b' => 1.23]);
$this->assertSame(1, $object->getRequiredInt('a'));
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected integer but fot float for key "b"');
$object->getRequiredInt('b');
}

Expand All @@ -124,6 +135,7 @@ public function testGetRequiredObject()
$object = new ObjectWrapper((object)['a' => $data, 'b' => 'other type']);
$this->assertDeepEquals($data, $object->getRequiredObject('a'));
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected object but fot string for key "b"');
$object->getRequiredObject('b');
}

Expand All @@ -132,6 +144,7 @@ public function testGetRequiredString()
$object = new ObjectWrapper((object)['a' => 'string', 'b' => 123]);
$this->assertSame('string', $object->getRequiredString('a'));
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected string but fot integer for key "b"');
$object->getRequiredString('b');
}

Expand All @@ -142,6 +155,7 @@ public function testGetBool()
$this->assertNull($object->getBool('c'));
$this->assertTrue($object->getBool('c', true));
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected boolean but fot string for key "b"');
$object->getRequiredBool('b');
}

Expand All @@ -153,6 +167,7 @@ public function testGetFloat()
$this->assertSame(2.34, $object->getFloat('d', 2.34));
$this->assertSame((float)1, $object->getFloat('b'));
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected float but fot string for key "c"');
$object->getFloat('c');
}

Expand All @@ -163,6 +178,7 @@ public function testGetInt()
$this->assertNull($object->getInt('c'));
$this->assertSame(2, $object->getInt('c', 2));
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected integer but fot float for key "b"');
$object->getInt('b');
}

Expand All @@ -174,6 +190,7 @@ public function testGetObject()
$this->assertDeepEquals($data, $object->getObject('a'));
$this->assertNull($object->getObject('c'));
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected object but fot string for key "b"');
$object->getObject('b');
}

Expand All @@ -184,6 +201,7 @@ public function testGetString()
$this->assertNull($object->getString('c'));
$this->assertSame('default', $object->getString('c', 'default'));
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected string but fot integer for key "b"');
$object->getString('b');
}

Expand All @@ -201,6 +219,7 @@ public function testGetArrayWithDifferentType()
{
$object = new ObjectWrapper((object)['a' => 'string']);
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected array but fot string for key "a"');
$object->getArray('a');
}

Expand All @@ -223,6 +242,7 @@ public function testGetArrayOfBoolWithDifferentType()
{
$object = new ObjectWrapper((object)['a' => 'string']);
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected array but fot string for key "a"');
$object->getArrayOfBool('a');
}

Expand All @@ -231,6 +251,7 @@ public function testGetArrayOfBoolWithDifferentItemType()
$array = [false, false, 0, true];
$object = new ObjectWrapper((object)['a' => $array]);
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected boolean but fot integer for key "a"');
$object->getArrayOfBool('a');
}

Expand All @@ -239,6 +260,7 @@ public function testGetArrayOfBoolWithNullItem()
$array = [false, false, null, true];
$object = new ObjectWrapper((object)['a' => $array]);
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected boolean but fot NULL for key "a"');
$object->getArrayOfBool('a');
}

Expand All @@ -255,6 +277,7 @@ public function testGetArrayOfFloatWithDifferentType()
{
$object = new ObjectWrapper((object)['a' => 'string']);
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected array but fot string for key "a"');
$object->getArrayOfFloat('a');
}

Expand All @@ -263,6 +286,7 @@ public function testGetArrayOfFloatWithDifferentItemType()
$array = [1.0, 2.3, false];
$object = new ObjectWrapper((object)['a' => $array]);
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected float but fot boolean for key "a"');
$object->getArrayOfFloat('a');
}

Expand All @@ -271,6 +295,7 @@ public function testGetArrayOfFloatWithNullItem()
$array = [1.0, 2.0, null, 3.3];
$object = new ObjectWrapper((object)['a' => $array]);
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected float but fot NULL for key "a"');
$object->getArrayOfFloat('a');
}

Expand All @@ -287,6 +312,7 @@ public function testGetArrayOfIntWithDifferentType()
{
$object = new ObjectWrapper((object)['a' => 'string']);
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected array but fot string for key "a"');
$object->getArrayOfInt('a');
}

Expand All @@ -295,6 +321,7 @@ public function testGetArrayOfIntWithDifferentItemType()
$array = [1, 9, 2.1, 4];
$object = new ObjectWrapper((object)['a' => $array]);
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected integer but fot float for key "a"');
$object->getArrayOfInt('a');
}

Expand All @@ -303,6 +330,7 @@ public function testGetArrayOfIntWithNullItem()
$array = [1, 3, null, 5];
$object = new ObjectWrapper((object)['a' => $array]);
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected integer but fot NULL for key "a"');
$object->getArrayOfInt('a');
}

Expand All @@ -319,6 +347,7 @@ public function testGetArrayOfStringWithDifferentType()
{
$object = new ObjectWrapper((object)['a' => 1]);
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected array but fot integer for key "a"');
$object->getArrayOfString('a');
}

Expand All @@ -327,6 +356,7 @@ public function testGetArrayOfStringWithDifferentItemType()
$array = ['string', 'aaa', 4];
$object = new ObjectWrapper((object)['a' => $array]);
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected string but fot integer for key "a"');
$object->getArrayOfString('a');
}

Expand All @@ -335,6 +365,7 @@ public function testGetArrayOfStringWithNullItem()
$array = ['string', 'item', null];
$object = new ObjectWrapper((object)['a' => $array]);
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected string but fot NULL for key "a"');
$object->getArrayOfString('a');
}

Expand All @@ -353,13 +384,15 @@ public function testGetArrayOfObjectWithDifferentType()
{
$object = new ObjectWrapper((object)['a' => 1]);
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected array but fot integer for key "a"');
$object->getArrayOfObject('a');
}

public function testGetArrayOfObjectWithDifferentItemType()
{
$object = new ObjectWrapper((object)['a' => 'string']);
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected array but fot string for key "a"');
$object->getArrayOfObject('a');
}

Expand All @@ -368,6 +401,7 @@ public function testGetArrayOfObjectWithNullItem()
$array = [(object)['a' => 'b'], (object)[0 => 0, 1 => 1], null];
$object = new ObjectWrapper((object)['a' => $array]);
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected object but fot NULL for key "a"');
$object->getArrayOfObject('a');
}

Expand All @@ -376,6 +410,7 @@ public function testGetArrayOfObjectWithAssociativeArrayItem()
$array = [(object)['a' => 'b'], (object)[0 => 0, 1 => 1], ['a' => 'b']];
$object = new ObjectWrapper((object)['a' => $array]);
$this->expectException(InvalidItemTypeException::class);
$this->expectExceptionMessage('Expected object but fot array for key "a"');
$object->getArrayOfObject('a');
}

Expand All @@ -386,7 +421,8 @@ public function testDoesNotAffectInput()
$data->a = $innerData;
$object = new ObjectWrapper($data);
$object->getRequiredObject('a')->getRequiredString('b');
$this->assertSame($innerData, $data->a);
$this->assertSame('c', $innerData->b);
$this->assertContains('c', (array) $data->a);
}

private function assertDeepEquals($expectedData, $dataWithWrappers)
Expand Down