Skip to content

Commit e8e88d3

Browse files
committed
Changed ImportSpecification::setMaxFetchAttempts to throw exception instead of silently converting invalid values.
1 parent 76d3909 commit e8e88d3

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

src/Specification/ImportSpecification.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,11 @@ final public function getMaxFetchAttempts()
229229
*/
230230
final public function setMaxFetchAttempts($attempts)
231231
{
232-
// TODO: Consider throwing exception instead of silently constraining bounds.
233-
$this->maxFetchAttempts = max(1, $attempts | 0);
232+
if (!is_int($attempts) || $attempts < 1) {
233+
throw new \InvalidArgumentException('Fetch attempts must be greater than or equal to 1.');
234+
}
235+
236+
$this->maxFetchAttempts = $attempts;
234237

235238
return $this;
236239
}

test/Unit/Porter/ImportSpecificationTest.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,27 +114,40 @@ public function testCache()
114114
}
115115

116116
/**
117-
* @param mixed $input
118-
* @param int $output
117+
* @param int $value
119118
*
120-
* @dataProvider provideFetchAttempts
119+
* @dataProvider provideValidFetchAttempts
121120
*/
122-
public function testMaxFetchAttempts($input, $output)
121+
public function testValidMaxFetchAttempts($value)
123122
{
124-
self::assertSame($output, $this->specification->setMaxFetchAttempts($input)->getMaxFetchAttempts());
123+
self::assertSame($value, $this->specification->setMaxFetchAttempts($value)->getMaxFetchAttempts());
125124
}
126125

127-
public function provideFetchAttempts()
126+
public function provideValidFetchAttempts()
128127
{
129128
return [
130-
// Valid.
131-
[1, 1],
132-
[2, 2],
133-
134-
// Invalid.
135-
'Too low, positive' => [0, 1],
136-
'Too low, negative' => [-1, 1],
137-
'Float in range' => [1.9, 1],
129+
[1],
130+
[PHP_INT_MAX],
131+
];
132+
}
133+
134+
/**
135+
* @param mixed $value
136+
*
137+
* @dataProvider provideInvalidFetchAttempts
138+
*/
139+
public function testInvalidMaxFetchAttempts($value)
140+
{
141+
$this->setExpectedException(\InvalidArgumentException::class);
142+
$this->specification->setMaxFetchAttempts($value);
143+
}
144+
145+
public function provideInvalidFetchAttempts()
146+
{
147+
return [
148+
'Too low, positive' => [0],
149+
'Too low, negative' => [-1],
150+
'Float in range' => [1.9],
138151
];
139152
}
140153

0 commit comments

Comments
 (0)