Tests: use PHPUnit (6.4) exception methods (#165)

* tests: update to PHPUnit 6.0 with rector

* [cs] clean empty docs

* composer: bump to PHPUnit 6.4

* tests: use class references over strings

* cleanup
This commit is contained in:
Tomáš Votruba 2017-11-28 08:00:13 +01:00 committed by Arkadiusz Kondas
parent 726cf4cddf
commit 946fbbc521
24 changed files with 64 additions and 117 deletions

View File

@ -20,7 +20,7 @@
"php": "^7.1"
},
"require-dev": {
"phpunit/phpunit": "^6.0",
"phpunit/phpunit": "^6.4",
"friendsofphp/php-cs-fixer": "^2.4",
"symplify/easy-coding-standard": "dev-master as 2.5",
"symplify/coding-standard": "dev-master as 2.5",

View File

@ -28,9 +28,9 @@ class Imputer implements Preprocessor
private $axis;
/**
* @var
* @var mixed[]
*/
private $samples;
private $samples = [];
/**
* @param mixed $missingValue

View File

@ -178,7 +178,7 @@ class AprioriTest extends TestCase
*
* @return mixed
*/
public function invoke(&$object, $method, array $params = [])
public function invoke(&$object, string $method, array $params = [])
{
$reflection = new ReflectionClass(get_class($object));
$method = $reflection->getMethod($method);

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace tests\Phpml\Classification;
use Phpml\Classification\MLPClassifier;
use Phpml\Exception\InvalidArgumentException;
use Phpml\ModelManager;
use Phpml\NeuralNetwork\Node\Neuron;
use PHPUnit\Framework\TestCase;
@ -160,19 +161,15 @@ class MLPClassifierTest extends TestCase
$this->assertEquals($predicted, $restoredClassifier->predict($testSamples));
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidLayersNumber(): void
{
$this->expectException(InvalidArgumentException::class);
new MLPClassifier(2, [], [0, 1]);
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidPartialTrainingClasses(): void
{
$this->expectException(InvalidArgumentException::class);
$classifier = new MLPClassifier(2, [2], [0, 1]);
$classifier->partialTrain(
[[0, 1], [1, 0]],
@ -181,11 +178,9 @@ class MLPClassifierTest extends TestCase
);
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidClassesNumber(): void
{
$this->expectException(InvalidArgumentException::class);
new MLPClassifier(2, [2], [0]);
}

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace tests\Phpml\Clustering;
use Phpml\Clustering\KMeans;
use Phpml\Exception\InvalidArgumentException;
use PHPUnit\Framework\TestCase;
class KMeansTest extends TestCase
@ -51,11 +52,9 @@ class KMeansTest extends TestCase
$this->assertCount(4, $clusters);
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidClusterNumber(): void
{
$this->expectException(InvalidArgumentException::class);
new KMeans(0);
}
}

View File

@ -6,23 +6,20 @@ namespace tests\Phpml\CrossValidation;
use Phpml\CrossValidation\RandomSplit;
use Phpml\Dataset\ArrayDataset;
use Phpml\Exception\InvalidArgumentException;
use PHPUnit\Framework\TestCase;
class RandomSplitTest extends TestCase
{
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnToSmallTestSize(): void
{
$this->expectException(InvalidArgumentException::class);
new RandomSplit(new ArrayDataset([], []), 0);
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnToBigTestSize(): void
{
$this->expectException(InvalidArgumentException::class);
new RandomSplit(new ArrayDataset([], []), 1);
}

View File

@ -5,15 +5,14 @@ declare(strict_types=1);
namespace tests\Phpml\Dataset;
use Phpml\Dataset\ArrayDataset;
use Phpml\Exception\InvalidArgumentException;
use PHPUnit\Framework\TestCase;
class ArrayDatasetTest extends TestCase
{
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidArgumentsSize(): void
{
$this->expectException(InvalidArgumentException::class);
new ArrayDataset([0, 1], [0]);
}

View File

@ -5,15 +5,14 @@ declare(strict_types=1);
namespace tests\Phpml\Dataset;
use Phpml\Dataset\CsvDataset;
use Phpml\Exception\FileException;
use PHPUnit\Framework\TestCase;
class CsvDatasetTest extends TestCase
{
/**
* @expectedException \Phpml\Exception\FileException
*/
public function testThrowExceptionOnMissingFile(): void
{
$this->expectException(FileException::class);
new CsvDataset('missingFile', 3);
}

View File

@ -5,15 +5,14 @@ declare(strict_types=1);
namespace tests\Phpml\Dataset;
use Phpml\Dataset\FilesDataset;
use Phpml\Exception\DatasetException;
use PHPUnit\Framework\TestCase;
class FilesDatasetTest extends TestCase
{
/**
* @expectedException \Phpml\Exception\DatasetException
*/
public function testThrowExceptionOnMissingRootFolder(): void
{
$this->expectException(DatasetException::class);
new FilesDataset('some/not/existed/path');
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace tests\Phpml\FeatureExtraction;
use Phpml\Exception\InvalidArgumentException;
use Phpml\FeatureExtraction\StopWords;
use PHPUnit\Framework\TestCase;
@ -22,11 +23,9 @@ class StopWordsTest extends TestCase
$this->assertFalse($stopWords->isStopWord('amet'));
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidLanguage(): void
{
$this->expectException(InvalidArgumentException::class);
StopWords::factory('Lorem');
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace tests\Phpml\Math;
use Phpml\Exception\InvalidArgumentException;
use Phpml\Math\Comparison;
use PHPUnit\Framework\TestCase;
@ -22,12 +23,10 @@ class ComparisonTest extends TestCase
$this->assertEquals($expected, $result);
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
* @expectedExceptionMessage Invalid operator "~=" provided
*/
public function testThrowExceptionWhenOperatorIsInvalid(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid operator "~=" provided');
Comparison::compare(1, 1, '~=');
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace tests\Phpml\Metric;
use Phpml\Exception\InvalidArgumentException;
use Phpml\Math\Distance\Chebyshev;
use PHPUnit\Framework\TestCase;
@ -19,14 +20,11 @@ class ChebyshevTest extends TestCase
$this->distanceMetric = new Chebyshev();
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidArguments(): void
{
$this->expectException(InvalidArgumentException::class);
$a = [0, 1, 2];
$b = [0, 2];
$this->distanceMetric->distance($a, $b);
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace tests\Phpml\Metric;
use Phpml\Exception\InvalidArgumentException;
use Phpml\Math\Distance\Euclidean;
use PHPUnit\Framework\TestCase;
@ -19,14 +20,11 @@ class EuclideanTest extends TestCase
$this->distanceMetric = new Euclidean();
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidArguments(): void
{
$this->expectException(InvalidArgumentException::class);
$a = [0, 1, 2];
$b = [0, 2];
$this->distanceMetric->distance($a, $b);
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace tests\Phpml\Metric;
use Phpml\Exception\InvalidArgumentException;
use Phpml\Math\Distance\Manhattan;
use PHPUnit\Framework\TestCase;
@ -19,14 +20,11 @@ class ManhattanTest extends TestCase
$this->distanceMetric = new Manhattan();
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidArguments(): void
{
$this->expectException(InvalidArgumentException::class);
$a = [0, 1, 2];
$b = [0, 2];
$this->distanceMetric->distance($a, $b);
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace tests\Phpml\Metric;
use Phpml\Exception\InvalidArgumentException;
use Phpml\Math\Distance\Minkowski;
use PHPUnit\Framework\TestCase;
@ -19,14 +20,11 @@ class MinkowskiTest extends TestCase
$this->distanceMetric = new Minkowski();
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidArguments(): void
{
$this->expectException(InvalidArgumentException::class);
$a = [0, 1, 2];
$b = [0, 2];
$this->distanceMetric->distance($a, $b);
}

View File

@ -4,16 +4,16 @@ declare(strict_types=1);
namespace tests\Phpml\Math;
use Phpml\Exception\InvalidArgumentException;
use Phpml\Exception\MatrixException;
use Phpml\Math\Matrix;
use PHPUnit\Framework\TestCase;
class MatrixTest extends TestCase
{
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidMatrixSupplied(): void
{
$this->expectException(InvalidArgumentException::class);
new Matrix([[1, 2], [3]]);
}
@ -29,20 +29,16 @@ class MatrixTest extends TestCase
$this->assertEquals($flatArray, $matrix->getColumnValues(0));
}
/**
* @expectedException \Phpml\Exception\MatrixException
*/
public function testThrowExceptionOnInvalidColumnNumber(): void
{
$this->expectException(MatrixException::class);
$matrix = new Matrix([[1, 2, 3], [4, 5, 6]]);
$matrix->getColumnValues(4);
}
/**
* @expectedException \Phpml\Exception\MatrixException
*/
public function testThrowExceptionOnGetDeterminantIfArrayIsNotSquare(): void
{
$this->expectException(MatrixException::class);
$matrix = new Matrix([[1, 2, 3], [4, 5, 6]]);
$matrix->getDeterminant();
}
@ -85,14 +81,11 @@ class MatrixTest extends TestCase
$this->assertEquals($transposedMatrix, $matrix->transpose()->toArray());
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnMultiplyWhenInconsistentMatrixSupplied(): void
{
$this->expectException(InvalidArgumentException::class);
$matrix1 = new Matrix([[1, 2, 3], [4, 5, 6]]);
$matrix2 = new Matrix([[3, 2, 1], [6, 5, 4]]);
$matrix1->multiply($matrix2);
}
@ -132,26 +125,21 @@ class MatrixTest extends TestCase
$this->assertEquals($quotient, $matrix->divideByScalar(2)->toArray());
}
/**
* @expectedException \Phpml\Exception\MatrixException
*/
public function testThrowExceptionWhenInverseIfArrayIsNotSquare(): void
{
$this->expectException(MatrixException::class);
$matrix = new Matrix([[1, 2, 3], [4, 5, 6]]);
$matrix->inverse();
}
/**
* @expectedException \Phpml\Exception\MatrixException
*/
public function testThrowExceptionWhenInverseIfMatrixIsSingular(): void
{
$this->expectException(MatrixException::class);
$matrix = new Matrix([
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
]);
$matrix->inverse();
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace test\Phpml\Math\StandardDeviation;
use Phpml\Exception\InvalidArgumentException;
use Phpml\Math\Statistic\Correlation;
use PHPUnit\Framework\TestCase;
@ -29,11 +30,9 @@ class CorrelationTest extends TestCase
$this->assertEquals(0.911, Correlation::pearson($x, $y), '', $delta);
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidArgumentsForPearsonCorrelation(): void
{
$this->expectException(InvalidArgumentException::class);
Correlation::pearson([1, 2, 4], [3, 5]);
}
}

View File

@ -4,16 +4,15 @@ declare(strict_types=1);
namespace test\Phpml\Math\StandardDeviation;
use Phpml\Exception\InvalidArgumentException;
use Phpml\Math\Statistic\Mean;
use PHPUnit\Framework\TestCase;
class MeanTest extends TestCase
{
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testArithmeticThrowExceptionOnEmptyArray(): void
{
$this->expectException(InvalidArgumentException::class);
Mean::arithmetic([]);
}
@ -25,11 +24,9 @@ class MeanTest extends TestCase
$this->assertEquals(1.7, Mean::arithmetic([0.5, 0.5, 1.5, 2.5, 3.5]), '', $delta);
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testMedianThrowExceptionOnEmptyArray(): void
{
$this->expectException(InvalidArgumentException::class);
Mean::median([]);
}
@ -47,11 +44,9 @@ class MeanTest extends TestCase
$this->assertEquals(3.5, Mean::median($numbers));
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testModeThrowExceptionOnEmptyArray(): void
{
$this->expectException(InvalidArgumentException::class);
Mean::mode([]);
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace test\Phpml\Math\StandardDeviation;
use Phpml\Exception\InvalidArgumentException;
use Phpml\Math\Statistic\StandardDeviation;
use PHPUnit\Framework\TestCase;
@ -25,19 +26,15 @@ class StandardDeviationTest extends TestCase
$this->assertEquals(50989, StandardDeviation::population($population), '', $delta);
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnEmptyArrayIfNotSample(): void
{
$this->expectException(InvalidArgumentException::class);
StandardDeviation::population([], false);
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnToSmallArray(): void
{
$this->expectException(InvalidArgumentException::class);
StandardDeviation::population([1]);
}
}

View File

@ -7,20 +7,18 @@ namespace tests\Phpml\Metric;
use Phpml\Classification\SVC;
use Phpml\CrossValidation\RandomSplit;
use Phpml\Dataset\Demo\IrisDataset;
use Phpml\Exception\InvalidArgumentException;
use Phpml\Metric\Accuracy;
use Phpml\SupportVectorMachine\Kernel;
use PHPUnit\Framework\TestCase;
class AccuracyTest extends TestCase
{
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidArguments(): void
{
$this->expectException(InvalidArgumentException::class);
$actualLabels = ['a', 'b', 'a', 'b'];
$predictedLabels = ['a', 'a'];
Accuracy::score($actualLabels, $predictedLabels);
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace tests;
use Phpml\Exception\FileException;
use Phpml\ModelManager;
use Phpml\Regression\LeastSquares;
use PHPUnit\Framework\TestCase;
@ -23,11 +24,9 @@ class ModelManagerTest extends TestCase
$this->assertEquals($estimator, $restored);
}
/**
* @expectedException \Phpml\Exception\FileException
*/
public function testRestoreWrongFile(): void
{
$this->expectException(FileException::class);
$filepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'unexisting';
$modelManager = new ModelManager();
$modelManager->restoreFromFile($filepath);

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace tests\Phpml\NeuralNetwork;
use Phpml\Exception\InvalidArgumentException;
use Phpml\NeuralNetwork\Layer;
use Phpml\NeuralNetwork\Node\Bias;
use Phpml\NeuralNetwork\Node\Neuron;
@ -39,11 +40,9 @@ class LayerTest extends TestCase
}
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidNodeClass(): void
{
$this->expectException(InvalidArgumentException::class);
new Layer(1, stdClass::class);
}

View File

@ -4,16 +4,15 @@ declare(strict_types=1);
namespace tests\Phpml\Preprocessing;
use Phpml\Exception\NormalizerException;
use Phpml\Preprocessing\Normalizer;
use PHPUnit\Framework\TestCase;
class NormalizerTest extends TestCase
{
/**
* @expectedException \Phpml\Exception\NormalizerException
*/
public function testThrowExceptionOnInvalidNorm(): void
{
$this->expectException(NormalizerException::class);
new Normalizer(99);
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace tests\Phpml\SupportVectorMachine;
use Phpml\Exception\InvalidArgumentException;
use Phpml\SupportVectorMachine\Kernel;
use Phpml\SupportVectorMachine\SupportVectorMachine;
use Phpml\SupportVectorMachine\Type;
@ -81,32 +82,26 @@ SV
$this->assertEquals('c', $predictions[2]);
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
* @expectedExceptionMessage is not writable
*/
public function testThrowExceptionWhenVarPathIsNotWritable(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('is not writable');
$svm = new SupportVectorMachine(Type::C_SVC, Kernel::RBF);
$svm->setVarPath('var-path');
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
* @expectedExceptionMessage does not exist
*/
public function testThrowExceptionWhenBinPathDoesNotExist(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('does not exist');
$svm = new SupportVectorMachine(Type::C_SVC, Kernel::RBF);
$svm->setBinPath('bin-path');
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
* @expectedExceptionMessage not found
*/
public function testThrowExceptionWhenFileIsNotFoundInBinPath(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('not found');
$svm = new SupportVectorMachine(Type::C_SVC, Kernel::RBF);
$svm->setBinPath('var');
}