Commit graph

43 commits

Author SHA1 Message Date
C. Scott Ananian
3bc172d0e4 [JsonCodec] Use wikimedia/json-codec to implement JsonCodec
This adds support for serializing/deserializing objects which
implement the JsonCodecable interface from the wikimedia/json-codec
library used by Parsoid.  JsonCodecable allows customizing the encoding
of objects of a given class using a class-specific codec object, and
JsonCodecable is an interface which is defined and can be used outside
mediawiki core.

In addition json-codec supports deserialization in the presence of
aliased class names, fixing T353883.

Backward and forward compatibility established via the mechanism
described in
https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility

Test data generated by this patch was added in
I109640b510cef9b3b870a8c188f3b4f086d75d06 to ensure forward
compatibility with the output after this patch is merged.

Benchmarks:
                        PHP 7.4.33          PHP 8.2.19          PHP 8.3.6
                      BEFORE    AFTER     BEFORE    AFTER     BEFORE    AFTER
Serialize:            926.7/s  1424.8/s   978.5/s  1542.4/s  1023.5/s  1488.6/s
Serialize (assoc):    930.2/s  1378.6/s   974.6/s  1541.9/s  1022.4/s  1463.4/s
Deserialize:         1942.7/s  1961.3/s  2118.8/s  2175.9/s  2129.8/s  2063.5/s
Deserialize (assoc): 1952.0/s  1905.7/s  2107.5/s  2192.1/s  2153.3/s  2011.1/s

These numbers definitely do not have as many significant digits as
written here.  But they should be sufficient to demonstrate that
performance is not impaired by this patch and in fact serialization
speed improves slightly.

Bug: T273540
Bug: T327439
Bug: T346829
Bug: T353883
Depends-On: If1d70ba18712839615c1f4fea236843ffebc8645
Change-Id: Ia1017dcef462f3ac1ff5112106f7df81f5cc384f
2024-10-15 20:09:51 -04:00
James D. Forrester
19f4e6945a Rename JsonUnserial… to JsonDeserial…
This is to make it clearer that they're related to converting serialized
content back into JSON, rather than stating that things are not
representable in JSON.

Change-Id: Ic440ac2d05b5ac238a1c0e4821d3f2d858bc3d76
2024-06-12 14:50:58 -04:00
C. Scott Ananian
c5cc43348a [JsonCodec, ParserCache] Improve debugging of serializability failures
Bug: T365036
Change-Id: I6c4c2a6a48d3bca4ade76a05bbd81cb4968872a3
2024-05-16 14:49:21 -04:00
Ebrahim Byagowi
a717db8e60 Add namespace and deprecation alias to FormatJson
This patch introduces a namespace declaration for the
MediaWiki\Json to FormatJson and establishes a class
alias marked as deprecated since version 1.43.

Bug: T353458
Change-Id: I5e1311e4eb7a878a7db319b725ae262f40671c32
2024-05-16 16:28:01 +03:30
jenkins-bot
78637143f1 Merge "Remove all @package comments" 2024-05-13 12:21:22 +00:00
Bartosz Dziewoński
73de566949 Use 'scalar' type alias to shorten PHPDoc annotations
'string|int|float|bool' (in any order) can be replaced by 'scalar'.
'string|int|float|bool|null' (likewise) can be replaced by '?scalar'.

This is convenient for functions that can accept any primitive value,
which comes up sometimes when serializing things as SQL, JSON etc.

Change-Id: I4a711ee59611d76d6745f3640e4aa6bebec02918
2024-05-11 23:21:22 +00:00
thiemowmde
52ddf3e8ce Remove all @package comments
I don't think these do anything with the documentation generators
we currently use. Especially not in tests. How are tests part of a
"package" when the code is not?

Note how most of these are simply identical to the namespace. They
are most probably auto-generated by some IDEs but don't actually
mean anything.

