-
-
Notifications
You must be signed in to change notification settings - Fork 499
/
SystemNodeProviderTest.php
188 lines (162 loc) · 5.96 KB
/
SystemNodeProviderTest.php
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace Ramsey\Uuid\Test\Provider\Node;
use Ramsey\Uuid\Provider\Node\SystemNodeProvider;
use Ramsey\Uuid\Test\TestCase;
use AspectMock\Test as AspectMock;
class SystemNodeProviderTest extends TestCase
{
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGetNodeReturnsSystemNodeFromMacAddress()
{
$provider = $this->getMockBuilder('Ramsey\Uuid\Provider\Node\SystemNodeProvider')
->setMethods(['getIfconfig'])
->getMock();
$provider->expects($this->once())
->method('getIfconfig')
->willReturn(PHP_EOL . 'AA-BB-CC-DD-EE-FF' . PHP_EOL);
$node = $provider->getNode();
$this->assertTrue(ctype_xdigit($node), 'Node should be a hexadecimal string. Actual node: ' . $node);
$length = strlen($node);
$lengthError = 'Node should be 12 characters. Actual length: ' . $length . PHP_EOL . ' Actual node: ' . $node;
$this->assertTrue(($length === 12), $lengthError);
}
public function notationalFormatsDataProvider()
{
return [
['01-23-45-67-89-ab', '0123456789ab'],
['01:23:45:67:89:ab', '0123456789ab']
];
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
* @dataProvider notationalFormatsDataProvider
* @param $formatted
* @param $expected
*/
public function testGetNodeReturnsNodeStrippedOfNotationalFormatting($formatted, $expected)
{
//Using a stub to provide data for the protected method that gets the node
$provider = $this->getMockBuilder(SystemNodeProvider::class)
->setMethods(['getIfconfig'])
->getMock();
$provider->method('getIfconfig')
->willReturn(PHP_EOL . $formatted . PHP_EOL);
$node = $provider->getNode();
$this->assertEquals($expected, $node);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGetNodeReturnsFirstMacAddressFound()
{
//Using a stub to provide data for the protected method that gets the node
$provider = $this->getMockBuilder(SystemNodeProvider::class)
->setMethods(['getIfconfig'])
->getMock();
$provider->method('getIfconfig')
->willReturn(PHP_EOL . 'AA-BB-CC-DD-EE-FF' . PHP_EOL .
'00-11-22-33-44-55' . PHP_EOL .
'FF-11-EE-22-DD-33' . PHP_EOL);
$node = $provider->getNode();
$this->assertEquals('AABBCCDDEEFF', $node);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGetNodeReturnsFalseWhenNodeIsNotFound()
{
$provider = $this->getMockBuilder('Ramsey\Uuid\Provider\Node\SystemNodeProvider')
->setMethods(['getIfconfig'])
->getMock();
$provider->expects($this->once())
->method('getIfconfig')
->willReturn('some string that does not match the mac address');
$node = $provider->getNode();
$this->assertFalse($node);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGetNodeWillNotExecuteSystemCallIfFailedFirstTime()
{
$provider = $this->getMockBuilder('Ramsey\Uuid\Provider\Node\SystemNodeProvider')
->setMethods(['getIfconfig'])
->getMock();
$provider->expects($this->once())
->method('getIfconfig')
->willReturn('some string that does not match the mac address');
$provider->getNode();
$provider->getNode();
}
public function osCommandDataProvider()
{
return [
'windows' => ['Windows', 'ipconfig /all 2>&1', 'ipconfig-all.txt'],
'mac' => ['Darwhat', 'ifconfig 2>&1', 'ifconfig.txt'],
'linux' => ['Linux', 'netstat -ie 2>&1', 'netstat-ie.txt'],
'anything_else' => ['someotherxyz', 'netstat -ie 2>&1', 'netstat-ie.txt']
];
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
* @dataProvider osCommandDataProvider
* @param $os
* @param $command
* @param $filename
*/
public function testGetNodeGetsNetworkInterfaceConfig($os, $command, $filename)
{
$commandOutput = file_get_contents(dirname(__FILE__) . '/' . $filename);
AspectMock::func('Ramsey\Uuid\Provider\Node', 'php_uname', $os);
$passthru = AspectMock::func(
'Ramsey\Uuid\Provider\Node',
'passthru',
function () use ($commandOutput) {
echo $commandOutput;
}
);
$provider = new SystemNodeProvider();
$this->assertSame('09001290e3e5', $provider->getNode());
$passthru->verifyInvokedOnce([$command]);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGetNodeReturnsSameNodeUponSubsequentCalls()
{
//Using a stub to provide data for the protected method that gets the node
$provider = $this->getMockBuilder(SystemNodeProvider::class)
->setMethods(['getIfconfig'])
->getMock();
$provider->method('getIfconfig')
->willReturn(PHP_EOL . 'AA-BB-CC-DD-EE-FF' . PHP_EOL);
$node = $provider->getNode();
$node2 = $provider->getNode();
$this->assertEquals($node, $node2);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testSubsequentCallsToGetNodeDoNotRecallIfconfig()
{
//Using a mock to verify the provider only gets the node from ifconfig one time
$provider = $this->getMockBuilder(SystemNodeProvider::class)
->setMethods(['getIfconfig'])
->getMock();
$provider->expects($this->once())
->method('getIfconfig')
->willReturn(PHP_EOL . 'AA-BB-CC-DD-EE-FF' . PHP_EOL);
$provider->getNode();
$provider->getNode();
}
}