MongoCodeTest.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
  3. use Alcaeus\MongoDbAdapter\Tests\TestCase;
  4. use Alcaeus\MongoDbAdapter\TypeInterface;
  5. use ReflectionProperty;
  6. /**
  7. * @author alcaeus <alcaeus@alcaeus.org>
  8. */
  9. class MongoCodeTest extends TestCase
  10. {
  11. public function testCreate()
  12. {
  13. $code = new \MongoCode('code', ['scope' => 'bleh']);
  14. $this->assertSame('code', $this->getAttributeValue($code, 'code'));
  15. $this->assertSame(['scope' => 'bleh'], $this->getAttributeValue($code, 'scope'));
  16. $this->assertSame('code', (string) $code);
  17. return $code;
  18. }
  19. /**
  20. * @depends testCreate
  21. */
  22. public function testConvertToBson(\MongoCode $code)
  23. {
  24. $this->skipTestUnless($code instanceof TypeInterface);
  25. $bsonCode = $code->toBSONType();
  26. $this->assertInstanceOf('MongoDB\BSON\Javascript', $bsonCode);
  27. }
  28. public function testCreateWithBsonObject()
  29. {
  30. $this->skipTestUnless(in_array(TypeInterface::class, class_implements('MongoCode')));
  31. $bsonCode = new \MongoDB\BSON\Javascript('code', ['scope' => 'bleh']);
  32. $code = new \MongoCode($bsonCode);
  33. $this->assertSame('code', $this->getAttributeValue($code, 'code'));
  34. $this->assertSame(['scope' => 'bleh'], $this->getAttributeValue($code, 'scope'));
  35. }
  36. private function getAttributeValue(\MongoCode $code, $attribute)
  37. {
  38. $property = new ReflectionProperty($code, $attribute);
  39. $property->setAccessible(true);
  40. return $property->getValue($code);
  41. }
  42. }