Skip to content

Instantly share code, notes, and snippets.

View eriktorsner's full-sized avatar

Erik Torsner eriktorsner

  • Stockholm, Sweden
View GitHub Profile
@eriktorsner
eriktorsner / helloTestableTest.php
Last active April 25, 2017 10:13
Snippet from helloTestableTest.php
<?php
public function testEchoLyric()
{
$mockLyric = new \MockLyric('foobar');
$hello = new helloTestable($mockLyric);
$this->expectOutputRegex('/.*foobar*./');
$hello->echoLyric();
}
@eriktorsner
eriktorsner / MockObjects.php
Last active April 25, 2017 10:13
helloTestable MockObjects.php
<?php
class MockLyric
{
public function __construct($lyric)
{
$this->lyric = $lyric;
}
public function getLyric()
@eriktorsner
eriktorsner / helloTestableTest.php
Last active April 25, 2017 10:14
Snippet from helloTestableTest.php
<?php
public function testInit()
{
$dummy = new \stdClass();
$hello = new helloTestable($dummy);
\WP_Mock::expectActionAdded('admin_notices', array($hello, 'echoLyric'));
\WP_Mock::expectActionAdded('admin_head', array($hello, 'echoCss'));
@eriktorsner
eriktorsner / LyricsTest.php
Last active April 25, 2017 10:14
Snippet from LyricsTest.php
<?php
public function testGetLyric()
{
$file = dirname(__DIR__) . '/fixtures/lyrics.txt';
$lyrics = new Lyrics($file);
\WP_Mock::userFunction('wptexturize', array(
'return' => function($s) {
return $s . ' wptexturize';
@eriktorsner
eriktorsner / helloTestable.php
Created April 25, 2017 10:06
Snippet 3 from helloTestable.php
<?php
/**
* Output the lyric string
*/
public function echoLyric()
{
$lyricString = $this->lyrics->getLyric();
echo "<p id='dolly'>$lyricString</p>";
}
@eriktorsner
eriktorsner / helloTestable.php
Created April 25, 2017 10:05
Snippet 2 from helloTestable.php
<?php
/**
* Hook up our plugin with WordPress
*/
public function init()
{
add_action('admin_notices', array($this, 'echoLyric'));
add_action('admin_head', array($this, 'echoCss'));
}
@eriktorsner
eriktorsner / helloTestable.php
Last active April 25, 2017 10:05
Snippet 1 helloTestable.php
<?php
/**
* helloTestable constructor.
*
* @param Lyrics $lyrics
*/
public function __construct($lyrics)
{
$this->lyrics = $lyrics;
@eriktorsner
eriktorsner / Lyrics.php
Created April 25, 2017 10:03
Snippet from Lyrics.php
<?php
/**
* Class Lyrics
* A class to encapsulate lyrics for a song
*
* @package helloTestable
*/
class Lyrics
{
/**
@eriktorsner
eriktorsner / RuntimeProvider.php
Last active December 27, 2017 10:06
Snippet from RuntimeProvider.php
<?php
/**
* Class RuntimeProvider
*
* Use Pimple as dependency injection container
*
*/
class RuntimeProvider implements ServiceProviderInterface
{
/**
@eriktorsner
eriktorsner / hello-bootstrap.php
Last active April 25, 2017 23:59
Snippet from hello-bootstrap.php
<?php
/**
* The main plugin function. Checks php version
* and initialize our classes
*/
function helloTestableBootstrap()
{
$pluginVersion = '0.1.0';
if (defined('DOING_AJAX') && DOING_AJAX) {
return;