Skip to content

Commit

Permalink
Reorganize
Browse files Browse the repository at this point in the history
  • Loading branch information
jedrzejchalubek committed Mar 14, 2017
1 parent c9bb86b commit 0948490
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 27 deletions.
10 changes: 10 additions & 0 deletions src/Gin/Foundation/Testing/MissingPropertyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Tonik\Gin\Foundation\Testing;

use Exception;

class MissingPropertyException extends Exception
{
//
}
53 changes: 46 additions & 7 deletions src/Gin/Foundation/Testing/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@
abstract class TestCase extends PHPUnit_Framework_TestCase
{
/**
* Path of file to autoload.
* Theme instance.
*
* @var string
* @var \Tonik\Gin\Foundation\Theme
*/
protected $file;
protected $theme;

/**
* Creates the Theme. Implemented by subclasses.
*
* @return \Tonik\Gin\Foundation\Theme
*/
abstract public function createTheme();

/**
* Setup before each test.
Expand All @@ -23,12 +30,19 @@ abstract class TestCase extends PHPUnit_Framework_TestCase
*/
protected function setUp()
{
$this->theme = $this->createTheme();

if (! isset($this->file)) {
throw new Exception('You have to define file to be autoloaded for tests.');
throw new MissingPropertyException('You have to define [$file] file path to be autoloaded for tests.');
}

if (! isset($this->dir)) {
throw new MissingPropertyException('You have to define [$dir] tests directory.');
}

$this->requireSrcFile($this->file);

parent::setUp();
Monkey::setUpWP();
}

/**
Expand All @@ -38,8 +52,33 @@ protected function setUp()
*/
protected function tearDown()
{
Monkey::tearDownWP();
Mockery::close();
$this->theme = null;

if (class_exists('Mockery')) {
Mockery::close();
}

parent::tearDown();
}

/**
* Require theme source file.
*
* @param string $file
* @return mixed
*/
protected function requireSrcFile($file)
{
require_once $this->dir . '/src/' . $file;
}

/**
* Gets the Theme instance.
*
* @return \Tonik\Gin\Foundation\Theme
*/
public function getTheme()
{
return $this->theme;
}
}
45 changes: 45 additions & 0 deletions tests/Gin/Foundation/Testing/InvalidCaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

use Tonik\Gin\Foundation\Testing\MissingPropertyException;
use Tonik\Gin\Foundation\Testing\TestCase as BaseTestCase;
use Tonik\Gin\Foundation\Theme;

class InvalidCaseTest extends PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function it_throw_when_directory_is_not_set() {
$test = new InvalidCase;

$test->dir = null;

$this->expectException(MissingPropertyException::class);

$test->setUp();
}

/**
* @test
*/
public function it_throw_when_file_is_not_set() {
$test = new InvalidCase;

$test->file = null;

$this->expectException(MissingPropertyException::class);

$test->setUp();
}
}

class InvalidCase extends BaseTestCase
{
public $file = 'file';
public $dir = 'dir';

public function createTheme()
{
//
}
}
20 changes: 0 additions & 20 deletions tests/Gin/Foundation/Testing/TestCaseTest.php

This file was deleted.

56 changes: 56 additions & 0 deletions tests/Gin/Foundation/Testing/ValidCaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use Tonik\Gin\Foundation\Testing\TestCase as BaseTestCase;
use Tonik\Gin\Foundation\Theme;

class ValidCaseTest extends PHPUnit_Framework_TestCase
{
public function setUp() {
$this->test = new ValidCase;

$this->test->setUp();
}

public function tearDown() {
$this->test->tearDown();
}

/**
* @test
*/
public function it_throw_when_directory_is_not_set() {
$this->assertEquals($this->test->dir, __DIR__);
$this->assertEquals($this->test->file, 'fixtures/file_to_autoload.php');
$this->assertInstanceOf(Theme::class, $this->test->getTheme());
}

/**
* @test
*/
public function it_should_heve_been_setted_up() {
$this->assertEquals($this->test->dir, __DIR__);
$this->assertEquals($this->test->file, 'fixtures/file_to_autoload.php');
$this->assertInstanceOf(Theme::class, $this->test->getTheme());
}

/**
* @test
*/
public function it_should_autoload_tested_source_file() {
$this->assertTrue(function_exists('autoloaded_function'));
$this->assertEquals('value', autoloaded_function('value'));
}
}

class ValidCase extends BaseTestCase
{
public $file = 'fixtures/file_to_autoload.php';
public $dir;

public function createTheme()
{
$this->dir = __DIR__;

return Theme::getInstance();
}
}

0 comments on commit 0948490

Please sign in to comment.