class JsonEncoderTest
Same name in this branch
- 11.x core/modules/serialization/tests/src/Unit/Encoder/JsonEncoderTest.php \Drupal\Tests\serialization\Unit\Encoder\JsonEncoderTest
Same name and namespace in other branches
- 10 core/modules/serialization/tests/src/Unit/Encoder/JsonEncoderTest.php \Drupal\Tests\serialization\Unit\Encoder\JsonEncoderTest
- 9 core/modules/serialization/tests/src/Unit/Encoder/JsonEncoderTest.php \Drupal\Tests\serialization\Unit\Encoder\JsonEncoderTest
- 8.9.x core/modules/serialization/tests/src/Unit/Encoder/JsonEncoderTest.php \Drupal\Tests\serialization\Unit\Encoder\JsonEncoderTest
Tests the JSON:API encoder.
@internal
Attributes
#[CoversClass(JsonEncoder::class)]
#[Group('jsonapi')]
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\jsonapi\Unit\Encoder\JsonEncoderTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of JsonEncoderTest
File
-
core/
modules/ jsonapi/ tests/ src/ Unit/ Encoder/ JsonEncoderTest.php, line 17
Namespace
Drupal\Tests\jsonapi\Unit\EncoderView source
class JsonEncoderTest extends UnitTestCase {
/**
* The encoder under test.
*/
protected JsonEncoder $encoder;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->encoder = new JsonEncoder();
}
/**
* Tests the supportsEncoding() method.
*/
public function testSupportsEncoding() : void {
$this->assertTrue($this->encoder
->supportsEncoding('api_json'));
$this->assertFalse($this->encoder
->supportsEncoding('json'));
$this->assertFalse($this->encoder
->supportsEncoding('xml'));
}
/**
* Tests JSON:API encoder inherits control character handling.
*
* This verifies that the JSON:API encoder correctly inherits the
* JSON_INVALID_UTF8_SUBSTITUTE flag from the parent serialization encoder.
* The comprehensive tests for this functionality are in the serialization
* module's JsonEncoderTest.
*
* @see \Drupal\Tests\serialization\Unit\Encoder\JsonEncoderTest
* @see https://www.drupal.org/project/drupal/issues/3549107
*/
public function testInheritsControlCharacterHandling() : void {
// Test that invalid UTF-8 is handled (would fail without the flag).
$input = "Test\x80Data";
$encoded = $this->encoder
->encode($input, 'api_json');
// Verify it's valid JSON.
$this->assertJson($encoded, 'Encoded output should be valid JSON even with invalid UTF-8.');
// Verify the replacement character is present.
$this->assertStringContainsString('\\ufffd', $encoded, 'Invalid UTF-8 should be replaced with U+FFFD.');
// Verify it can be decoded.
json_decode($encoded);
$this->assertSame(JSON_ERROR_NONE, json_last_error(), 'Encoded JSON should be decodable without errors.');
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
|---|---|---|---|---|
| ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
| ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | |
| ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | |
| JsonEncoderTest::$encoder | protected | property | The encoder under test. | |
| JsonEncoderTest::setUp | protected | function | Overrides UnitTestCase::setUp | |
| JsonEncoderTest::testInheritsControlCharacterHandling | public | function | Tests JSON:API encoder inherits control character handling. | |
| JsonEncoderTest::testSupportsEncoding | public | function | Tests the supportsEncoding() method. | |
| RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |
| RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |
| RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |
| RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
| UnitTestCase::$root | protected | property | The app root. | |
| UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |
| UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |
| UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |
| UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |
| UnitTestCase::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | |
| UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.