Skip to content

Commit

Permalink
Tests: use PHPUnit (6.4) exception methods (#165)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Tomáš Votruba authored and akondas committed Nov 28, 2017
1 parent 726cf4c commit 946fbbc
Show file tree
Hide file tree
Showing 24 changed files with 64 additions and 117 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/Phpml/Preprocessing/Imputer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class Imputer implements Preprocessor
private $axis;

/**
* @var
* @var mixed[]
*/
private $samples;
private $samples = [];

/**
* @param mixed $missingValue
Expand Down
2 changes: 1 addition & 1 deletion tests/Phpml/Association/AprioriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function testEquals(): void
*
* @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);
Expand Down
13 changes: 4 additions & 9 deletions tests/Phpml/Classification/MLPClassifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace tests\Phpml\Classification;

use Phpml\Classification\MLPClassifier;
use Phpml\Exception\InvalidArgumentException;
use Phpml\ModelManager;
use Phpml\NeuralNetwork\Node\Neuron;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -160,19 +161,15 @@ public function testSaveAndRestore(): void
$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]],
Expand All @@ -181,11 +178,9 @@ public function testThrowExceptionOnInvalidPartialTrainingClasses(): void
);
}

/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidClassesNumber(): void
{
$this->expectException(InvalidArgumentException::class);
new MLPClassifier(2, [2], [0]);
}

Expand Down
5 changes: 2 additions & 3 deletions tests/Phpml/Clustering/KMeansTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace tests\Phpml\Clustering;

use Phpml\Clustering\KMeans;
use Phpml\Exception\InvalidArgumentException;
use PHPUnit\Framework\TestCase;

class KMeansTest extends TestCase
Expand Down Expand Up @@ -51,11 +52,9 @@ public function testKMeansInitializationMethods(): void
$this->assertCount(4, $clusters);
}

/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidClusterNumber(): void
{
$this->expectException(InvalidArgumentException::class);
new KMeans(0);
}
}
9 changes: 3 additions & 6 deletions tests/Phpml/CrossValidation/RandomSplitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,20 @@

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);
}

Expand Down
5 changes: 2 additions & 3 deletions tests/Phpml/Dataset/ArrayDatasetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
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]);
}

Expand Down
5 changes: 2 additions & 3 deletions tests/Phpml/Dataset/CsvDatasetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
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);
}

Expand Down
5 changes: 2 additions & 3 deletions tests/Phpml/Dataset/FilesDatasetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
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');
}

Expand Down
5 changes: 2 additions & 3 deletions tests/Phpml/FeatureExtraction/StopWordsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace tests\Phpml\FeatureExtraction;

use Phpml\Exception\InvalidArgumentException;
use Phpml\FeatureExtraction\StopWords;
use PHPUnit\Framework\TestCase;

Expand All @@ -22,11 +23,9 @@ public function testCustomStopWords(): void
$this->assertFalse($stopWords->isStopWord('amet'));
}

/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidLanguage(): void
{
$this->expectException(InvalidArgumentException::class);
StopWords::factory('Lorem');
}

Expand Down
7 changes: 3 additions & 4 deletions tests/Phpml/Math/ComparisonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace tests\Phpml\Math;

use Phpml\Exception\InvalidArgumentException;
use Phpml\Math\Comparison;
use PHPUnit\Framework\TestCase;

Expand All @@ -22,12 +23,10 @@ public function testResult($a, $b, string $operator, bool $expected): void
$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, '~=');
}

Expand Down
6 changes: 2 additions & 4 deletions tests/Phpml/Math/Distance/ChebyshevTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace tests\Phpml\Metric;

use Phpml\Exception\InvalidArgumentException;
use Phpml\Math\Distance\Chebyshev;
use PHPUnit\Framework\TestCase;

Expand All @@ -19,14 +20,11 @@ public function setUp(): void
$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);
}

Expand Down
6 changes: 2 additions & 4 deletions tests/Phpml/Math/Distance/EuclideanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace tests\Phpml\Metric;

use Phpml\Exception\InvalidArgumentException;
use Phpml\Math\Distance\Euclidean;
use PHPUnit\Framework\TestCase;

Expand All @@ -19,14 +20,11 @@ public function setUp(): void
$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);
}

Expand Down
6 changes: 2 additions & 4 deletions tests/Phpml/Math/Distance/ManhattanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace tests\Phpml\Metric;

use Phpml\Exception\InvalidArgumentException;
use Phpml\Math\Distance\Manhattan;
use PHPUnit\Framework\TestCase;

Expand All @@ -19,14 +20,11 @@ public function setUp(): void
$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);
}

Expand Down
6 changes: 2 additions & 4 deletions tests/Phpml/Math/Distance/MinkowskiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace tests\Phpml\Metric;

use Phpml\Exception\InvalidArgumentException;
use Phpml\Math\Distance\Minkowski;
use PHPUnit\Framework\TestCase;

Expand All @@ -19,14 +20,11 @@ public function setUp(): void
$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);
}

Expand Down
Loading

0 comments on commit 946fbbc

Please sign in to comment.