Change-Id: I771b5f2041a8e3b077865c79cbebddbe028543d1
2024-05-10 13:53:15 +02:00
Reedy
85396a9c99 tests: Fix @covers and @coversDefaultClass to have leading \
Change-Id: I5629f91387f2ac453ee4341bfe4bba310bd52f03
2024-02-16 22:43:56 +00:00
C. Scott Ananian
dbc75831fe [JsonCodec] throw JsonException now that we require PHP >= 7.4
Also fixes JsonCodeTest::testInvalidJsonData() which was misusing the
data provided by ::provideSimpleTypes().

Change-Id: Ia654359e0fdec3ad546e8bea2e9133c142f0f144
2024-01-08 20:03:12 +00:00
thiemowmde
46bed8ac6d Make use of assertStatusGood/Error and such in tests
Change-Id: I11eace3d9823ca28a1d9a64f959f5f8ca2945821
2023-10-04 17:16:00 +00:00
Amir Sarabadani
f4e68e055f Reorg: Move Status to MediaWiki\Status\
This class is used heavily basically everywhere, moving it to Utils
wouldn't make much sense. Also with this change, we can move
StatusValue to MediaWiki\Status as well.

Bug: T321882
Depends-On: I5f89ecf27ce1471a74f31c6018806461781213c3
Change-Id: I04c1dcf5129df437589149f0f3e284974d7c98fa
2023-08-25 15:44:17 +02:00
Tim Starling
5e30a927bc tests: Make some PHPUnit data providers static
Just methods where adding "static" to the declaration was enough, I
didn't do anything with providers that used $this.

Initially by search and replace. There were many mistakes which I
found mostly by running the PHPStorm inspection which searches for
$this usage in a static method. Later I used the PHPStorm "make static"
action which avoids the more obvious mistakes.

Bug: T332865
Change-Id: I47ed6692945607dfa5c139d42edbd934fa4f3a36
2023-03-24 02:53:57 +00:00
Matěj Suchánek
c231736471 Fix some typos
Bug: T201491
Change-Id: I5c9408c262f09c936525f35abfacfa92a193b791
2023-03-21 15:58:09 +01:00
James D. Forrester
ad06527fb4 Reorg: Namespace the Title class
This is moderately messy.

Process was principally:

* xargs rg --files-with-matches '^use Title;' | grep 'php$' | \
  xargs -P 1 -n 1 sed -i -z 's/use Title;/use MediaWiki\\Title\\Title;/1'
* rg --files-without-match 'MediaWiki\\Title\\Title;' . | grep 'php$' | \
  xargs rg --files-with-matches 'Title\b' | \
  xargs -P 1 -n 1 sed -i -z 's/\nuse /\nuse MediaWiki\\Title\\Title;\nuse /1'
* composer fix

Then manual fix-ups for a few files that don't have any use statements.

Bug: T166010
Follows-Up: Ia5d8cb759dc3bc9e9bbe217d0fb109e2f8c4101a
Change-Id: If8fc9d0d95fc1a114021e282a706fc3e7da3524b
2023-03-02 08:46:53 -05:00
thiemowmde
d4d070c6e9 json: Dont try to deserialize actual user-land instances
A valid JSON serialization is an instance of PHP's stdClass. A check
with is_object() is not sufficient in this case because it includes
everything else that's also a class in PHP.

This should help to uncover programming errors like the one in
I969d8c4.

Bug: T312589
Change-Id: I917d49944497b19909a9a1d1e2861e86e7a0aca8
2023-01-26 21:17:21 +00:00
C. Scott Ananian
96e4f5d840 JsonCodec: fix en/decoding of nested objects and stdClass objects
Add a type annotation when encoding `stdClass` objects so that we can
be sure to decode them as objects instead of arrays.

This avoids issues such as that seen in the Graph extension (T312589)
where an extension data key is stored as a stdClass.  If ParserOutput
was computed fresh, a subsequent getExtensionData(..) call will return
a stdClass object, but if the ParserOutput was cached, getExtensionData()
would return an array.  After this change the return type is always
consistent.

Properly handle nested objects: encode all object values returned by
JsonSerializable::jsonSerialize() (so that client is not responsible
for implementing this correctly), and decode all object values *before*
calling JsonUnserializable::newFromJsonArray (again, so that the
client is not responsible for decoding its property values).  The new
behavior matches how serialize/unserialize is handled in the 'naive'
JsonUnserializable{Sub,Super}Class test cases; ParserOutput (the only
users of JsonCodec in core) was doing an extra manual decode for
the ExtensionData array in ParserOutput::initFromJson that is no longer
necessary.

The GrowthExperiments and SemanticMediaWiki extensions were working
around the non-recursive nature of JsonCodec; this patch depends on
patches to GrowthExperiments to make it agnostic about whether object
unserialization occurs before or after ::newFromJsonArray() is called,
which can then be further cleaned up once this is released.
A pull request for SemanticMediaWiki has also been submitted.

Bug: T312589
Depends-On: I3413609251f056893d3921df23698aeed40754ed
Change-Id: Id7d0695af40b9801b42a9b82f41e46118da288dc
2023-01-12 14:12:32 -05:00
Reedy
a3095fbb94 Add return type to jsonSerialize()
Bug: T311919
Change-Id: I469deae973ab58ef41aac6a56cea0653a988c05c
2022-07-02 15:34:02 +00:00
Aryeh Gregor
1560b98225 Type hints for ArrayAccess and JsonSerializable
These two interfaces' methods have tentative return types in PHP 8.1,
which causes code without the type hints to raise warnings. Where the
type hint is "mixed", we need to use the special declaration
[\ReturnTypeWillChange] in a comment to suppress the warning as long as
we still support PHP < 8.0, which doesn't have a "mixed" type hint.

Bug: T289879
Change-Id: I1a126e602e92b8d13c7795eb6d790effd5ddc986
2022-04-11 15:06:27 +03:00
daniel
e239b02a5e Add convenience methods for asserting status.
This ensures that assertions work in a uniform way,
and provides meaningful messages in cause of failure.

Change-Id: Ic01715b9a55444d3df6b5d4097e78cb8ac082b3e
2022-03-16 22:44:25 +01:00
Gergő Tisza
35e1f7cc43
JsonCodec: Fix test
Doesn't make any functional difference but less confusing.
Also, clarify why this testcase is expected to fail.

Change-Id: I56f03d5c02cf624a4eba73d9d546cf6c2ebf6a77
2021-12-09 19:28:30 -08:00
Thiemo Kreuz
ee0f97378b Replace new stdClass with more compact array syntax
It does the exact same. The resulting object is still an stdClass
instance.

Change-Id: Ief68609943ee30aa95732d24021c921dfbad166c
2021-11-02 09:43:03 +00:00
jenkins-bot
83943e3187 Merge "FormatJson: Optimize encode() for supported PHP versions" 2021-09-04 01:24:12 +00:00
Fomafix
3a322ef9b0 Use PHP \u{xxxx} syntax
Let PHP do the UTF-8 encoding of Unicode characters in PHP strings.

Also use faster str_replace instead of preg_replace.

Change-Id: I4e99de694a607e2b5df52c6efcd3d863bb42f76e
2021-08-27 20:53:19 +00:00
Kevin Israel
210a34369a FormatJson: Optimize encode() for supported PHP versions
- Removed the str_replace() call to replace unescaped line terminators
  if UTF8_OK is set. PHP 7.1 and later escape these by default.

  The speedup isn't much at all (about 1% in my testing when encoding an
  API siteinfo result taken from enwiki). Perhaps it's not surprising
  given the way str_replace() works[1]. Still, it's better not to spend
  CPU time looking for characters that will not occur.

