Skip to content

Commit

Permalink
Add tests to make sure readme and methods stay up to date (webmozarts#75
Browse files Browse the repository at this point in the history
)

* Add tests to make sure the methods and readme stay up to date

* Improve error messages
  • Loading branch information
BackEndTea authored and Nyholm committed Dec 25, 2018
1 parent 827c644 commit 318f16b
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* @method static void nullOrIntegerish($value, $message = '')
* @method static void nullOrFloat($value, $message = '')
* @method static void nullOrNumeric($value, $message = '')
* @method static void nullOrNatural($value, $message = '')
* @method static void nullOrBoolean($value, $message = '')
* @method static void nullOrScalar($value, $message = '')
* @method static void nullOrObject($value, $message = '')
Expand All @@ -38,6 +39,7 @@
* @method static void nullOrIsTraversable($value, $message = '')
* @method static void nullOrIsArrayAccessible($value, $message = '')
* @method static void nullOrIsCountable($value, $message = '')
* @method static void nullOrIsIterable($value, $message = '')
* @method static void nullOrIsInstanceOf($value, $class, $message = '')
* @method static void nullOrNotInstanceOf($value, $class, $message = '')
* @method static void nullOrIsInstanceOfAny($value, $classes, $message = '')
Expand Down Expand Up @@ -93,14 +95,16 @@
* @method static void nullOrCount($value, $key, $message = '')
* @method static void nullOrMinCount($value, $min, $message = '')
* @method static void nullOrMaxCount($value, $max, $message = '')
* @method static void nullCountBetween($value, $min, $max, $message = '')
* @method static void nullOrCountBetween($value, $min, $max, $message = '')
* @method static void nullOrUuid($values, $message = '')
* @method static void nullOrThrows($expression, $class = 'Exception', $message = '')
* @method static void allString($values, $message = '')
* @method static void allStringNotEmpty($values, $message = '')
* @method static void allInteger($values, $message = '')
* @method static void allIntegerish($values, $message = '')
* @method static void allFloat($values, $message = '')
* @method static void allNumeric($values, $message = '')
* @method static void allNatural($values, $message = '')
* @method static void allBoolean($values, $message = '')
* @method static void allScalar($values, $message = '')
* @method static void allObject($values, $message = '')
Expand All @@ -110,6 +114,7 @@
* @method static void allIsTraversable($values, $message = '')
* @method static void allIsArrayAccessible($values, $message = '')
* @method static void allIsCountable($values, $message = '')
* @method static void allIsIterable($values, $message = '')
* @method static void allIsInstanceOf($values, $class, $message = '')
* @method static void allNotInstanceOf($values, $class, $message = '')
* @method static void allIsInstanceOfAny($values, $classes, $message = '')
Expand Down Expand Up @@ -169,6 +174,7 @@
* @method static void allMaxCount($values, $max, $message = '')
* @method static void allCountBetween($values, $min, $max, $message = '')
* @method static void allUuid($values, $message = '')
* @method static void allThrows($expressions, $class = 'Exception', $message = '')
*
* @since 1.0
*
Expand Down
119 changes: 119 additions & 0 deletions tests/ProjectCodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace Webmozart\Assert\Tests;

use PHPUnit_Framework_TestCase;
use ReflectionClass;
use ReflectionMethod;

/**
* @coversNothing
*/
class ProjectCodeTest extends PHPUnit_Framework_TestCase
{
private static $readmeContent;
private static $assertDocComment;

public static function setUpBeforeClass()
{
self::$readmeContent = file_get_contents(__DIR__ . '/../README.md');

$rc = new ReflectionClass('\Webmozart\Assert\Assert');
self::$assertDocComment = $rc->getDocComment();
}

/**
* @dataProvider providesMethodNames
*
* @param string $method
*/
public function testHasNullOr($method)
{
if ($method === 'notNull' || $method === 'null') {
$this->addToAssertionCount(1);
return;
}

$this->assertContains(
'@method static void nullOr' . ucfirst($method),
self::$assertDocComment,
sprintf(
'All methods have a corresponding "nullOr" method, please add the "nullOr%s" method to the class level doc comment.',
ucfirst($method)
)
);
}

/**
* @dataProvider providesMethodNames
*
* @param string $method
*/
public function testHasAll($method)
{
$this->assertContains(
'@method static void all' . ucfirst($method),
self::$assertDocComment,
sprintf(
'All methods have a corresponding "all" method, please add the "all%s" method to the class level doc comment.',
ucfirst($method)
)
);
}

/**
* @dataProvider providesMethodNames
*
* @param string $method
*/
public function testIsInReadme($method)
{
$this->assertContains(
$method,
self::$readmeContent,
sprintf(
'All methods must be documented in the README.md, please add the "%s" method.',
ucfirst($method)
)
);
}

/**
* @return array
*/
public function providesMethodNames()
{
return array_map(function($value) {
return array($value);
}, $this->getMethodNames());
}

/**
* @return array
*/
private function getMethodNames()
{
static $methods;

if ($methods !== null) {
return $methods;
}

$rc = new ReflectionClass('\Webmozart\Assert\Assert');
$methods = array();

$rcMethods = $rc->getMethods(ReflectionMethod::IS_PUBLIC);

foreach ($rcMethods as $rcMethod) {
$methodName = $rcMethod->getName();
if (strpos($methodName, '__') === 0) {
continue;
}
$methods[] = $methodName;
}

return $methods;

}

}

0 comments on commit 318f16b

Please sign in to comment.