The expected value is passed in the constructor.
Inheritance: extends PHPUnit_Framework_Constraint
Exemplo n.º 1
0
 /**
  *
  */
 public function assertJSONTypes($keyOrFieldTypes)
 {
     // Formats function args
     $args = func_get_args();
     $params = array('key' => null, 'field_types' => array());
     if (count($args) === 1 && is_array($args[0])) {
         $params['field_types'] = $args[0];
     } elseif (count($args) === 2 && (is_string($args[0]) || is_null($args[0])) && is_array($args[1])) {
         $params['key'] = $args[0];
         $params['field_types'] = $args[1];
     } else {
         // TODO
         throw new \Exception('');
     }
     // Get JSON content
     $accessor = PropertyAccess::createPropertyAccessor(true, true);
     $content = $this->getJSONContent()->response;
     if ($params['key'] !== null) {
         $content = $accessor->getValue($content, $params['key']);
     }
     // Check assertions
     foreach ($params['field_types'] as $fieldName => $formats) {
         if (!property_exists($content, $fieldName)) {
             // If optional field
             if (starts_with($fieldName, '(') && ends_with($fieldName, ')')) {
                 continue;
             }
             throw new \Exception(sprintf('The field named "%s" does not exists into the content "%s"', $fieldName, json_encode($content, true)));
         }
         if (is_array($formats) && $this->isKeyValueArray($formats)) {
             // TODO nested func
             // $this->assertJSONTypes($fieldName, $formats, $value)
         } else {
             $value = $accessor->getValue($content, $fieldName);
             $asserts = array();
             foreach ((array) $formats as $format) {
                 if (in_array($format, self::$formats)) {
                     // TEMPORARY FIX
                     // TODO remove it when possible (see phpunit)
                     if ($format === 'double') {
                         $format = 'float';
                     }
                     // $this->assertInternalType($format, $value);
                     $constraint = new \PHPUnit_Framework_Constraint_IsType($format);
                     array_push($asserts, $constraint->evaluate($value, '', true));
                 } elseif ($format === 'datetime') {
                     array_push($asserts, $this->isDateTimeString($value, \DateTime::ISO8601));
                 } elseif (class_exists($format)) {
                     // $this->assertInstanceOf($format, $value);
                     array_push($asserts, $value instanceof $format);
                 } else {
                     throw new \Exception(sprintf('The type or class "%s" does not exist.', $format));
                 }
             }
             // Assert
             $this->assertContains(true, $asserts, sprintf('Failed asserting that %s for field "%s" is of %s "%s".', json_encode($value), $fieldName, count($formats) === 1 ? 'type' : 'types', implode(', ', (array) $formats)));
         }
     }
 }
Exemplo n.º 2
0
 public function testConstraintIsType2()
 {
     $constraint = new PHPUnit_Framework_Constraint_IsType('string');
     try {
         $constraint->fail(new stdClass(), 'custom message');
     } catch (PHPUnit_Framework_ExpectationFailedException $e) {
         $this->assertEquals("custom message\nFailed asserting that \nstdClass Object\n(\n)\n is of type \"string\".", $e->getDescription());
         return;
     }
     $this->fail();
 }
Exemplo n.º 3
0
 public function testConstraintIsType()
 {
     $constraint = new PHPUnit_Framework_Constraint_IsType('string');
     $this->assertFalse($constraint->evaluate(0));
     $this->assertTrue($constraint->evaluate(''));
     $this->assertEquals('is of type "string"', $constraint->toString());
     try {
         $constraint->fail(new stdClass(), '');
     } catch (PHPUnit_Framework_ExpectationFailedException $e) {
         $this->assertEquals("Failed asserting that \nstdClass Object\n(\n)\n is of type \"string\".", $e->getDescription());
         return;
     }
     $this->fail();
 }