Skip to content

Commit

Permalink
Merge pull request #44 from xp-forge/feature/equals
Browse files Browse the repository at this point in the history
Add equals() methods for wrapper classes
  • Loading branch information
thekid authored Dec 30, 2023
2 parents dd50d89 + e790eca commit 2aad76d
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/php/com/mongodb/Code.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,14 @@ public function toString() { return nameof($this).'(`'.$this->source.'`)'; }
public function compareTo($value) {
return $value instanceof self ? $this->source <=> $value->source : 1;
}

/**
* Test for equality
*
* @param var $value
* @return bool
*/
public function equals($value) {
return $value instanceof self ? 0 === ($this->source <=> $value->source) : false;
}
}
10 changes: 10 additions & 0 deletions src/main/php/com/mongodb/Decimal128.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,14 @@ public function toString() { return nameof($this).'('.$this->__toString().')'; }
public function compareTo($value) {
return $value instanceof self ? $this->__toString() <=> $value->__toString() : 1;
}

/**
* Test for equality
*
* @param var $value
* @return bool
*/
public function equals($value) {
return $value instanceof self ? 0 === ($this->__toString() <=> $value->__toString()) : false;
}
}
10 changes: 10 additions & 0 deletions src/main/php/com/mongodb/Int64.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,14 @@ public function toString() { return nameof($this).'('.$this->number.')'; }
public function compareTo($value) {
return $value instanceof self ? $this->number <=> $value->number : 1;
}

/**
* Test for equality
*
* @param var $value
* @return bool
*/
public function equals($value) {
return $value instanceof self ? 0 === ($this->number <=> $value->number) : false;
}
}
10 changes: 10 additions & 0 deletions src/main/php/com/mongodb/ObjectId.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,14 @@ public function toString() { return nameof($this).'('.$this->string.')'; }
public function compareTo($value) {
return $value instanceof self ? $this->string <=> $value->string : 1;
}

/**
* Test for equality
*
* @param var $value
* @return bool
*/
public function equals($value) {
return $value instanceof self ? 0 === ($this->string <=> $value->string) : false;
}
}
6 changes: 6 additions & 0 deletions src/test/php/com/mongodb/unittest/CodeTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ public function compare() {
Assert::equals(0, (new Code(self::SOURCE))->compareTo(new Code(self::SOURCE)));
Assert::equals(1, (new Code(self::SOURCE))->compareTo(new Code('')));
}

#[Test]
public function equals() {
Assert::true((new Code(self::SOURCE))->equals(new Code(self::SOURCE)));
Assert::false((new Code(self::SOURCE))->equals(new Code('')));
}
}
8 changes: 8 additions & 0 deletions src/test/php/com/mongodb/unittest/Decimal128Test.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,12 @@ public function comparison($n) {
Assert::equals(new Decimal128($n), $fixture);
Assert::notEquals(new Decimal128(6100), $fixture);
}

#[Test, Values(from: 'numbers')]
public function equals($n) {
$fixture= new Decimal128($n);

Assert::true((new Decimal128($n))->equals($fixture));
Assert::false((new Decimal128(6100))->equals($fixture));
}
}
8 changes: 8 additions & 0 deletions src/test/php/com/mongodb/unittest/Int64Test.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ public function comparison($n) {
Assert::equals(new Int64($n), $fixture);
Assert::notEquals(new Int64(6100), $fixture);
}

#[Test, Values(from: 'numbers')]
public function equals($n) {
$fixture= new Int64($n);

Assert::true((new Int64($n))->equals($fixture));
Assert::false((new Int64(6100))->equals($fixture));
}
}
6 changes: 6 additions & 0 deletions src/test/php/com/mongodb/unittest/ObjectIdTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public function larger_than() {
Assert::equals(1, (new ObjectId(self::ID))->compareTo(new ObjectId('4f1dda9973edf2501751884b')));
}

#[Test]
public function equals() {
Assert::true((new ObjectId(self::ID))->equals(new ObjectId(self::ID)));
Assert::false((new ObjectId(self::ID))->equals(new ObjectId('4f1dda9973edf2501751884b')));
}

#[Test]
public function create() {
Assert::matches('/^[0-9a-f]{24}$/', ObjectId::create());
Expand Down

0 comments on commit 2aad76d

Please sign in to comment.