Skip to content

Instantly share code, notes, and snippets.

@jeremyharris
Created November 8, 2016 21:30
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 jeremyharris/99a0b59e8df57846a18de6f710f727c7 to your computer and use it in GitHub Desktop.
Save jeremyharris/99a0b59e8df57846a18de6f710f727c7 to your computer and use it in GitHub Desktop.
<?php
class CallbackCaller
{
public function on($callback)
{
// whoops doesn't fire, perhaps due to code error
}
}
class AssertionTest extends \PHPUnit_Framework_TestCase
{
public function testAssertionInCallback()
{
$class = new CallbackCaller();
$class->on(function() {
$this->assertTrue(false);
});
$this->assertTrue(true);
}
}
@dogmatic69
Copy link

dogmatic69 commented Nov 8, 2016

<?php
class CallbackCaller
{
	public function on($callback)
	{
            $this->_callback = $callback;
	}

        public function run() {
            // whoops doesn't fire, perhaps due to code error
            $callback = $this->_callback;
            return $callback();
        }
}
class AssertionTest extends \PHPUnit_Framework_TestCase
{
	public function testAssertionInCallback()
	{
                $localVar = false;

		$class = new CallbackCaller();
		$class->on(function() use ($localVar) {
			$localVar = true;
		});

		// run code that should run the callback
		$class->run();

		$this->assertTrue($localVar);
	}
}

@jeremyharris
Copy link
Author

Right! This is a possible solution to the initial poorly written test.

@dogmatic69
Copy link

Not sure any better ways, always done callback checks like that

your test should just be checking its called, nothing else. Should be a mocked callback that only sets the variable.

Another test should test what the callback actually does IRL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment