Skip to content

Commit 3286f1e

Browse files
committed
Added encoding test.
1 parent 9f5b746 commit 3286f1e

File tree

5 files changed

+229
-160
lines changed

5 files changed

+229
-160
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
This Source Code Form is subject to the terms of the Mozilla Public
3+
License, v. 2.0. If a copy of the MPL was not distributed with this
4+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
**/
6+
7+
using Microsoft.VisualStudio.TestTools.UnitTesting;
8+
using System.Text;
9+
10+
namespace PhpSerializerNET.Test {
11+
[TestClass]
12+
public class InputEncodingTest {
13+
private static string Latin1TestString = Encoding.Latin1.GetString(
14+
Encoding.Convert(
15+
Encoding.Default,
16+
Encoding.Latin1,
17+
Encoding.Default.GetBytes("s:3:\"äöü\";")
18+
)
19+
);
20+
21+
[TestMethod]
22+
public void WrongEncodingFails() {
23+
Assert.ThrowsException<DeserializationException>(
24+
() => PhpSerialization.Deserialize(Latin1TestString)
25+
);
26+
}
27+
28+
[TestMethod]
29+
public void CorrectEncodingWorks() {
30+
var result = PhpSerialization.Deserialize(
31+
Latin1TestString,
32+
new PhpDeserializationOptions(){
33+
InputEncoding =Encoding.Latin1
34+
}
35+
);
36+
37+
Assert.AreEqual("äöü", result);
38+
}
39+
}
40+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
This Source Code Form is subject to the terms of the Mozilla Public
3+
License, v. 2.0. If a copy of the MPL was not distributed with this
4+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
**/
6+
7+
using System.Collections;
8+
using System.Collections.Generic;
9+
using Microsoft.VisualStudio.TestTools.UnitTesting;
10+
using static PhpSerializerNET.Test.TestObjects;
11+
12+
namespace PhpSerializerNET.Test {
13+
[TestClass]
14+
public class StdClassTest {
15+
public struct MyStruct {
16+
public double John;
17+
public double Jane;
18+
}
19+
20+
[TestMethod]
21+
public void Option_Throw() {
22+
var ex = Assert.ThrowsException<DeserializationException>(
23+
() => PhpSerialization.Deserialize(
24+
"O:8:\"stdClass\":2:{s:4:\"John\";d:3.14;s:4:\"Jane\";d:2.718;}",
25+
new PhpDeserializationOptions() { StdClass = StdClassOption.Throw }
26+
)
27+
);
28+
29+
Assert.AreEqual(
30+
"Encountered 'stdClass' and the behavior 'Throw' was specified in deserialization options.",
31+
ex.Message
32+
);
33+
}
34+
35+
[TestMethod]
36+
public void Option_Dynamic() {
37+
dynamic result = (PhpDynamicObject)PhpSerialization.Deserialize(
38+
"O:8:\"stdClass\":2:{s:4:\"John\";d:3.14;s:4:\"Jane\";d:2.718;}",
39+
new PhpDeserializationOptions() { StdClass = StdClassOption.Dynamic }
40+
);
41+
42+
Assert.AreEqual(3.14, result.John);
43+
Assert.AreEqual(2.718, result.Jane);
44+
Assert.AreEqual("stdClass", result.GetClassName());
45+
}
46+
47+
[TestMethod]
48+
public void Option_Dictionary() {
49+
var result = (IDictionary)PhpSerialization.Deserialize(
50+
"O:8:\"stdClass\":2:{s:4:\"John\";d:3.14;s:4:\"Jane\";d:2.718;}",
51+
new PhpDeserializationOptions() { StdClass = StdClassOption.Dictionary }
52+
);
53+
54+
Assert.AreEqual(3.14, result["John"]);
55+
Assert.AreEqual(2.718, result["Jane"]);
56+
}
57+
58+
[TestMethod]
59+
public void Overridden_By_Class() {
60+
var anonymous = new { foo = "foo" };
61+
var result = PhpSerialization.Deserialize<NamedClass>(
62+
"O:8:\"stdClass\":2:{s:4:\"John\";d:3.14;s:4:\"Jane\";d:2.718;}",
63+
new PhpDeserializationOptions() { StdClass = StdClassOption.Dynamic }
64+
);
65+
66+
Assert.AreEqual(3.14, result.John);
67+
Assert.AreEqual(2.718, result.Jane);
68+
}
69+
70+
[TestMethod]
71+
public void Overridden_By_Struct() {
72+
var result = PhpSerialization.Deserialize<MyStruct>(
73+
"O:8:\"stdClass\":2:{s:4:\"John\";d:3.14;s:4:\"Jane\";d:2.718;}",
74+
new PhpDeserializationOptions() { StdClass = StdClassOption.Dynamic }
75+
);
76+
77+
Assert.AreEqual(3.14, result.John);
78+
Assert.AreEqual(2.718, result.Jane);
79+
}
80+
81+
[TestMethod]
82+
public void Overridden_By_Dictionary() {
83+
var result = PhpSerialization.Deserialize<Dictionary<string, object>>(
84+
"O:8:\"stdClass\":2:{s:4:\"John\";d:3.14;s:4:\"Jane\";d:2.718;}",
85+
new PhpDeserializationOptions() { StdClass = StdClassOption.Dynamic }
86+
);
87+
88+
Assert.AreEqual(3.14, result["John"]);
89+
Assert.AreEqual(2.718, result["Jane"]);
90+
}
91+
}
92+
}
Lines changed: 96 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,96 @@
1-
/**
2-
This Source Code Form is subject to the terms of the Mozilla Public
3-
License, v. 2.0. If a copy of the MPL was not distributed with this
4-
file, You can obtain one at http://mozilla.org/MPL/2.0/.
5-
**/
6-
7-
using System;
8-
using System.Collections;
9-
using System.Collections.Generic;
10-
using System.Text.Json;
11-
using Microsoft.VisualStudio.TestTools.UnitTesting;
12-
using PhpSerializerNET;
13-
14-
namespace PhpSerializerNET.Test {
15-
[TestClass]
16-
public class DeserializeListOptions {
17-
18-
[TestMethod]
19-
public void ListOptionNever() {
20-
var test = PhpSerialization.Deserialize(
21-
"a:2:{i:0;s:1:\"a\";i:1;s:1:\"b\";}",
22-
new PhpDeserializationOptions() {
23-
UseLists = ListOptions.Never
24-
}
25-
);
26-
27-
var dictionary = test as Dictionary<object, object>;
28-
Assert.IsNotNull(dictionary);
29-
Assert.AreEqual(2, dictionary.Count);
30-
Assert.AreEqual("a", dictionary[(long)0]);
31-
Assert.AreEqual("b", dictionary[(long)1]);
32-
}
33-
34-
[TestMethod]
35-
public void ListOptionDefault() {
36-
var test = PhpSerialization.Deserialize(
37-
"a:2:{i:0;s:1:\"a\";i:1;s:1:\"b\";}",
38-
new PhpDeserializationOptions() {
39-
UseLists = ListOptions.Default
40-
}
41-
);
42-
43-
var list = test as List<object>;
44-
Assert.IsNotNull(list);
45-
Assert.AreEqual(2, list.Count);
46-
Assert.AreEqual("a", list[0]);
47-
Assert.AreEqual("b", list[1]);
48-
49-
// Same option, non-consecutive integer keys:
50-
test = PhpSerialization.Deserialize(
51-
"a:2:{i:2;s:1:\"a\";i:4;s:1:\"b\";}",
52-
new PhpDeserializationOptions() {
53-
UseLists = ListOptions.Default
54-
}
55-
);
56-
57-
var dictionary = test as Dictionary<object, object>;
58-
Assert.IsNotNull(dictionary);
59-
Assert.AreEqual(2, dictionary.Count);
60-
Assert.AreEqual("a", dictionary[(long)2]);
61-
Assert.AreEqual("b", dictionary[(long)4]);
62-
}
63-
64-
[TestMethod]
65-
public void ListOptionAllIntegers() {
66-
var test = PhpSerialization.Deserialize(
67-
"a:2:{i:0;s:1:\"a\";i:1;s:1:\"b\";}",
68-
new PhpDeserializationOptions() {
69-
UseLists = ListOptions.OnAllIntegerKeys
70-
}
71-
);
72-
73-
var list = test as List<object>;
74-
Assert.IsNotNull(list);
75-
Assert.AreEqual(2, list.Count);
76-
Assert.AreEqual("a", list[0]);
77-
Assert.AreEqual("b", list[1]);
78-
79-
// Same option, non-consecutive integer keys:
80-
test = PhpSerialization.Deserialize(
81-
"a:2:{i:2;s:1:\"a\";i:4;s:1:\"b\";}",
82-
new PhpDeserializationOptions() {
83-
UseLists = ListOptions.OnAllIntegerKeys
84-
}
85-
);
86-
87-
list = test as List<object>;
88-
Assert.IsNotNull(list);
89-
Assert.AreEqual(2, list.Count);
90-
Assert.AreEqual("a", list[0]);
91-
Assert.AreEqual("b", list[1]);
92-
}
93-
}
94-
}
1+
/**
2+
This Source Code Form is subject to the terms of the Mozilla Public
3+
License, v. 2.0. If a copy of the MPL was not distributed with this
4+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
**/
6+
7+
using System.Collections.Generic;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
10+
namespace PhpSerializerNET.Test {
11+
[TestClass]
12+
public class UseListsTest {
13+
[TestMethod]
14+
public void Option_Never() {
15+
var test = PhpSerialization.Deserialize(
16+
"a:2:{i:0;s:1:\"a\";i:1;s:1:\"b\";}",
17+
new PhpDeserializationOptions() {
18+
UseLists = ListOptions.Never
19+
}
20+
);
21+
22+
var dictionary = test as Dictionary<object, object>;
23+
Assert.IsNotNull(dictionary);
24+
Assert.AreEqual(2, dictionary.Count);
25+
Assert.AreEqual("a", dictionary[(long)0]);
26+
Assert.AreEqual("b", dictionary[(long)1]);
27+
}
28+
29+
[TestMethod]
30+
public void Option_Default() {
31+
var result = PhpSerialization.Deserialize(
32+
"a:2:{i:0;s:1:\"a\";i:1;s:1:\"b\";}",
33+
new PhpDeserializationOptions() {
34+
UseLists = ListOptions.Default
35+
}
36+
);
37+
38+
var list = result as List<object>;
39+
Assert.IsNotNull(list);
40+
Assert.AreEqual(2, list.Count);
41+
Assert.AreEqual("a", list[0]);
42+
Assert.AreEqual("b", list[1]);
43+
}
44+
45+
[TestMethod]
46+
public void Option_Default_NonConsequetive() {
47+
// Same option, non-consecutive integer keys:
48+
var result = PhpSerialization.Deserialize(
49+
"a:2:{i:2;s:1:\"a\";i:4;s:1:\"b\";}",
50+
new PhpDeserializationOptions() {
51+
UseLists = ListOptions.Default
52+
}
53+
);
54+
55+
var dictionary = result as Dictionary<object, object>;
56+
Assert.IsNotNull(dictionary);
57+
Assert.AreEqual(2, dictionary.Count);
58+
Assert.AreEqual("a", dictionary[(long)2]);
59+
Assert.AreEqual("b", dictionary[(long)4]);
60+
}
61+
62+
[TestMethod]
63+
public void Option_OnAllIntegerKeys() {
64+
var test = PhpSerialization.Deserialize(
65+
"a:2:{i:0;s:1:\"a\";i:1;s:1:\"b\";}",
66+
new PhpDeserializationOptions() {
67+
UseLists = ListOptions.OnAllIntegerKeys
68+
}
69+
);
70+
71+
var list = test as List<object>;
72+
Assert.IsNotNull(list);
73+
Assert.AreEqual(2, list.Count);
74+
Assert.AreEqual("a", list[0]);
75+
Assert.AreEqual("b", list[1]);
76+
77+
}
78+
79+
[TestMethod]
80+
public void Option_OnAllIntegerKeys_NonConsequetive() {
81+
// Same option, non-consecutive integer keys:
82+
var result = PhpSerialization.Deserialize(
83+
"a:2:{i:2;s:1:\"a\";i:4;s:1:\"b\";}",
84+
new PhpDeserializationOptions() {
85+
UseLists = ListOptions.OnAllIntegerKeys
86+
}
87+
);
88+
89+
var list = result as List<object>;
90+
Assert.IsNotNull(list);
91+
Assert.AreEqual(2, list.Count);
92+
Assert.AreEqual("a", list[0]);
93+
Assert.AreEqual("b", list[1]);
94+
}
95+
}
96+
}

PhpSerializerNET.Test/Deserialize/PhpDateTimeDeserialization.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public void DeserializesCorrectly() {
2121
var date = result as PhpDateTime;
2222
Assert.AreEqual("UTC", date.Timezone);
2323
Assert.AreEqual("2021-08-18 09:10:23.441055", date.Date);
24+
Assert.AreEqual("DateTime", date.GetClassName());
2425
}
2526
}
2627
}

0 commit comments

Comments
 (0)