Skip to content

Commit 368368e

Browse files
committed
PHPLIB-462: Add compatibility layer for constraint changes
1 parent d7aed71 commit 368368e

File tree

4 files changed

+202
-38
lines changed

4 files changed

+202
-38
lines changed

tests/Compat/ConstraintTrait.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Compat;
4+
5+
use PHPUnit\Framework\Constraint\Constraint;
6+
use ReflectionClass;
7+
8+
$r = new ReflectionClass(Constraint::class);
9+
if (\PHP_VERSION_ID < 70000 || ! $r->getMethod('matches')->hasReturnType()) {
10+
/**
11+
* @internal
12+
*/
13+
trait ConstraintTrait
14+
{
15+
use Legacy\ConstraintTraitForV6;
16+
}
17+
} else {
18+
/**
19+
* @internal
20+
*/
21+
trait ConstraintTrait
22+
{
23+
use Legacy\ConstraintTraitForV7;
24+
}
25+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Compat\Legacy;
4+
5+
use SebastianBergmann\Exporter\Exporter;
6+
7+
/**
8+
* @internal
9+
*/
10+
trait ConstraintTraitForV6
11+
{
12+
/**
13+
* @return int
14+
*/
15+
public function count()
16+
{
17+
return $this->doCount();
18+
}
19+
20+
/**
21+
* @return string
22+
*/
23+
public function toString()
24+
{
25+
return $this->doToString();
26+
}
27+
28+
/**
29+
* @param mixed $other
30+
*
31+
* @return string
32+
*/
33+
protected function additionalFailureDescription($other)
34+
{
35+
return $this->doAdditionalFailureDescription($other);
36+
}
37+
38+
/**
39+
* @return Exporter
40+
*/
41+
protected function exporter()
42+
{
43+
return $this->exporter;
44+
}
45+
46+
/**
47+
* @param mixed $other
48+
*
49+
* @return string
50+
*/
51+
protected function failureDescription($other)
52+
{
53+
return $this->doFailureDescription($other);
54+
}
55+
56+
/**
57+
* @param mixed $other
58+
*
59+
* @return bool
60+
*/
61+
protected function matches($other)
62+
{
63+
return $this->doMatches($other);
64+
}
65+
66+
private function doAdditionalFailureDescription($other)
67+
{
68+
return '';
69+
}
70+
71+
private function doCount()
72+
{
73+
return 1;
74+
}
75+
76+
private function doFailureDescription($other)
77+
{
78+
return $this->exporter()->export($other) . ' ' . $this->toString();
79+
}
80+
81+
private function doMatches($other)
82+
{
83+
return false;
84+
}
85+
86+
private function doToString()
87+
{
88+
return '';
89+
}
90+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Compat\Legacy;
4+
5+
/**
6+
* @internal
7+
*/
8+
trait ConstraintTraitForV7
9+
{
10+
public function count(): int
11+
{
12+
return $this->doCount();
13+
}
14+
15+
public function toString(): string
16+
{
17+
return $this->doToString();
18+
}
19+
20+
protected function additionalFailureDescription($other): string
21+
{
22+
return $this->doAdditionalFailureDescription($other);
23+
}
24+
25+
protected function failureDescription($other): string
26+
{
27+
return $this->doFailureDescription($other);
28+
}
29+
30+
protected function matches($other): bool
31+
{
32+
return $this->doMatches($other);
33+
}
34+
35+
private function doAdditionalFailureDescription($other): string
36+
{
37+
return '';
38+
}
39+
40+
private function doCount(): int
41+
{
42+
return 1;
43+
}
44+
45+
private function doFailureDescription($other): string
46+
{
47+
return $this->exporter()->export($other) . ' ' . $this->toString();
48+
}
49+
50+
private function doMatches($other): bool
51+
{
52+
return false;
53+
}
54+
55+
private function doToString(): string
56+
{
57+
return '';
58+
}
59+
}

tests/SpecTests/DocumentsMatchConstraint.php

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use MongoDB\Model\BSONArray;
66
use MongoDB\Model\BSONDocument;
7+
use MongoDB\Tests\Compat\ConstraintTrait;
78
use PHPUnit\Framework\Constraint\Constraint;
89
use ArrayObject;
910
use InvalidArgumentException;
@@ -17,6 +18,8 @@
1718
*/
1819
class DocumentsMatchConstraint extends Constraint
1920
{
21+
use ConstraintTrait;
22+
2023
private $ignoreExtraKeysInRoot = false;
2124
private $ignoreExtraKeysInEmbedded = false;
2225
private $placeholders = [];
@@ -38,50 +41,12 @@ class DocumentsMatchConstraint extends Constraint
3841
*/
3942
public function __construct($value, $ignoreExtraKeysInRoot = false, $ignoreExtraKeysInEmbedded = false, array $placeholders = [])
4043
{
41-
parent::__construct();
4244
$this->value = $this->prepareBSON($value, true, $this->sortKeys);
4345
$this->ignoreExtraKeysInRoot = $ignoreExtraKeysInRoot;
4446
$this->ignoreExtraKeysInEmbedded = $ignoreExtraKeysInEmbedded;
4547
$this->placeholders = $placeholders;
4648
}
4749

48-
/**
49-
* Returns a string representation of the constraint.
50-
*
51-
* @return string
52-
*/
53-
public function toString()
54-
{
55-
return 'matches ' . json_encode($this->value);
56-
}
57-
58-
/**
59-
* Evaluates the constraint for parameter $other. Returns true if the
60-
* constraint is met, false otherwise.
61-
*
62-
* @param mixed $other
63-
* @return boolean
64-
*/
65-
protected function matches($other)
66-
{
67-
/* TODO: If ignoreExtraKeys and sortKeys are both false, then we may be
68-
* able to skip preparation, convert both documents to extended JSON,
69-
* and compare strings.
70-
*
71-
* If ignoreExtraKeys is false and sortKeys is true, we still be able to
72-
* compare JSON strings but will still require preparation to sort keys
73-
* in all documents and sub-documents. */
74-
$other = $this->prepareBSON($other, true, $this->sortKeys);
75-
76-
try {
77-
$this->assertEquals($this->value, $other, $this->ignoreExtraKeysInRoot);
78-
} catch (RuntimeException $e) {
79-
return false;
80-
}
81-
82-
return true;
83-
}
84-
8550
/**
8651
* Compares two documents recursively.
8752
*
@@ -131,6 +96,31 @@ private function assertEquals(ArrayObject $expected, ArrayObject $actual, $ignor
13196
}
13297
}
13398

99+
private function doMatches($other)
100+
{
101+
/* TODO: If ignoreExtraKeys and sortKeys are both false, then we may be
102+
* able to skip preparation, convert both documents to extended JSON,
103+
* and compare strings.
104+
*
105+
* If ignoreExtraKeys is false and sortKeys is true, we still be able to
106+
* compare JSON strings but will still require preparation to sort keys
107+
* in all documents and sub-documents. */
108+
$other = $this->prepareBSON($other, true, $this->sortKeys);
109+
110+
try {
111+
$this->assertEquals($this->value, $other, $this->ignoreExtraKeysInRoot);
112+
} catch (RuntimeException $e) {
113+
return false;
114+
}
115+
116+
return true;
117+
}
118+
119+
private function doToString()
120+
{
121+
return 'matches ' . json_encode($this->value);
122+
}
123+
134124
/**
135125
* Prepare a BSON document or array for comparison.
136126
*

0 commit comments

Comments
 (0)