importNode($expectedElement, true); $tmp = new DOMDocument; $actualElement = $tmp->importNode($actualElement, true); unset($tmp); static::assertEquals( $expectedElement->tagName, $actualElement->tagName, $message ); if ($checkAttributes) { static::assertEquals( $expectedElement->attributes->length, $actualElement->attributes->length, \sprintf( '%s%sNumber of attributes on node "%s" does not match', $message, !empty($message) ? "\n" : '', $expectedElement->tagName ) ); for ($i = 0; $i < $expectedElement->attributes->length; $i++) { $expectedAttribute = $expectedElement->attributes->item($i); $actualAttribute = $actualElement->attributes->getNamedItem( $expectedAttribute->name ); if (!$actualAttribute) { static::fail( \sprintf( '%s%sCould not find attribute "%s" on node "%s"', $message, !empty($message) ? "\n" : '', $expectedAttribute->name, $expectedElement->tagName ) ); } } } Xml::removeCharacterDataNodes($expectedElement); Xml::removeCharacterDataNodes($actualElement); static::assertEquals( $expectedElement->childNodes->length, $actualElement->childNodes->length, \sprintf( '%s%sNumber of child nodes of "%s" differs', $message, !empty($message) ? "\n" : '', $expectedElement->tagName ) ); for ($i = 0; $i < $expectedElement->childNodes->length; $i++) { static::assertEqualXMLStructure( $expectedElement->childNodes->item($i), $actualElement->childNodes->item($i), $checkAttributes, $message ); } } public static function assertThat($value, Constraint $constraint, $message = '') { self::$count += \count($constraint); $constraint->evaluate($value, $message); } public static function assertJson($actualJson, $message = '') { if (!\is_string($actualJson)) { throw InvalidArgumentHelper::factory(1, 'string'); } static::assertThat($actualJson, static::isJson(), $message); } public static function assertJsonStringEqualsJsonString($expectedJson, $actualJson, $message = '') { static::assertJson($expectedJson, $message); static::assertJson($actualJson, $message); $constraint = new JsonMatches( $expectedJson ); static::assertThat($actualJson, $constraint, $message); } public static function assertJsonStringNotEqualsJsonString($expectedJson, $actualJson, $message = '') { static::assertJson($expectedJson, $message); static::assertJson($actualJson, $message); $constraint = new JsonMatches( $expectedJson ); static::assertThat($actualJson, new LogicalNot($constraint), $message); } public static function assertJsonStringEqualsJsonFile($expectedFile, $actualJson, $message = '') { static::assertFileExists($expectedFile, $message); $expectedJson = \file_get_contents($expectedFile); static::assertJson($expectedJson, $message); static::assertJson($actualJson, $message); $constraint = new JsonMatches( $expectedJson ); static::assertThat($actualJson, $constraint, $message); } public static function assertJsonStringNotEqualsJsonFile($expectedFile, $actualJson, $message = '') { static::assertFileExists($expectedFile, $message); $expectedJson = \file_get_contents($expectedFile); static::assertJson($expectedJson, $message); static::assertJson($actualJson, $message); $constraint = new JsonMatches( $expectedJson ); static::assertThat($actualJson, new LogicalNot($constraint), $message); } public static function assertJsonFileEqualsJsonFile($expectedFile, $actualFile, $message = '') { static::assertFileExists($expectedFile, $message); static::assertFileExists($actualFile, $message); $actualJson = \file_get_contents($actualFile); $expectedJson = \file_get_contents($expectedFile); static::assertJson($expectedJson, $message); static::assertJson($actualJson, $message); $constraintExpected = new JsonMatches( $expectedJson ); $constraintActual = new JsonMatches($actualJson); static::assertThat($expectedJson, $constraintActual, $message); static::assertThat($actualJson, $constraintExpected, $message); } public static function assertJsonFileNotEqualsJsonFile($expectedFile, $actualFile, $message = '') { static::assertFileExists($expectedFile, $message); static::assertFileExists($actualFile, $message); $actualJson = \file_get_contents($actualFile); $expectedJson = \file_get_contents($expectedFile); static::assertJson($expectedJson, $message); static::assertJson($actualJson, $message); $constraintExpected = new JsonMatches( $expectedJson ); $constraintActual = new JsonMatches($actualJson); static::assertThat($expectedJson, new LogicalNot($constraintActual), $message); static::assertThat($actualJson, new LogicalNot($constraintExpected), $message); } public static function logicalAnd() { $constraints = \func_get_args(); $constraint = new LogicalAnd; $constraint->setConstraints($constraints); return $constraint; } public static function logicalOr() { $constraints = \func_get_args(); $constraint = new LogicalOr; $constraint->setConstraints($constraints); return $constraint; } public static function logicalNot(Constraint $constraint) { return new LogicalNot($constraint); } public static function logicalXor() { $constraints = \func_get_args(); $constraint = new LogicalXor; $constraint->setConstraints($constraints); return $constraint; } public static function anything() { return new IsAnything; } public static function isTrue() { return new IsTrue; } public static function callback($callback) { return new Callback($callback); } public static function isFalse() { return new IsFalse; } public static function isJson() { return new IsJson; } public static function isNull() { return new IsNull; } public static function isFinite() { return new IsFinite; } public static function isInfinite() { return new IsInfinite; } public static function isNan() { return new IsNan; } public static function attribute(Constraint $constraint, $attributeName) { return new Attribute( $constraint, $attributeName ); } public static function contains($value, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false) { return new TraversableContains($value, $checkForObjectIdentity, $checkForNonObjectIdentity); } public static function containsOnly($type) { return new TraversableContainsOnly($type); } public static function containsOnlyInstancesOf($classname) { return new TraversableContainsOnly($classname, false); } public static function arrayHasKey($key) { return new ArrayHasKey($key); } public static function equalTo($value, $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false) { return new IsEqual( $value, $delta, $maxDepth, $canonicalize, $ignoreCase ); } public static function attributeEqualTo($attributeName, $value, $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false) { return static::attribute( static::equalTo( $value, $delta, $maxDepth, $canonicalize, $ignoreCase ), $attributeName ); } public static function isEmpty() { return new IsEmpty; } public static function isWritable() { return new IsWritable; } public static function isReadable() { return new IsReadable; } public static function directoryExists() { return new DirectoryExists; } public static function fileExists() { return new FileExists; } public static function greaterThan($value) { return new GreaterThan($value); } public static function greaterThanOrEqual($value) { return static::logicalOr( new IsEqual($value), new GreaterThan($value) ); } public static function classHasAttribute($attributeName) { return new ClassHasAttribute( $attributeName ); } public static function classHasStaticAttribute($attributeName) { return new ClassHasStaticAttribute( $attributeName ); } public static function objectHasAttribute($attributeName) { return new ObjectHasAttribute( $attributeName ); } public static function identicalTo($value) { return new IsIdentical($value); } public static function isInstanceOf($className) { return new IsInstanceOf($className); } public static function isType($type) { return new IsType($type); } public static function lessThan($value) { return new LessThan($value); } public static function lessThanOrEqual($value) { return static::logicalOr( new IsEqual($value), new LessThan($value) ); } public static function matchesRegularExpression($pattern) { return new RegularExpression($pattern); } public static function matches($string) { return new StringMatchesFormatDescription($string); } public static function stringStartsWith($prefix) { return new StringStartsWith($prefix); } public static function stringContains($string, $case = true) { return new StringContains($string, $case); } public static function stringEndsWith($suffix) { return new StringEndsWith($suffix); } public static function countOf($count) { return new Count($count); } public static function fail($message = '') { self::$count++; throw new AssertionFailedError($message); } public static function readAttribute($classOrObject, $attributeName) { if (!\is_string($attributeName)) { throw InvalidArgumentHelper::factory(2, 'string'); } if (!\preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) { throw InvalidArgumentHelper::factory(2, 'valid attribute name'); } if (\is_string($classOrObject)) { if (!\class_exists($classOrObject)) { throw InvalidArgumentHelper::factory( 1, 'class name' ); } return static::getStaticAttribute( $classOrObject, $attributeName ); } if (\is_object($classOrObject)) { return static::getObjectAttribute( $classOrObject, $attributeName ); } throw InvalidArgumentHelper::factory( 1, 'class name or object' ); } public static function getStaticAttribute($className, $attributeName) { if (!\is_string($className)) { throw InvalidArgumentHelper::factory(1, 'string'); } if (!\class_exists($className)) { throw InvalidArgumentHelper::factory(1, 'class name'); } if (!\is_string($attributeName)) { throw InvalidArgumentHelper::factory(2, 'string'); } if (!\preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) { throw InvalidArgumentHelper::factory(2, 'valid attribute name'); } $class = new ReflectionClass($className); while ($class) { $attributes = $class->getStaticProperties(); if (\array_key_exists($attributeName, $attributes)) { return $attributes[$attributeName]; } $class = $class->getParentClass(); } throw new Exception( \sprintf( 'Attribute "%s" not found in class.', $attributeName ) ); } public static function getObjectAttribute($object, $attributeName) { if (!\is_object($object)) { throw InvalidArgumentHelper::factory(1, 'object'); } if (!\is_string($attributeName)) { throw InvalidArgumentHelper::factory(2, 'string'); } if (!\preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) { throw InvalidArgumentHelper::factory(2, 'valid attribute name'); } try { $attribute = new ReflectionProperty($object, $attributeName); } catch (ReflectionException $e) { $reflector = new ReflectionObject($object); while ($reflector = $reflector->getParentClass()) { try { $attribute = $reflector->getProperty($attributeName); break; } catch (ReflectionException $e) { } } } if (isset($attribute)) { if (!$attribute || $attribute->isPublic()) { return $object->$attributeName; } $attribute->setAccessible(true); $value = $attribute->getValue($object); $attribute->setAccessible(false); return $value; } throw new Exception( \sprintf( 'Attribute "%s" not found in object.', $attributeName ) ); } public static function markTestIncomplete($message = '') { throw new IncompleteTestError($message); } public static function markTestSkipped($message = '') { throw new SkippedTestError($message); } public static function getCount() { return self::$count; } public static function resetCount() { self::$count = 0; } }