MongoDbTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. use Codeception\Module\MongoDb;
  3. use Codeception\Exception\ModuleException;
  4. use Codeception\Test\Unit;
  5. class MongoDbTest extends Unit
  6. {
  7. /**
  8. * @var array
  9. */
  10. private $mongoConfig = array(
  11. 'dsn' => 'mongodb://localhost:27017/test?connectTimeoutMS=300',
  12. 'dump' => 'tests/data/dumps/mongo.js',
  13. 'populate' => true
  14. );
  15. /**
  16. * @var MongoDb
  17. */
  18. protected $module;
  19. /**
  20. * @var \MongoDB\Database
  21. */
  22. protected $db;
  23. /**
  24. * @var \MongoDB\Collection
  25. */
  26. private $userCollection;
  27. protected function setUp()
  28. {
  29. if (!class_exists('\MongoDB\Client')) {
  30. $this->markTestSkipped('MongoDB is not installed');
  31. }
  32. $mongo = new \MongoDB\Client();
  33. $this->module = new MongoDb(make_container());
  34. $this->module->_setConfig($this->mongoConfig);
  35. try {
  36. $this->module->_initialize();
  37. } catch (ModuleException $e) {
  38. $this->markTestSkipped($e->getMessage());
  39. }
  40. $this->db = $mongo->selectDatabase('test');
  41. $this->userCollection = $this->db->users;
  42. $this->userCollection->insertOne(array('id' => 1, 'email' => 'miles@davis.com'));
  43. }
  44. protected function tearDown()
  45. {
  46. if (!is_null($this->userCollection)) {
  47. $this->userCollection->drop();
  48. }
  49. }
  50. public function testSeeInCollection()
  51. {
  52. $this->module->seeInCollection('users', array('email' => 'miles@davis.com'));
  53. }
  54. public function testDontSeeInCollection()
  55. {
  56. $this->module->dontSeeInCollection('users', array('email' => 'davert@davert.com'));
  57. }
  58. public function testHaveAndSeeInCollection()
  59. {
  60. $this->module->haveInCollection('users', array('name' => 'John', 'email' => 'john@coltrane.com'));
  61. $this->module->seeInCollection('users', array('name' => 'John', 'email' => 'john@coltrane.com'));
  62. }
  63. public function testGrabFromCollection()
  64. {
  65. $user = $this->module->grabFromCollection('users', array('id' => 1));
  66. $this->assertArrayHasKey('email', $user);
  67. $this->assertEquals('miles@davis.com', $user['email']);
  68. }
  69. public function testSeeNumElementsInCollection()
  70. {
  71. $this->module->seeNumElementsInCollection('users', 1);
  72. $this->module->seeNumElementsInCollection('users', 1, array('email' => 'miles@davis.com'));
  73. $this->module->seeNumElementsInCollection('users', 0, array('name' => 'Doe'));
  74. }
  75. public function testGrabCollectionCount()
  76. {
  77. $this->userCollection->insertOne(array('id' => 2, 'email' => 'louis@armstrong.com'));
  78. $this->userCollection->insertOne(array('id' => 3, 'email' => 'dizzy@gillespie.com'));
  79. $this->assertEquals(1, $this->module->grabCollectionCount('users', array('id' => 3)));
  80. $this->assertEquals(3, $this->module->grabCollectionCount('users'));
  81. }
  82. public function testSeeElementIsArray()
  83. {
  84. $this->userCollection->insertOne(array('id' => 4, 'trumpets' => array('piccolo', 'bass', 'slide')));
  85. $this->module->seeElementIsArray('users', array('id' => 4), 'trumpets');
  86. }
  87. public function testSeeElementIsArrayThrowsError()
  88. {
  89. $this->setExpectedException('PHPUnit\Framework\ExpectationFailedException');
  90. $this->userCollection->insertOne(array('id' => 5, 'trumpets' => array('piccolo', 'bass', 'slide')));
  91. $this->userCollection->insertOne(array('id' => 6, 'trumpets' => array('piccolo', 'bass', 'slide')));
  92. $this->module->seeElementIsArray('users', array(), 'trumpets');
  93. }
  94. public function testSeeElementIsObject()
  95. {
  96. $trumpet = new \StdClass;
  97. $trumpet->name = 'Trumpet 1';
  98. $trumpet->pitch = 'B♭';
  99. $trumpet->price = array('min' => 458, 'max' => 891);
  100. $this->userCollection->insertOne(array('id' => 6, 'trumpet' => $trumpet));
  101. $this->module->seeElementIsObject('users', array('id' => 6), 'trumpet');
  102. }
  103. public function testSeeElementIsObjectThrowsError()
  104. {
  105. $trumpet = new \StdClass;
  106. $trumpet->name = 'Trumpet 1';
  107. $trumpet->pitch = 'B♭';
  108. $trumpet->price = array('min' => 458, 'max' => 891);
  109. $this->setExpectedException('PHPUnit\Framework\ExpectationFailedException');
  110. $this->userCollection->insertOne(array('id' => 5, 'trumpet' => $trumpet));
  111. $this->userCollection->insertOne(array('id' => 6, 'trumpet' => $trumpet));
  112. $this->module->seeElementIsObject('users', array(), 'trumpet');
  113. }
  114. public function testUseDatabase()
  115. {
  116. $this->module->useDatabase('example');
  117. $this->module->haveInCollection('stuff', array('name' => 'Ashley', 'email' => 'me@ashleyclarke.me'));
  118. $this->module->seeInCollection('stuff', array('name' => 'Ashley', 'email' => 'me@ashleyclarke.me'));
  119. $this->module->dontSeeInCollection('users', array('email' => 'miles@davis.com'));
  120. }
  121. public function testLoadDump()
  122. {
  123. $testRecords = [
  124. ['name' => 'Michael Jordan', 'position' => 'sg'],
  125. ['name' => 'Ron Harper','position' => 'pg'],
  126. ['name' => 'Steve Kerr','position' => 'pg'],
  127. ['name' => 'Toni Kukoc','position' => 'sf'],
  128. ['name' => 'Luc Longley','position' => 'c'],
  129. ['name' => 'Scottie Pippen','position' => 'sf'],
  130. ['name' => 'Dennis Rodman','position' => 'pf']
  131. ];
  132. foreach ($testRecords as $testRecord) {
  133. $this->module->haveInCollection('96_bulls', $testRecord);
  134. }
  135. }
  136. }