class GetSetMethodNormalizerTest

Hierarchy

  • class \Symfony\Component\Serializer\Tests\Normalizer\GetSetMethodNormalizerTest extends \Symfony\Component\Serializer\Tests\Normalizer\PHPUnit_Framework_TestCase

Expanded class hierarchy of GetSetMethodNormalizerTest

File

drupal/core/vendor/symfony/serializer/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php, line 16

Namespace

Symfony\Component\Serializer\Tests\Normalizer
View source
class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase {
  protected function setUp() {
    $this->normalizer = new GetSetMethodNormalizer();
    $this->normalizer
      ->setSerializer($this
      ->getMock('Symfony\\Component\\Serializer\\Serializer'));
  }
  public function testNormalize() {
    $obj = new GetSetDummy();
    $obj
      ->setFoo('foo');
    $obj
      ->setBar('bar');
    $this
      ->assertEquals(array(
      'foo' => 'foo',
      'bar' => 'bar',
      'fooBar' => 'foobar',
    ), $this->normalizer
      ->normalize($obj, 'any'));
  }
  public function testDenormalize() {
    $obj = $this->normalizer
      ->denormalize(array(
      'foo' => 'foo',
      'bar' => 'bar',
      'fooBar' => 'foobar',
    ), __NAMESPACE__ . '\\GetSetDummy', 'any');
    $this
      ->assertEquals('foo', $obj
      ->getFoo());
    $this
      ->assertEquals('bar', $obj
      ->getBar());
  }
  public function testConstructorDenormalize() {
    $obj = $this->normalizer
      ->denormalize(array(
      'foo' => 'foo',
      'bar' => 'bar',
      'fooBar' => 'foobar',
    ), __NAMESPACE__ . '\\GetConstructorDummy', 'any');
    $this
      ->assertEquals('foo', $obj
      ->getFoo());
    $this
      ->assertEquals('bar', $obj
      ->getBar());
  }

  /**
   * @dataProvider provideCallbacks
   */
  public function testCallbacks($callbacks, $value, $result, $message) {
    $this->normalizer
      ->setCallbacks($callbacks);
    $obj = new GetConstructorDummy('', $value);
    $this
      ->assertEquals($result, $this->normalizer
      ->normalize($obj, 'any'), $message);
  }

  /**
   * @expectedException \InvalidArgumentException
   */
  public function testUncallableCallbacks() {
    $this->normalizer
      ->setCallbacks(array(
      'bar' => null,
    ));
    $obj = new GetConstructorDummy('baz', 'quux');
    $this->normalizer
      ->normalize($obj, 'any');
  }
  public function testIgnoredAttributes() {
    $this->normalizer
      ->setIgnoredAttributes(array(
      'foo',
      'bar',
    ));
    $obj = new GetSetDummy();
    $obj
      ->setFoo('foo');
    $obj
      ->setBar('bar');
    $this
      ->assertEquals(array(
      'fooBar' => 'foobar',
    ), $this->normalizer
      ->normalize($obj, 'any'));
  }
  public function provideCallbacks() {
    return array(
      array(
        array(
          'bar' => function ($bar) {
            return 'baz';
          },
        ),
        'baz',
        array(
          'foo' => '',
          'bar' => 'baz',
        ),
        'Change a string',
      ),
      array(
        array(
          'bar' => function ($bar) {
            return null;
          },
        ),
        'baz',
        array(
          'foo' => '',
          'bar' => null,
        ),
        'Null an item',
      ),
      array(
        array(
          'bar' => function ($bar) {
            return $bar
              ->format('d-m-Y H:i:s');
          },
        ),
        new \DateTime('2011-09-10 06:30:00'),
        array(
          'foo' => '',
          'bar' => '10-09-2011 06:30:00',
        ),
        'Format a date',
      ),
      array(
        array(
          'bar' => function ($bars) {
            $foos = '';
            foreach ($bars as $bar) {
              $foos .= $bar
                ->getFoo();
            }
            return $foos;
          },
        ),
        array(
          new GetConstructorDummy('baz', ''),
          new GetConstructorDummy('quux', ''),
        ),
        array(
          'foo' => '',
          'bar' => 'bazquux',
        ),
        'Collect a property',
      ),
      array(
        array(
          'bar' => function ($bars) {
            return count($bars);
          },
        ),
        array(
          new GetConstructorDummy('baz', ''),
          new GetConstructorDummy('quux', ''),
        ),
        array(
          'foo' => '',
          'bar' => 2,
        ),
        'Count a property',
      ),
    );
  }

}

Members