forked from phpgearbox/string
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseStrTest.php
More file actions
152 lines (126 loc) · 3.72 KB
/
Copy pathBaseStrTest.php
File metadata and controls
152 lines (126 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
use Gears\String\Str;
class BaseStrTest extends PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$this->assertInstanceOf('Gears\\String\\Str', new Str('foo'));
}
public function testFactory()
{
$this->assertInstanceOf('Gears\\String\\Str', Str::s('foo'));
}
public function testArrayInput()
{
$this->setExpectedException
(
'InvalidArgumentException',
'Passed value cannot be an array'
);
new Str(['foo']);
}
public function testCastableObjectInput()
{
$this->assertInstanceOf('Gears\\String\\Str', new Str(new Str('foo')));
}
public function testNonCastableObjectInput()
{
$this->setExpectedException
(
'InvalidArgumentException',
'Passed object must have a __toString method'
);
new Str(new \stdClass());
}
public function testToStringMagicMethod()
{
$str = new Str('öäü - foo');
$result = $str->__toString();
$this->assertTrue(is_string($result));
$this->assertEquals((string)$str, $result);
$this->assertEquals('öäü - foo', $result);
}
public function testDefaultEncoding()
{
$this->assertEquals('UTF-8', Str::s('foo')->getEncoding());
}
public function testLength()
{
$this->assertEquals(3, Str::s('foo')->getLength());
}
public function testCountable()
{
$this->assertInstanceOf('\\Countable', new Str('foo'));
$this->assertEquals(3, count(Str::s('foo')));
}
public function testIterable()
{
$foo = new Str('foo');
$this->assertInstanceOf('\\IteratorAggregate', $foo);
$iterator = $foo->getIterator();
$this->assertInstanceOf('\\ArrayIterator', $iterator);
$chars = $iterator->getArrayCopy();
$this->assertEquals(['f', 'o', 'o'], $chars);
}
public function testOffsetExists()
{
$foo = new Str('foo');
$this->assertTrue(isset($foo[0]));
$this->assertTrue(isset($foo[1]));
$this->assertTrue(isset($foo[2]));
$this->assertFalse(isset($foo[3]));
}
public function testOffsetGet()
{
$foo = new Str('foo');
$this->assertEquals('f', $foo[0]);
$this->assertEquals('o', $foo[1]);
$this->assertEquals('o', $foo[2]);
$this->assertInstanceOf('Gears\\String\\Str', $foo[0]);
}
public function testOffsetGetOutofBounds()
{
$this->setExpectedException
(
'OutOfBoundsException',
'No character exists at the index'
);
Str::s('foo')[3];
}
public function testOffsetSet()
{
$this->setExpectedException
(
'Exception',
'Str object is immutable, cannot modify char'
);
Str::s('foo')[0] = 'a';
}
public function testOffsetUnset()
{
$this->setExpectedException
(
'Exception',
'Str object is immutable, cannot unset char'
);
unset(Str::s('foo')[0]);
}
public function testCompare()
{
$this->assertEquals(0, Str::s('foo')->compare(Str::s('foo')));
if (defined('HHVM_VERSION') || strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
{
$this->assertEquals(-1, Str::s('foo')->compare(Str::s('bar')));
}
else
{
$this->assertEquals(-4, Str::s('foo')->compare(Str::s('bar')));
}
}
public function testEquals()
{
$this->assertTrue(Str::s('foo')->equals(Str::s('foo')));
$this->assertFalse(Str::s('foo')->equals(Str::s('bar')));
$this->assertFalse(Str::s('foo')->equals('foo'));
}
}