TypedDataNormalizerTest.php 1.71 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php

/**
 * @file
 * Contains \Drupal\serialization\Tests\Normalizer\TypedDataNormalizerTest
 */

namespace Drupal\serialization\Tests\Normalizer;

use Drupal\Tests\UnitTestCase;
use Drupal\serialization\Normalizer\TypedDataNormalizer;

/**
 * Tests the TypedDataNormalizer class.
 *
 * @see \Drupal\serialization\Normalizer\TypedDataNormalizer
 *
 * @group Drupal
 */
class TypedDataNormalizerTest extends UnitTestCase {

  /**
   * The TypedDataNormalizer instance.
   *
   * @var \Drupal\serialization\Normalizer\TypedDataNormalizer
   */
  protected $normalizer;

  /**
   * The mock typed data instance.
   *
   * @var \Drupal\Core\TypedData\TypedDataInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $typedData;

  public static function getInfo() {
    return array(
      'name' => 'TypedDataNormalizer',
      'description' => 'Tests the TypedDataNormalizer class.',
      'group' => 'Serialization',
    );
  }

  public function setUp() {
    $this->normalizer = new TypedDataNormalizer();
    $this->typedData = $this->getMock('Drupal\Core\TypedData\TypedDataInterface');
  }

  /**
   * Tests the supportsNormalization() method.
   */
  public function testSupportsNormalization() {
    $this->assertTrue($this->normalizer->supportsNormalization($this->typedData));
    // Also test that an object not implementing TypedDataInterface fails.
    $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
  }

  /**
   * Tests the normalize() method.
   */
  public function testNormalize() {
    $this->typedData->expects($this->once())
      ->method('getValue')
      ->will($this->returnValue('test'));

    $this->assertEquals('test', $this->normalizer->normalize($this->typedData));
  }

}