Skip to content

Commit 30570b7

Browse files
committed
0.2.0 - 2016-08-28
- ADD ReflectionTestable , ExceptionTestable, FileSystemTestable Traits - ADD support for php>=7.0.0 - REMOVE support for php<7.0 - CHANGE TestBase now use ReflectionTestable , ExceptionTestable, FileSystemTestable traits by default.
1 parent 4011cea commit 30570b7

5 files changed

+142
-27
lines changed

.travis.yml

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
language: php
22

33
php:
4-
- 5.4
5-
- 5.5
6-
- 5.6
74
- 7.0
8-
- hhvm
5+
- 7.1
96

107
# This triggers builds to run on the new TravisCI infrastructure.
118
# See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/
129
sudo: false
1310

1411
matrix:
15-
allow_failures:
16-
- php: 7.0
17-
include:
18-
- php: 5.4
19-
env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"'
12+
- COMPOSER_FLAGS=""
2013

2114
before_script:
2215
- travis_retry composer self-update

src/TestBase.php

+6-18
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
namespace Padosoft\Test;
44

5+
use Padosoft\Test\traits\ExceptionTestable;
6+
use Padosoft\Test\traits\FileSystemTestable;
7+
use Padosoft\Test\traits\ReflectionTestable;
8+
59
/**
610
* User: Lore
711
* Date: 05/12/2015
812
* Time: 12:38
913
*/
1014
class TestBase extends \PHPUnit_Framework_TestCase
1115
{
16+
use ReflectionTestable , ExceptionTestable, FileSystemTestable;
17+
1218
protected function setUp()
1319
{
1420

@@ -18,22 +24,4 @@ protected function tearDown()
1824
{
1925

2026
}
21-
22-
/**
23-
* Call protected/private method of a class.
24-
*
25-
* @param object &$object Instantiated object that we will run method on.
26-
* @param string $methodName Method name to call
27-
* @param array $parameters Array of parameters to pass into method.
28-
*
29-
* @return mixed Method return.
30-
*/
31-
protected function invokeMethod(&$object, $methodName, array $parameters = array())
32-
{
33-
$reflection = new \ReflectionClass(get_class($object));
34-
$method = $reflection->getMethod($methodName);
35-
$method->setAccessible(true);
36-
37-
return $method->invokeArgs($object, $parameters);
38-
}
3927
}

src/traits/ExceptionTestable.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Padosoft\Test\traits;
4+
5+
trait ExceptionTestable
6+
{
7+
/**
8+
* @param $expected
9+
* @return bool
10+
*/
11+
protected function expectedIsAnException($expected) : bool
12+
{
13+
if (is_array($expected)) {
14+
return false;
15+
}
16+
17+
return strpos($expected, 'Exception') !== false
18+
|| strpos($expected, 'PHPUnit_Framework_') !== false
19+
|| strpos($expected, 'TypeError') !== false
20+
|| strpos($expected, 'SoapFault') !== false;
21+
}
22+
}

src/traits/FileSystemTestable.php

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Padosoft\Test\traits;
4+
5+
trait FileSystemTestable
6+
{
7+
/**
8+
* Remove created path during test
9+
*/
10+
protected function initFileAndPath()
11+
{
12+
$dir = __DIR__ . '/resources/';
13+
if (!is_dir($dir)) {
14+
mkdir($dir, '0777', true);
15+
}
16+
17+
$file = $dir . 'dummy.txt';
18+
if (!file_exists($file)) {
19+
file_put_contents($file, 'dummy');
20+
}
21+
22+
$file = $dir . 'dummy.csv';
23+
if (!file_exists($file)) {
24+
file_put_contents($file, 'dummy;');
25+
}
26+
27+
$dir = __DIR__ . '/resources/subdir/';
28+
if (!is_dir($dir)) {
29+
mkdir($dir, '0777', true);
30+
}
31+
32+
$file = $dir . 'dummy.txt';
33+
if (!file_exists($file)) {
34+
file_put_contents($file, 'dummy');
35+
}
36+
}
37+
38+
/**
39+
* Remove created path during test
40+
*/
41+
protected function removeCreatedPathDuringTest()
42+
{
43+
if (is_dir(__DIR__ . '/pippo.txt')) {
44+
rmdir(__DIR__ . '/pippo.txt');
45+
}
46+
if (is_dir(__DIR__ . '/dummy/')) {
47+
rmdir(__DIR__ . '/dummy/');
48+
}
49+
if (file_exists(__DIR__ . '/resources/new/dummy.txt')) {
50+
unlink(__DIR__ . '/resources/new/dummy.txt');
51+
}
52+
if (is_dir(__DIR__ . '/resources/new/')) {
53+
rmdir(__DIR__ . '/resources/new/');
54+
}
55+
if (file_exists(__DIR__ . '/resources/new2/dummy.txt')) {
56+
unlink(__DIR__ . '/resources/new2/dummy.txt');
57+
}
58+
if (is_dir(__DIR__ . '/resources/new2/')) {
59+
rmdir(__DIR__ . '/resources/new2/');
60+
}
61+
62+
/*
63+
$dir = __DIR__ . '/resources/subdir/';
64+
65+
$file = $dir . 'dummy.txt';
66+
if (file_exists($file)) {
67+
unlink($file);
68+
}
69+
if (is_dir($dir)) {
70+
rmdir($dir);
71+
}
72+
73+
$dir = __DIR__ . '/resources/';
74+
75+
$file = $dir . 'dummy.txt';
76+
if (file_exists($file)) {
77+
unlink($file);
78+
}
79+
$file = $dir . 'dummy.csv';
80+
if (file_exists($file)) {
81+
unlink($file);
82+
}
83+
if (is_dir($dir)) {
84+
rmdir($dir);
85+
}
86+
*/
87+
}
88+
}

src/traits/ReflectionTestable.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Padosoft\Test\traits;
4+
5+
trait ReflectionTestable
6+
{
7+
/**
8+
* Call protected/private method of a class.
9+
*
10+
* @param object &$object Instantiated object that we will run method on.
11+
* @param string $methodName Method name to call
12+
* @param array $parameters Array of parameters to pass into method.
13+
*
14+
* @return mixed Method return.
15+
*/
16+
public function invokeMethod(&$object, $methodName, array $parameters = array())
17+
{
18+
$reflection = new \ReflectionClass(get_class($object));
19+
$method = $reflection->getMethod($methodName);
20+
$method->setAccessible(true);
21+
22+
return $method->invokeArgs($object, $parameters);
23+
}
24+
}

0 commit comments

Comments
 (0)