Completed
Push — master ( 4da088...efd6ae )
by mw
39:06 queued 04:09
created

servicesProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 38
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\Tests\Services;
4
5
use Onoi\CallbackContainer\CallbackContainerFactory;
6
7
/**
8
 * @group semantic-mediawiki
9
 *
10
 * @license GNU GPL v2+
11
 * @since 2.5
12
 *
13
 * @author mwjames
14
 */
15
class MediaWikiServicesContainerBuildTest extends \PHPUnit_Framework_TestCase {
16
17
	private $callbackContainerFactory;
18
	private $servicesFileDir;
19
20
	protected function setUp() {
0 ignored issues
show
Coding Style introduced by
setUp uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
21
		parent::setUp();
22
23
		$this->callbackContainerFactory = new CallbackContainerFactory();
24
		$this->servicesFileDir = $GLOBALS['smwgServicesFileDir'];
25
	}
26
27
	/**
28
	 * @dataProvider servicesProvider
29
	 */
30
	public function testCanConstruct( $service, $parameters, $expected ) {
31
32
		array_unshift( $parameters, $service );
33
34
		$containerBuilder = $this->callbackContainerFactory->newCallbackContainerBuilder();
35
		$containerBuilder->registerFromFile( $this->servicesFileDir . '/' . 'MediaWikiServices.php' );
36
37
		$this->assertInstanceOf(
38
			$expected,
39
			call_user_func_array( array( $containerBuilder, 'create' ), $parameters )
40
		);
41
	}
42
43
	public function servicesProvider() {
44
45
		$title = $this->getMockBuilder( '\Title' )
46
			->disableOriginalConstructor()
47
			->getMock();
48
49
		$provider[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$provider was never initialized. Although not strictly required by PHP, it is generally a good practice to add $provider = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
50
			'WikiPage',
51
			array( $title ),
52
			'\WikiPage'
53
		);
54
55
		$provider[] = array(
56
			'DBLoadBalancer',
57
			array(),
58
			'\LoadBalancer'
59
		);
60
61
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
62
		$database = $this->getMockBuilder( '\DatabaeBase' )
63
			->disableOriginalConstructor()
64
			->getMock();
65
66
		$provider[] = array(
67
			'DefaultSearchEngineTypeForDB',
68
			array( $database ),
69
			'\SearchEngine'
70
		);
71
*/
72
73
		$provider[] = array(
74
			'MediaWikiLogger',
75
			array(),
76
			'\Psr\Log\LoggerInterface'
77
		);
78
79
		return $provider;
80
	}
81
}
82