Skip to content

Commit c519c06

Browse files
sommmenStringEpsilon
authored andcommitted
Added tests
Moved to more specific ArgumentOutOfRangeException
1 parent 8cbdb91 commit c519c06

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
**Deserialization:**
44
- Added `string Serialize(object? input, PhpSerializiationOptions? options = null)` to `PhpSerialization` so the target type can be specified at run time.
55
- `PhpSerialization` (entry point of the library) is now null reference aware, aiding library consumers in caching `NullReferenceException`.
6+
- `PhpSerialization` throws `ArgumentOutOfRangeException` instead of the more generalised `ArgumentException`
67
- Bugfix: "INF" and "-INF" would not be handled correctly when using explicit typing (`Deserialize<T>`) for some target types.
78
- Bugfix: Properly set classname when deserializing with explicit types that implement IPhpObject.
89
- Performance tweaks:

PhpSerializerNET.Test/Deserialize/Validation/TestOtherErrors.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,24 @@ public void ErrorOnTuple() {
3333

3434
[TestMethod]
3535
public void ErrorOnEmptyInput() {
36-
var ex = Assert.ThrowsException<ArgumentException>(
36+
var ex = Assert.ThrowsException<ArgumentOutOfRangeException>(
3737
() => PhpSerialization.Deserialize("")
3838
);
3939

40-
Assert.AreEqual("PhpSerialization.Deserialize(): Parameter 'input' must not be null or empty.", ex.Message);
40+
const string expected = "PhpSerialization.Deserialize(): Parameter 'input' must not be null or empty. (Parameter 'input')";
41+
Assert.AreEqual(expected, ex.Message);
4142

42-
ex = Assert.ThrowsException<ArgumentException>(
43+
ex = Assert.ThrowsException<ArgumentOutOfRangeException>(
4344
() => PhpSerialization.Deserialize<string>("")
4445
);
4546

46-
Assert.AreEqual("PhpSerialization.Deserialize(): Parameter 'input' must not be null or empty.", ex.Message);
47+
Assert.AreEqual(expected, ex.Message);
48+
49+
ex = Assert.ThrowsException<ArgumentOutOfRangeException>(
50+
() => PhpSerialization.Deserialize("", typeof(string))
51+
);
52+
53+
Assert.AreEqual(expected, ex.Message);
4754
}
4855
}
4956
}

PhpSerializerNET.Test/DeserializeObjects.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,30 @@ public void Test_Issue12() {
5757
var result = PhpSerialization.Deserialize("a:1:{i:0;a:4:{s:1:\"A\";s:2:\"63\";s:1:\"B\";a:2:{i:558710;s:1:\"2\";i:558709;s:1:\"2\";}s:1:\"C\";s:2:\"71\";s:1:\"G\";a:3:{s:1:\"x\";s:6:\"446368\";s:1:\"y\";s:1:\"0\";s:1:\"z\";s:5:\"1.029\";}}}");
5858
Assert.IsNotNull(result);
5959
}
60+
61+
[TestMethod]
62+
public void DeserializeWithRuntimeType() {
63+
var expectedType = typeof(CircularTest);
64+
var result = PhpSerialization.Deserialize("a:2:{s:3:\"Foo\";s:5:\"First\";s:3:\"Bar\";a:2:{s:3:\"Foo\";s:6:\"Second\";s:3:\"Bar\";N;}}", expectedType);
65+
66+
Assert.IsNotNull(result);
67+
Assert.AreEqual(expectedType, result.GetType());
68+
69+
if (result is CircularTest circularTest) {
70+
Assert.AreEqual(
71+
"First",
72+
circularTest.Foo
73+
);
74+
Assert.IsNotNull(
75+
circularTest.Bar
76+
);
77+
Assert.AreEqual(
78+
"Second",
79+
circularTest.Bar.Foo
80+
);
81+
} else {
82+
throw new InvalidOperationException();
83+
}
84+
}
6085
}
6186
}

PhpSerializerNET/PhpSerialization.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static class PhpSerialization {
3434
/// </returns>
3535
public static object? Deserialize(string input, PhpDeserializationOptions? options = null) {
3636
if (string.IsNullOrEmpty(input)) {
37-
throw new ArgumentException("PhpSerialization.Deserialize(): Parameter 'input' must not be null or empty.");
37+
throw new ArgumentOutOfRangeException(nameof(input), "PhpSerialization.Deserialize(): Parameter 'input' must not be null or empty.");
3838
}
3939
return new PhpDeserializer(input, options).Deserialize();
4040
}
@@ -60,7 +60,7 @@ public static T Deserialize<T>(
6060
PhpDeserializationOptions? options = null
6161
) {
6262
if (string.IsNullOrEmpty(input)) {
63-
throw new ArgumentException("PhpSerialization.Deserialize(): Parameter 'input' must not be null or empty.");
63+
throw new ArgumentOutOfRangeException(nameof(input), "PhpSerialization.Deserialize(): Parameter 'input' must not be null or empty.");
6464
}
6565
return new PhpDeserializer(input, options).Deserialize<T>();
6666
}
@@ -86,8 +86,11 @@ public static T Deserialize<T>(
8686
Type type,
8787
PhpDeserializationOptions? options = null
8888
) {
89+
if (type == null) {
90+
throw new ArgumentNullException(nameof(type));
91+
}
8992
if (string.IsNullOrEmpty(input)) {
90-
throw new ArgumentException("PhpSerialization.Deserialize(): Parameter 'input' must not be null or empty.");
93+
throw new ArgumentOutOfRangeException(nameof(input), "PhpSerialization.Deserialize(): Parameter 'input' must not be null or empty.");
9194
}
9295
return new PhpDeserializer(input, options).Deserialize(type);
9396
}

0 commit comments

Comments
 (0)