- Changed the algorithm for the optional spaces-to-tabs conversion when
  pretty printing. Instead of replacing one indent level throughout the
  entire string before replacing the next level, use a regex to replace
  in one pass. This is usually faster now that PHP 7 enables PCRE's JIT
  compiler by default. Without JIT, the regex was often slower.

  The speedup can be large for deeply nested data. For example, in my
  testing the languages/i18n data took about 8% less time to encode as
  tab-indented JSON, yet the API site info result took about 45% less.
  (This, of course, isn't actually relevant to the API even when pretty
  printed output is requested, because ApiFormatJson uses the default
  indent string of four spaces, which will always be faster unless
  support for tab indentation is added to PHP's json extension.)

- Set options using if statements instead of the ternary operator. This
  is the clearer way, and maybe the slightly faster one, skipping the
  assignment when the flags do not need to be set.

[1]: https://github.com/php/php-src/blob/PHP-8.0.10/ext/standard/string.c#L2969

Change-Id: Iebb1df0264e335a1819956710eeacf6d6b8f1471
2021-08-20 08:03:11 -04:00
Kevin Israel
b084f499db FormatJson: Add message for JSON_ERROR_INVALID_PROPERTY_NAME
The comment added in b9461e3f1c is incorrect. This is actually a
decode error, so is relevant to FormatJson::parse().

Change-Id: I3cc33f0f260c0ba4fe96fb75565f52d089b9a975
2021-08-16 10:57:14 -04:00
DannyS712
db4f2bbb24 Merge FormatJsonTest and FormatJsonUnitTest
FormatJsonUnitTest was split off back when the
rest still needed to be integration tests[1], but
after [2] with global functions being loaded for unit
tests, the rest of FormatJsonTest was moved to a
unit test since it no longer required integration

[1] I86dfe17f794c615048b3c20487b0e84d38d13b93
[2] Ib42c56a67926ebcdba53f4c6c54a5bff98cb77a3

Change-Id: I92bb7a6cafd82d8b2186f92e0953bc18f40b0ee4
2021-05-05 20:52:30 +00:00
Umherirrender
f20c0ac8bb Remove incomplete @param from test function
the @dataProvider should be a enough here

Change-Id: I36549dcac027a68244de042d48d90d4070771be8
2021-02-01 20:19:22 +01:00
Thiemo Kreuz
b655f382db Remove broken/outdated @param/@throws tags from @dataProviders
My personal best practice is to not document @params when there
is a @dataProvider. I mean, these test…() functions are not
meant to be called from anywhere. They do not really need
documentation. @param tags don't do much but duplicate what the
@dataProvider does. This is error-prone, as demonstrated by the
examples in this patch.

This patch also removes @throws tags from tests. A test…() can
never throw an exception. Otherwise the test would fail.

Most of these are found by the not yet released I10559d8.

Change-Id: I3782bca43f875687cd2be972144a7ab6b298454e
2021-01-21 03:42:42 +00:00
Umherirrender
0347fd0631 Improve some function documentation in tests
Also fix some whitespaces

Change-Id: Ibed50a4f07442d3f299cf545c16f5dbb5f27a411
2021-01-14 22:13:55 +01:00
Thiemo Kreuz
ac205fa84c Fix incomplete/bogus PHPDoc tags in various tests
Most of these are found by the not yet released I10559d8.

I remove the type MockObject in some cases when the calling
code really does not need to know if he get's a mock or the
real thing. However, I do this only in places that are very
closely related to the fixes.

Change-Id: I26a4c3c5a8ae141bf56161b52b54bce7e68f2e30
2021-01-14 19:02:00 +00:00
DannyS712
6a93b0ca93 More misc test cleanup
* parent::setUp() should be first, and ::tearDown()
  should be last
* Move tests that directly extend PHPUnit\Framework\TestCase
  to /unit

Change-Id: I1172855c58f4f52a8f624e6d596ec43beb8c93ff
2020-12-24 00:52:06 +00:00
Petr Pchelko
dbdc2a3cd3 Introduce JsonCodec to help with serialization/deserialization
Change-Id: I5433090ae8e2b3f2a4590cc404baf838025546ce
2020-11-19 08:32:21 -07:00
Petr Pchelko
7c68ae9296 Safe ParserOutput extension data and JsonUnserializable helper.
One major difference with what we've had before is that now we
actually write class names into the serialization - given that
this new mechanism is extencible, we can't establish any kind
of mapping of allowed classes. I do not think it's a problem
though.

Bug: T264394
Change-Id: Ia152f3b76b967aabde2d8a182e3aec7d3002e5ea
2020-11-10 11:21:09 -07:00
Petr Pchelko
1c70cca3ee Check if non-JSON-serializable data passed to ParserOutput
Bug: T264394
Change-Id: I6eedd03a81b95f6f55d25c00b31e01cbd8658d43
2020-10-05 10:54:08 -06:00
Thiemo Kreuz
6b2c9deef5 Replace all new stdClass() with identical (object)[]
This should be the exact same. Its more a style change than anything.
So why do it then?
* I believe this is much less confusing than code mentioning a weird
"standard class". Barely anybody knows what this is, and what the
difference between "object" and "stdClass" is.
* The code is shorter.
* It's even faster. In my micro benchmark it's twice as fast.

Change-Id: I7ee0e8ae6d9264a89b6cd1dd861f0466ae620ccc
2020-03-04 21:18:30 +00:00
Kunal Mehta
b9461e3f1c FormatJson: Improve parse() error code handling and tests
Three of the errors are encode errors that won't be emitted when we're
trying to decode JSON, so we can ignore those lines of code.

JSON_ERROR_UTF16 is a new error code in PHP 7.0, so add that in.

Improve test coverage while we're at it. The UTF16 test case was
copied from php-src/ext/json/tests/bug62010.phpt.

Change-Id: I79aa0db3d967d512611f8521bb052af36c3cda8e
2020-01-01 02:34:44 -08:00
Daimona Eaytoy
6365eaab8d Autofix 94 PHPUnit 8 compat issues
Done automatically using the master version of MW codesniffer and
running composer fix.

Bug: T192167
Change-Id: If6b40f515fde32ab5eff074a90e821c30c791827
2019-12-13 15:29:10 +01:00
Reedy
5b6477fb76 Tweak comment in FormatJsonUnitTest::testEmptyJsonKeyArray
Change-Id: I7625952f6afa2c8840dd9f52c0bd79ff29bfd91a
Follows-up: Ieed5662435c7a4070a7ac28ce74b8d51c8135c66
2019-10-12 23:32:12 +01:00
Reedy
3cd66f6ae9 Remove PHP < 7.1 case from FormatJsonUnitTest::testEmptyJsonKeyArray
Change-Id: Ieed5662435c7a4070a7ac28ce74b8d51c8135c66
2019-10-12 22:41:07 +01:00
Amir Sarabadani
06f645c453 Load GlobalFunctions.php to tests/phpunit/bootstrap.php
That mostly enables testing global functions

Bug: T87781
Change-Id: Ib42c56a67926ebcdba53f4c6c54a5bff98cb77a3
2019-07-14 01:28:07 +02:00
Amir Sarabadani
fe9863fd55 Move unit tests FormatJsonTest.php to a dedicated file in unit tests
Out of 140 tests of this file, 131 one of them are pure unit test
Let's keep the 9 in the original file and move the rest

Bug: T87781
Change-Id: I86dfe17f794c615048b3c20487b0e84d38d13b93
2019-07-07 21:35:40 -04:00
Legoktm
4e35134f7a Revert "Separate MediaWiki unit and integration tests"
This reverts commit 0a2b996278.

Reason for revert: Broke postgres tests.

Change-Id: I27d8e0c807ad5f0748b9611a4f3df84cc213fbe1
2019-06-13 23:00:08 +00:00
Máté Szabó
0a2b996278 Separate MediaWiki unit and integration tests
This changeset implements T89432 and related tickets and is based on exploration
done at the Prague Hackathon. The goal is to identify tests in MediaWiki core
that can be run without having to install & configure MediaWiki and its dependencies,
and provide a way to execute these tests via the standard phpunit entry point,
allowing for faster development and integration with existing tooling like IDEs.

The initial set of tests that met these criteria were identified using the work Amir did in
I88822667693d9e00ac3d4639c87bc24e5083e5e8. These tests were then moved into a new subdirectory
under phpunit/ and organized into a separate test suite. The environment for this suite
is set up via a PHPUnit bootstrap file without a custom entry point.

You can execute these tests by running:
$ vendor/bin/phpunit -d memory_limit=512M -c tests/phpunit/unit-tests.xml

Bug: T89432
Bug: T87781
Bug: T84948
Change-Id: Iad01033a0548afd4d2a6f2c1ef6fcc9debf72c0d
2019-06-13 22:56:31 +02:00