Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add equals() methods for wrapper classes #44

Merged
merged 2 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading