-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
StepTest.php
167 lines (145 loc) · 5.96 KB
/
StepTest.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
<?php
declare(strict_types=1);
use Codeception\Command\Shared\ConfigTrait;
use Codeception\Step;
use Codeception\Step\Argument\FormattedOutput;
use Codeception\Stub;
use Facebook\WebDriver\WebDriverBy;
use PHPUnit\Framework\TestCase;
class StepTest extends TestCase
{
protected function getStep(array $args): Step
{
return $this->getMockBuilder('\Codeception\Step')
->setConstructorArgs($args)
->onlyMethods([])
->getMock();
}
/**
* @dataProvider getArgumentsDataProvider
*/
public function testGetArguments(array $arguments, string $expectedOutput)
{
$step = $this->getStep(['', $arguments]);
$this->assertSame($expectedOutput, $step->getArgumentsAsString());
}
protected function getArgumentsDataProvider(): array
{
$mockGenerator = new \PHPUnit\Framework\MockObject\Generator();
$traitMock = $mockGenerator->getMockForTrait(_generated\CodeGuyActions::class);
$classMock = $mockGenerator->getMock($this::class);
return [
'scalar arguments' => [
[['just', 'array']],
'["just","array"]',
],
'closure' => [
[function () {
}],
'"Closure"',
],
'instance method' => [
[[$this, 'testGetArguments']],
'["StepTest","testGetArguments"]',
],
'class method' => [
[[PDO::class, 'getAvailableDrivers']],
'["PDO","getAvailableDrivers"]',
],
'stubbed class method' => [
[[Stub::make($this, []), 'testGetArguments']],
'["StepTest","testGetArguments"]',
],
'stubbed interface method' => [
[[Stub::makeEmpty(Countable::class, []), 'testGetArguments']],
'["Countable","testGetArguments"]',
],
'mocked trait method' => [
[[$traitMock, 'testGetArguments']],
'["' . get_parent_class($traitMock) . '","testGetArguments"]',
],
'mocked class method' => [
[[$classMock, 'testGetArguments']],
'["StepTest","testGetArguments"]',
],
];
}
public function testGetHtml()
{
$step = $this->getStep(['Do some testing', ['arg1', 'arg2']]);
$this->assertSame('I do some testing <span style="color: #732E81">"arg1","arg2"</span>', $step->getHtml());
$step = $this->getStep(['Do some testing', []]);
$this->assertSame('I do some testing', $step->getHtml());
$argument = str_repeat("A string with a length exceeding Step::DEFAULT_MAX_LENGTH.", Step::DEFAULT_MAX_LENGTH);
$step = $this->getStep(['Do some testing', [$argument]]);
$this->assertSame('I do some testing <span style="color: #732E81">"' . $argument . '"</span>', $step->getHtml());
}
public function testLongArguments()
{
$step = $this->getStep(['have in database', [str_repeat('a', 2000)]]);
$output = $step->toString(200);
$this->assertLessThan(201, strlen($output), 'Output is too long: ' . $output);
$step = $this->getStep(['have in database', [str_repeat('a', 100), str_repeat('b', 100)]]);
$output = $step->toString(50);
$this->assertSame(50, strlen($output), 'Incorrect length of output: ' . $output);
$this->assertSame('have in database "aaaaaaaaaaa...","bbbbbbbbbbb..."', $output);
$step = $this->getStep(['have in database', [1, str_repeat('b', 100)]]);
$output = $step->toString(50);
$this->assertSame('have in database 1,"bbbbbbbbbbbbbbbbbbbbbbbbbb..."', $output);
$step = $this->getStep(['have in database', [str_repeat('b', 100), 1]]);
$output = $step->toString(50);
$this->assertSame('have in database "bbbbbbbbbbbbbbbbbbbbbbbbbb...",1', $output);
}
public function testArrayAsArgument()
{
$step = $this->getStep(['see array', [[1,2,3], 'two']]);
$output = $step->toString(200);
$this->assertSame('see array [1,2,3],"two"', $output);
}
public function testSingleQuotedStringAsArgument()
{
$step = $this->getStep(['see array', [[1,2,3], "'two'"]]);
$output = $step->toString(200);
$this->assertSame('see array [1,2,3],"\'two\'"', $output);
}
public function testSeeUppercaseText()
{
$step = $this->getStep(['see', ['UPPER CASE']]);
$output = $step->toString(200);
$this->assertSame('see "UPPER CASE"', $output);
}
public function testMultiByteTextLengthIsMeasuredCorrectly()
{
$step = $this->getStep(['see', ['ŽŽŽŽŽŽŽŽŽŽ', 'AAAAAAAAAAA']]);
$output = $step->toString(30);
$this->assertSame('see "ŽŽŽŽŽŽŽŽŽŽ","AAAAAAAAAAA"', $output);
}
public function testAmOnUrl()
{
$step = $this->getStep(['amOnUrl', ['http://www.example.org/test']]);
$output = $step->toString(200);
$this->assertSame('am on url "http://www.example.org/test"', $output);
}
public function testNoArgs()
{
$step = $this->getStep(['acceptPopup', []]);
$output = $step->toString(200);
$this->assertSame('accept popup ', $output);
$output = $step->toString(-5);
$this->assertSame('accept popup ', $output);
}
public function testSeeMultiLineStringInSingleLine()
{
$step = $this->getStep(['see', ["aaaa\nbbbb\nc"]]);
$output = $step->toString(200);
$this->assertSame('see "aaaa\nbbbb\nc"', $output);
}
public function testFormattedOutput()
{
$argument = Stub::makeEmpty(FormattedOutput::class);
$argument->method('getOutput')->willReturn('some formatted output');
$step = $this->getStep(['argument', [$argument]]);
$output = $step->toString(200);
$this->assertSame('argument "some formatted output"', $output);
}
}