diff options
Diffstat (limited to 'tests/phpunit/mocks/json/SampleObject.php')
-rw-r--r-- | tests/phpunit/mocks/json/SampleObject.php | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/phpunit/mocks/json/SampleObject.php b/tests/phpunit/mocks/json/SampleObject.php new file mode 100644 index 000000000000..9b1faaea7c2f --- /dev/null +++ b/tests/phpunit/mocks/json/SampleObject.php @@ -0,0 +1,54 @@ +<?php +namespace MediaWiki\Tests\Json; + +use Wikimedia\Assert\Assert; +use Wikimedia\JsonCodec\JsonCodecable; +use Wikimedia\JsonCodec\JsonCodecableTrait; + +/** + * Sample object which uses JsonCodecableTrait to directly implement + * serialization/deserialization. + */ +class SampleObject implements JsonCodecable { + use JsonCodecableTrait; + + /** @var string */ + public string $property; + + /** + * Create a new SampleObject which stores $property. + * @param string $property + */ + public function __construct( string $property ) { + $this->property = $property; + } + + // Implement JsonCodecable using the JsonCodecableTrait + + /** @inheritDoc */ + public function toJsonArray(): array { + if ( $this->property !== 'use _type_' ) { + // Allow testing both with and without the '_type_' special case + return [ 'property' => $this->property ]; + } + return [ + 'property' => $this->property, + // Implementers shouldn't have to know which properties the + // codec is using for its own purposes; this will still work + // fine: + '_type_' => 'check123', + ]; + } + + /** @inheritDoc */ + public static function newFromJsonArray( array $json ): SampleObject { + if ( $json['property'] === 'use _type_' ) { + Assert::invariant( $json['_type_'] === 'check123', 'protected field' ); + } + return new SampleObject( $json['property'] ); + } +} + +// This class_alias exists for testing alias support in JsonCodec and +// should not be removed. +class_alias( SampleObject::class, 'MediaWiki\\Tests\\Json\\SampleObjectAlias' ); |