MongoCodeTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. public function testCreateWithoutScope()
  20. {
  21. $code = new \MongoCode('code');
  22. $this->assertSame('code', $this->getAttributeValue($code, 'code'));
  23. $this->assertSame([], $this->getAttributeValue($code, 'scope'));
  24. $this->assertSame('code', (string) $code);
  25. return $code;
  26. }
  27. public function testConvertToBson()
  28. {
  29. $code = new \MongoCode('code', ['scope' => 'bleh']);
  30. $this->skipTestUnless($code instanceof TypeInterface);
  31. $bsonCode = $code->toBSONType();
  32. $this->assertInstanceOf('MongoDB\BSON\Javascript', $bsonCode);
  33. $this->assertSame('code', $bsonCode->getCode());
  34. $this->assertEquals((object) ['scope' => 'bleh'], $bsonCode->getScope());
  35. }
  36. public function testConvertToBsonWithoutScope()
  37. {
  38. $code = new \MongoCode('code');
  39. $this->skipTestUnless($code instanceof TypeInterface);
  40. $bsonCode = $code->toBSONType();
  41. $this->assertInstanceOf('MongoDB\BSON\Javascript', $bsonCode);
  42. $this->assertSame('code', $bsonCode->getCode());
  43. $this->assertNull($bsonCode->getScope());
  44. }
  45. public function testCreateWithBsonObject()
  46. {
  47. $this->skipTestUnless(in_array(TypeInterface::class, class_implements('MongoCode')));
  48. $bsonCode = new \MongoDB\BSON\Javascript('code', ['scope' => 'bleh']);
  49. $code = new \MongoCode($bsonCode);
  50. $this->assertSame('code', $this->getAttributeValue($code, 'code'));
  51. $this->assertSame(['scope' => 'bleh'], $this->getAttributeValue($code, 'scope'));
  52. }
  53. private function getAttributeValue(\MongoCode $code, $attribute)
  54. {
  55. $property = new ReflectionProperty($code, $attribute);
  56. $property->setAccessible(true);
  57. return $property->getValue($code);
  58. }
  59. }