fwolf /
fwlib
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | namespace FwlibTest\Test\Benchmark; |
||
| 3 | |||
| 4 | use Fwlib\Test\Benchmark\Benchmark; |
||
| 5 | use Fwlib\Test\Benchmark\Group; |
||
| 6 | use Fwlib\Test\Benchmark\Marker; |
||
| 7 | use Fwlib\Test\Benchmark\RendererInterface; |
||
| 8 | use Fwolf\Wrapper\PHPUnit\PHPUnitTestCase; |
||
| 9 | use PHPUnit_Framework_MockObject_MockObject as MockObject; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @copyright Copyright 2015 Fwolf |
||
| 13 | * @license http://www.gnu.org/licenses/lgpl.html LGPL-3.0+ |
||
| 14 | */ |
||
| 15 | class BenchmarkTest extends PHPUnitTestCase |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @param string[] $methods |
||
| 19 | * @return MockObject|Benchmark |
||
| 20 | */ |
||
| 21 | protected function buildMock(array $methods = null) |
||
| 22 | { |
||
| 23 | $mock = $this->getMock( |
||
| 24 | Benchmark::class, |
||
| 25 | $methods |
||
| 26 | ); |
||
| 27 | |||
| 28 | return $mock; |
||
| 29 | } |
||
| 30 | |||
| 31 | |||
| 32 | View Code Duplication | public function testAutoStop() |
|
|
0 ignored issues
–
show
|
|||
| 33 | { |
||
| 34 | $benchmark = $this->buildMock(['getCurrentGroup', 'stop']); |
||
| 35 | |||
| 36 | $group = (new Group(0))->setBeginTime(42); |
||
| 37 | $benchmark->expects($this->once()) |
||
|
0 ignored issues
–
show
The method
expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Fwlib\Test\Benchmark\Benchmark.
It seems like the method you are trying to call exists only in some of the possible types. Let’s take a look at an example: class A
{
public function foo() { }
}
class B extends A
{
public function bar() { }
}
/**
* @param A|B $x
*/
function someFunction($x)
{
$x->foo(); // This call is fine as the method exists in A and B.
$x->bar(); // This method only exists in B and might cause an error.
}
Available Fixes
Loading history...
|
|||
| 38 | ->method('getCurrentGroup') |
||
| 39 | ->willReturn($group); |
||
| 40 | |||
| 41 | $benchmark->expects($this->once()) |
||
| 42 | ->method('stop'); |
||
| 43 | |||
| 44 | |||
| 45 | $this->reflectionCall($benchmark, 'autoStop'); |
||
| 46 | } |
||
| 47 | |||
| 48 | |||
| 49 | public function testDisplay() |
||
| 50 | { |
||
| 51 | $benchmark = $this->buildMock(['getOutput']); |
||
| 52 | $benchmark->expects($this->once()) |
||
|
0 ignored issues
–
show
The method
expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Fwlib\Test\Benchmark\Benchmark.
It seems like the method you are trying to call exists only in some of the possible types. Let’s take a look at an example: class A
{
public function foo() { }
}
class B extends A
{
public function bar() { }
}
/**
* @param A|B $x
*/
function someFunction($x)
{
$x->foo(); // This call is fine as the method exists in A and B.
$x->bar(); // This method only exists in B and might cause an error.
}
Available Fixes
Loading history...
|
|||
| 53 | ->method('getOutput') |
||
| 54 | ->willReturn('dummy output'); |
||
| 55 | |||
| 56 | $this->expectOutputString('dummy output'); |
||
| 57 | $benchmark->display(); |
||
|
0 ignored issues
–
show
The method
display does only exist in Fwlib\Test\Benchmark\Benchmark, but not in PHPUnit_Framework_MockObject_MockObject.
It seems like the method you are trying to call exists only in some of the possible types. Let’s take a look at an example: class A
{
public function foo() { }
}
class B extends A
{
public function bar() { }
}
/**
* @param A|B $x
*/
function someFunction($x)
{
$x->foo(); // This call is fine as the method exists in A and B.
$x->bar(); // This method only exists in B and might cause an error.
}
Available Fixes
Loading history...
|
|||
| 58 | } |
||
| 59 | |||
| 60 | |||
| 61 | public function testGetCurrentGroup() |
||
| 62 | { |
||
| 63 | $group = new Group(3); |
||
| 64 | $benchmark = $this->buildMock(); |
||
| 65 | $this->reflectionSet($benchmark, 'groups', [3 => $group]); |
||
| 66 | |||
| 67 | $this->reflectionSet($benchmark, 'groupId', 1); |
||
| 68 | $this->assertNull($this->reflectionCall($benchmark, 'getCurrentGroup')); |
||
| 69 | |||
| 70 | $this->reflectionSet($benchmark, 'groupId', 3); |
||
| 71 | $this->assertInstanceOf( |
||
| 72 | Group::class, |
||
| 73 | $this->reflectionCall($benchmark, 'getCurrentGroup') |
||
| 74 | ); |
||
| 75 | } |
||
| 76 | |||
| 77 | |||
| 78 | public function testGetOutput() |
||
| 79 | { |
||
| 80 | $benchmark = $this->buildMock(['autoStop']); |
||
| 81 | $benchmark->expects($this->once()) |
||
|
0 ignored issues
–
show
The method
expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Fwlib\Test\Benchmark\Benchmark.
It seems like the method you are trying to call exists only in some of the possible types. Let’s take a look at an example: class A
{
public function foo() { }
}
class B extends A
{
public function bar() { }
}
/**
* @param A|B $x
*/
function someFunction($x)
{
$x->foo(); // This call is fine as the method exists in A and B.
$x->bar(); // This method only exists in B and might cause an error.
}
Available Fixes
Loading history...
|
|||
| 82 | ->method('autoStop'); |
||
| 83 | |||
| 84 | $benchmark->getOutput(); |
||
|
0 ignored issues
–
show
The method
getOutput does only exist in Fwlib\Test\Benchmark\Benchmark, but not in PHPUnit_Framework_MockObject_MockObject.
It seems like the method you are trying to call exists only in some of the possible types. Let’s take a look at an example: class A
{
public function foo() { }
}
class B extends A
{
public function bar() { }
}
/**
* @param A|B $x
*/
function someFunction($x)
{
$x->foo(); // This call is fine as the method exists in A and B.
$x->bar(); // This method only exists in B and might cause an error.
}
Available Fixes
Loading history...
|
|||
| 85 | } |
||
| 86 | |||
| 87 | |||
| 88 | public function testGetTime() |
||
| 89 | { |
||
| 90 | $benchmark = $this->buildMock(); |
||
| 91 | |||
| 92 | $time = $this->reflectionCall($benchmark, 'getTime'); |
||
| 93 | // Convert float to string with strval() will loose precision |
||
| 94 | $timeStr = sprintf('%f', $time); |
||
| 95 | |||
| 96 | $this->assertRegExp('/\d+\.\d{3,}/', $timeStr); |
||
| 97 | } |
||
| 98 | |||
| 99 | |||
| 100 | public function testMark() |
||
| 101 | { |
||
| 102 | $benchmark = $this->buildMock(); |
||
| 103 | |||
| 104 | $benchmark->start(); |
||
|
0 ignored issues
–
show
The method
start does only exist in Fwlib\Test\Benchmark\Benchmark, but not in PHPUnit_Framework_MockObject_MockObject.
It seems like the method you are trying to call exists only in some of the possible types. Let’s take a look at an example: class A
{
public function foo() { }
}
class B extends A
{
public function bar() { }
}
/**
* @param A|B $x
*/
function someFunction($x)
{
$x->foo(); // This call is fine as the method exists in A and B.
$x->bar(); // This method only exists in B and might cause an error.
}
Available Fixes
Loading history...
|
|||
| 105 | $benchmark->mark('m1'); |
||
|
0 ignored issues
–
show
The method
mark does only exist in Fwlib\Test\Benchmark\Benchmark, but not in PHPUnit_Framework_MockObject_MockObject.
It seems like the method you are trying to call exists only in some of the possible types. Let’s take a look at an example: class A
{
public function foo() { }
}
class B extends A
{
public function bar() { }
}
/**
* @param A|B $x
*/
function someFunction($x)
{
$x->foo(); // This call is fine as the method exists in A and B.
$x->bar(); // This method only exists in B and might cause an error.
}
Available Fixes
Loading history...
|
|||
| 106 | $benchmark->mark('', 'red'); |
||
| 107 | |||
| 108 | $markerId = $this->reflectionGet($benchmark, 'markerId'); |
||
| 109 | $this->assertEquals(2, $markerId); |
||
| 110 | |||
| 111 | // When stop, marker id is reset |
||
| 112 | $benchmark->stop(); |
||
|
0 ignored issues
–
show
The method
stop does only exist in Fwlib\Test\Benchmark\Benchmark, but not in PHPUnit_Framework_MockObject_MockObject.
It seems like the method you are trying to call exists only in some of the possible types. Let’s take a look at an example: class A
{
public function foo() { }
}
class B extends A
{
public function bar() { }
}
/**
* @param A|B $x
*/
function someFunction($x)
{
$x->foo(); // This call is fine as the method exists in A and B.
$x->bar(); // This method only exists in B and might cause an error.
}
Available Fixes
Loading history...
|
|||
| 113 | $markerId = $this->reflectionGet($benchmark, 'markerId'); |
||
| 114 | $this->assertEquals(0, $markerId); |
||
| 115 | |||
| 116 | /** @var Marker[] $markers */ |
||
| 117 | $markers = $this->reflectionGet($benchmark, 'markers')[0]; |
||
| 118 | $marker0 = $markers[0]; |
||
| 119 | $marker1 = $markers[1]; |
||
| 120 | $this->assertEquals('m1', $marker0->getDescription()); |
||
| 121 | $this->assertNotEmpty($marker1->getDescription()); |
||
| 122 | $this->assertEquals('red', $marker1->getColor()); |
||
| 123 | } |
||
| 124 | |||
| 125 | |||
| 126 | public function testSetGetRenderer() |
||
| 127 | { |
||
| 128 | $benchmark = $this->buildMock(); |
||
| 129 | |||
| 130 | /** @var MockObject|RendererInterface $renderer */ |
||
| 131 | $renderer = $this->getMockBuilder(RendererInterface::class) |
||
| 132 | ->getMockForAbstractClass(); |
||
| 133 | |||
| 134 | $benchmark->setRenderer($renderer); |
||
|
0 ignored issues
–
show
It seems like
$renderer defined by $this->getMockBuilder(\F...tMockForAbstractClass() on line 131 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Fwlib\Test\Benchmark\Benchmark::setRenderer() does only seem to accept object<Fwlib\Test\Benchmark\RendererInterface>, maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue. Loading history...
The method
setRenderer does only exist in Fwlib\Test\Benchmark\Benchmark, but not in PHPUnit_Framework_MockObject_MockObject.
It seems like the method you are trying to call exists only in some of the possible types. Let’s take a look at an example: class A
{
public function foo() { }
}
class B extends A
{
public function bar() { }
}
/**
* @param A|B $x
*/
function someFunction($x)
{
$x->foo(); // This call is fine as the method exists in A and B.
$x->bar(); // This method only exists in B and might cause an error.
}
Available Fixes
Loading history...
|
|||
| 135 | $newRenderer = $this->reflectionCall($benchmark, 'getRenderer'); |
||
| 136 | |||
| 137 | $this->assertInstanceOf(RendererInterface::class, $newRenderer); |
||
| 138 | } |
||
| 139 | } |
||
| 140 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.