/** * Generate assertions of an object. * * @param string $variableName name without '$' prefix * @param \object $value * * @return string */ public function generateForAnObject($variableName, $value) { $objectTypeString = get_class($value); $isMockObject = $this->mockClassUtil->isMockClass($objectTypeString); $typeCheckStatement = $this->renderObjectTypeAssertion($isMockObject, $variableName, $value); if ($isMockObject) { // Calling methods on mocked object at the rendering phase can // confuse users where the call comes from. // Don't call json_encode() method here. // @TODO (ryang 12/19/14) : re-evaluate when we start supporting non shmock based mocking // frameworks $valueCheckStatement = ''; } else { $valueCheckStatement = $this->renderObjectValueAssertion($variableName, $value); } $statements = ArrayUtil::joinNonEmptyStringsWithNewLine([$typeCheckStatement, $valueCheckStatement], 2); return $statements; }
/** * @param object $value * * @param bool $replaceMockObjectWithClassName * * @return string */ private function translateObjectToString($value, $replaceMockObjectWithClassName) { $objectTypeString = get_class($value); $mockClassUtil = new MockClassUtil(); $isMockObject = $mockClassUtil->isMockClass($objectTypeString); if ($isMockObject) { // This object must support MockTrait which includes this method. /* @var MockClass $mockObj */ $mockObj = $value->__getUnitTestGeneratorMockInstance(); if ($replaceMockObjectWithClassName) { $objectName = $mockObj->getMockObjectName(); $result = "\$" . $objectName; } else { $result = $mockObj->__toString(); } } else { $result = "( {$objectTypeString} ) object"; if ($value instanceof \JsonSerializable) { $jsonEncoded = json_encode($value); $result .= " value ( {$jsonEncoded} )"; } } return $result; }