public function testFails() { $constraint = new testedClass(); try { $constraint->evaluate(null); } catch (\PHPUnit_Framework_ExpectationFailedException $exception) { $analyzer = new atoum\tools\variable\analyzer(); $this->assertEquals($analyzer->getTypeOf(null) . ' is null', $exception->getMessage()); } }
public function testAssertArrayContainsOnlyStdClass() { $constraint = new testedClass('StdClass'); $this->assertSame($constraint, $constraint->evaluate(array(new \stdClass()))); $actual = array('StdClass'); try { $constraint->evaluate($actual); $this->fail(); } catch (\PHPUnit_Framework_ExpectationFailedException $exception) { $analyzer = new atoum\tools\variable\analyzer(); $this->assertEquals($analyzer->getTypeOf($actual[0]) . ' is not an object', $exception->getMessage()); } }
public function testAssertFinite() { $constraint = new testedClass(); $this->assertSame($constraint, $constraint->evaluate(rand(0, PHP_INT_MAX))); $actual = INF; try { $constraint->evaluate($actual); $this->fail(); } catch (\PHPUnit_Framework_ExpectationFailedException $exception) { $analyzer = new atoum\tools\variable\analyzer(); $this->assertEquals($analyzer->getTypeOf($actual) . ' is not finite', $exception->getMessage()); } }
public function testAssertCountTraversable() { $constraint = new testedClass(2); $constraint->evaluate(new \arrayIterator(array(1, 2))); try { $constraint->evaluate(new \arrayIterator(array(1, 2, 3))); $this->fail(); } catch (\PHPUnit_Framework_ExpectationFailedException $exception) { $analyzer = new atoum\tools\variable\analyzer(); $diff = new atoum\tools\diff($analyzer->dump(2), $analyzer->dump(3)); $this->assertEquals('integer(3) is not equal to integer(2)' . PHP_EOL . $diff, $exception->getMessage()); } }
public function testGreaterThanWithFloats() { $expected = 1; $constraint = new testedClass($expected); $this->assertSame($constraint, $constraint->evaluate(4 / 3)); $actual = 1 / 3; try { $constraint->evaluate($actual); $this->fail(); } catch (\PHPUnit_Framework_ExpectationFailedException $exception) { $analyzer = new atoum\tools\variable\analyzer(); $this->assertEquals($analyzer->getTypeOf($actual) . ' is not greater than ' . $analyzer->getTypeOf($expected), $exception->getMessage()); } }
public function testAssertInternalTypeDouble() { $constraint = new testedClass('double'); $this->assertSame($constraint, $constraint->evaluate(1.0)); $actual = rand(0, PHP_INT_MAX); try { $constraint->evaluate($actual); $this->fail(); } catch (\PHPUnit_Framework_ExpectationFailedException $exception) { $analyzer = new atoum\tools\variable\analyzer(); $diff = new atoum\tools\diffs\variable(true, false); $this->assertEquals($analyzer->getTypeOf($actual) . ' is not a double' . PHP_EOL . $diff, $exception->getMessage()); } }
public function testAssertContainsOnlyInstancesOf() { $constraint = new testedClass('Book'); $this->assertSame($constraint, $constraint->evaluate(array(new \book(), new \book()))); $this->assertSame($constraint, $constraint->evaluate(new \arrayIterator(array(new \book(), new \book())))); $constraint = new testedClass('stdClass'); $this->assertSame($constraint, $constraint->evaluate(array(new \stdClass()))); $actual = array(new \author('Test')); $expected = 'Book'; $constraint = new testedClass($expected); try { $constraint->evaluate($actual); $this->fail(); } catch (\PHPUnit_Framework_ExpectationFailedException $exception) { $analyzer = new atoum\tools\variable\analyzer(); $this->assertEquals($analyzer->getTypeOf($actual[0]) . ' is not an instance of Book', $exception->getMessage()); } }
public function testFails() { $constraint = new testedClass(true); try { $constraint->evaluate(false); } catch (\PHPUnit_Framework_ExpectationFailedException $exception) { $analyzer = new atoum\tools\variable\analyzer(); $diff = new atoum\tools\diff($analyzer->dump(true), $analyzer->dump(false)); $this->assertEquals($analyzer->getTypeOf(false) . ' is not true' . PHP_EOL . $diff, $exception->getMessage()); } $constraint = new testedClass(false); try { $constraint->evaluate(true); } catch (\PHPUnit_Framework_ExpectationFailedException $exception) { $analyzer = new atoum\tools\variable\analyzer(); $diff = new atoum\tools\diff($analyzer->dump(false), $analyzer->dump(true)); $this->assertEquals($analyzer->getTypeOf(true) . ' is not false' . PHP_EOL . $diff, $exception->getMessage()); } }
public function testAssertArraySubsetWithStrictCheckAndObjects() { $obj = new \stdClass(); $reference =& $obj; $array = array('a' => $obj); $constraint = new testedClass(array('a' => $reference), null, true); $this->assertSame($constraint, $constraint->evaluate($array)); $expected = array('a' => new \stdClass()); $constraint = new testedClass($expected, null, true); $analyzer = new atoum\tools\variable\analyzer(); try { $constraint->evaluate($array); $this->fail('Strict recursive array check fail.'); } catch (\PHPUnit_Framework_ExpectationFailedException $exception) { $patched = array_replace_recursive($array, $expected); $diff = new atoum\tools\diffs\variable($patched, $array); $this->assertEquals($analyzer->getTypeOf($array) . ' is not identical to ' . $analyzer->getTypeOf($patched) . PHP_EOL . $diff, $exception->getMessage()); } }
private static function getAssertion($type, analyzer $analyzer) { $namespace = 'mageekguy\\atoum\\asserters'; $assertion = null; $transform = null; $expected = null; $message = null; switch ($type) { case 'int': $classname = $namespace . '\\integer'; break; case 'float': $classname = $namespace . '\\phpFloat'; break; case 'bool': $classname = $namespace . '\\boolean'; break; case 'string': $classname = $namespace . '\\phpString'; break; case 'array': $classname = $namespace . '\\phpArray'; break; case 'null': $classname = $namespace . '\\variable'; $assertion = 'isNull'; break; case 'scalar': case 'numeric': case 'double': case 'real': case 'callable': $classname = $namespace . '\\boolean'; $transform = 'is_' . $type; $assertion = 'isTrue'; $message = '%s is not a %s'; break; case 'boolean': case 'integer': case 'resource': $classname = $namespace . '\\' . $type; break; default: $classname = $namespace . '\\object'; } return function ($actual) use($classname, $assertion, $transform, $expected, $message, $type, $analyzer) { $asserter = new $classname(); if ($message !== null) { $message = sprintf($message, $analyzer->getTypeOf($actual), $type); } if ($transform !== null) { $actual = call_user_func($transform, $actual); } try { $asserter->setWith($actual, $message); } catch (exceptions\logic $exception) { throw new \PHPUnit_Framework_Exception('Expected value of ' . __CLASS__ . ' must be a valid type or class name'); } if ($assertion !== null) { $asserter->{$assertion}($expected ?: $message, $message); } }; }
public function testAssertStringContainsString() { $expected = 'foo'; $constraint = new testedClass($expected); $this->assertSame($constraint, $constraint->evaluate('foobar')); $actual = 'bar'; try { $constraint->evaluate($actual); $this->fail(); } catch (\PHPUnit_Framework_ExpectationFailedException $exception) { $analyzer = new atoum\tools\variable\analyzer(); $this->assertEquals($analyzer->getTypeOf($actual) . ' does not contain ' . $analyzer->getTypeOf($expected), $exception->getMessage()); } }