Skip to content

Instantly share code, notes, and snippets.

@stojg
Last active December 27, 2015 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stojg/7344445 to your computer and use it in GitHub Desktop.
Save stojg/7344445 to your computer and use it in GitHub Desktop.
An example on how to test a static function call in SilverStripe using the Injection system, see discussion at https://groups.google.com/forum/#!topic/silverstripe-dev/f9agsTc3iJo
<?php
class MockStaticTest extends FunctionalTest {
function foo() {
// Since Director doesnt extends Object, you have to do it like this, otherwise
// Director::create() should return an Injected Director
$director = Injector::inst()->get('Director');
$director::forceSSL(array('/^Security/', '/^api/'));
}
function testRedirectsSSL() {
$stubDirector = $this->getMock('Director', array('forceSSL'));
Injector::inst()->registerNamedService('Director', new $stubDirector);
// Ensure that forceSSL was called once with an array as arguments
$stubDirector::staticExpects($this->once())
->method('forceSSL')
->with($this->equalTo(array('/^Security/')));
// Exercise the method
$this->foo();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment