Skip to content

Commit

Permalink
Introduced Runner\Test
Browse files Browse the repository at this point in the history
  • Loading branch information
milo committed May 2, 2017
1 parent b35f213 commit a34637b
Show file tree
Hide file tree
Showing 2 changed files with 213 additions and 0 deletions.
149 changes: 149 additions & 0 deletions src/Runner/Test.php
@@ -0,0 +1,149 @@
<?php

/**
* This file is part of the Nette Tester.
* Copyright (c) 2009 David Grudl (https://davidgrudl.com)
*/

namespace Tester\Runner;


/**
* Test represents one result.
*/
class Test
{
const
PREPARED = 0,
PASSED = 1,
SKIPPED = 2,
FAILED = 3;

/** @var string|NULL */
public $title;

/** @var string|NULL */
public $message;

/** @var string */
public $stdout = '';

/** @var string */
public $stderr = '';

/** @var string */
private $file;

/** @var int */
private $result = self::PREPARED;

/** @var string[]|string[][] */
private $args = [];


/**
* @param string
* @param string
*/
public function __construct($file, $title = NULL)
{
$this->file = $file;
$this->title = $title;
}


/**
* @return string
*/
public function getFile()
{
return $this->file;
}


/**
* @return string[]|string[][]
*/
public function getArguments()
{
return $this->args;
}


/**
* @param int
* @return string
*/
public function getName($maxArgsLen = NULL)
{
$name = $this->title ?: basename($this->file);
$args = implode(' ', array_map(function ($arg) {
return is_array($arg) ? "$arg[0]=$arg[1]" : $arg;
}, $this->args));

if ($args && $maxArgsLen !== NULL && $maxArgsLen < strlen(" [$args]")) {
$args = substr($args, 0, max(0, $maxArgsLen - 6)) . '...';
}

return $name . ($args ? " [$args]" : '');
}


/**
* @return int
*/
public function getResult()
{
return $this->result;
}


/**
* @return bool
*/
public function hasResult()
{
return $this->result !== self::PREPARED;
}


/**
* @param array $args
* @return static
*/
public function withArguments(array $args)
{
if ($this->hasResult()) {
throw new \LogicException('Cannot change arguments of test which already has a result.');
}

$me = clone $this;
foreach ($args as $name => $values) {
foreach ((array) $values as $value) {
$me->args[] = is_int($name)
? "$value"
: [$name, "$value"];
}
}
return $me;
}


/**
* @param int
* @param string
* @return static
*/
public function withResult($result, $message)
{
if ($this->hasResult()) {
throw new \LogicException("Result of test is already set to $this->result with message '$this->message'.");
}

$me = clone $this;
$me->result = $result;
$me->message = $message;
return $me;
}

}
64 changes: 64 additions & 0 deletions tests/Runner/Test.phpt
@@ -0,0 +1,64 @@
<?php

/**
* TEST: Runner\Test basics.
*/

use Tester\Assert;
use Tester\Runner\Test;

require __DIR__ . '/../../src/Runner/Test.php';
require __DIR__ . '/../bootstrap.php';


test(function() {
$test = new Test('some/Test.phpt');

Assert::null($test->title);
Assert::null($test->message);
Assert::same('', $test->stdout);
Assert::same('', $test->stderr);
Assert::same('some/Test.phpt', $test->getFile());
Assert::same([], $test->getArguments());
Assert::same('Test.phpt', $test->getName());
Assert::false($test->hasResult());
Assert::same(Test::PREPARED, $test->getResult());
});


test(function() {
$test = new Test(__FILE__, 'My test');

Assert::same('My test', $test->title);
Assert::same('My test', $test->getName());
});


test(function() {
$test = (new Test(__FILE__, 'My test'))->withResult(Test::PASSED, 'It is done');

Assert::true($test->hasResult());
Assert::same(Test::PASSED, $test->getResult());
Assert::same('It is done', $test->message);

Assert::exception(function () use ($test) {
$test->withResult(Test::FAILED, 'Foo');
}, 'LogicException', "Result of test is already set to 1 with message 'It is done'.");
});


test(function() {
$test = new Test(__FILE__, 'My test');

$test = $test->withArguments(['one', 'two' => 1]);
Assert::same('My test', $test->title);
Assert::same('My test [one two=1]', $test->getName());

$test = $test->withArguments(['one', 'two' => [1, 2], 'three']);
Assert::same('My test [one two=1 one two=1 two=2 three]', $test->getName());
Assert::same('My test [one ...]', $test->getName(10));

Assert::exception(function () use ($test) {
$test->withResult(Test::PASSED, '')->withArguments([]);
}, 'LogicException', 'Cannot change arguments of test which already has a result.');
});

0 comments on commit a34637b

Please sign in to comment.