MongoDbLegacyTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. use Codeception\Module\MongoDb;
  3. class MongoDbLegacyTest extends \PHPUnit\Framework\TestCase
  4. {
  5. /**
  6. * @var array
  7. */
  8. private $mongoConfig = array(
  9. 'dsn' => 'mongodb://localhost:27017/test'
  10. );
  11. /**
  12. * @var MongoDb
  13. */
  14. protected $module;
  15. /**
  16. * @var \MongoDb
  17. */
  18. protected $db;
  19. /**
  20. * @var \MongoCollection
  21. */
  22. private $userCollection;
  23. protected function setUp()
  24. {
  25. if (!class_exists('Mongo')) {
  26. $this->markTestSkipped('Mongo is not installed');
  27. }
  28. if (!class_exists('MongoDB\Client')) {
  29. $this->markTestSkipped('MongoDb\Client is not installed');
  30. }
  31. $mongo = new \MongoClient();
  32. $this->module = new MongoDb(make_container());
  33. $this->module->_setConfig($this->mongoConfig);
  34. $this->module->_initialize();
  35. $this->db = $mongo->selectDB('test');
  36. $this->userCollection = $this->db->createCollection('users');
  37. $this->userCollection->insert(array('id' => 1, 'email' => 'miles@davis.com'));
  38. }
  39. protected function tearDown()
  40. {
  41. if (!is_null($this->userCollection)) {
  42. $this->userCollection->drop();
  43. }
  44. }
  45. public function testSeeInCollection()
  46. {
  47. $this->module->seeInCollection('users', array('email' => 'miles@davis.com'));
  48. }
  49. public function testDontSeeInCollection()
  50. {
  51. $this->module->dontSeeInCollection('users', array('email' => 'davert@davert.com'));
  52. }
  53. public function testHaveAndSeeInCollection()
  54. {
  55. $this->module->haveInCollection('users', array('name' => 'John', 'email' => 'john@coltrane.com'));
  56. $this->module->seeInCollection('users', array('name' => 'John', 'email' => 'john@coltrane.com'));
  57. }
  58. public function testGrabFromCollection()
  59. {
  60. $user = $this->module->grabFromCollection('users', array('id' => 1));
  61. $this->assertArrayHasKey('email', $user);
  62. $this->assertEquals('miles@davis.com', $user['email']);
  63. }
  64. public function testSeeNumElementsInCollection()
  65. {
  66. $this->module->seeNumElementsInCollection('users', 1);
  67. $this->module->seeNumElementsInCollection('users', 1, array('email' => 'miles@davis.com'));
  68. $this->module->seeNumElementsInCollection('users', 0, array('name' => 'Doe'));
  69. }
  70. public function testGrabCollectionCount()
  71. {
  72. $this->userCollection->insert(array('id' => 2, 'email' => 'louis@armstrong.com'));
  73. $this->userCollection->insert(array('id' => 3, 'email' => 'dizzy@gillespie.com'));
  74. $this->assertEquals(1, $this->module->grabCollectionCount('users', array('id' => 3)));
  75. $this->assertEquals(3, $this->module->grabCollectionCount('users'));
  76. }
  77. public function testSeeElementIsArray()
  78. {
  79. $this->userCollection->insert(array('id' => 4, 'trumpets' => array('piccolo', 'bass', 'slide')));
  80. $this->module->seeElementIsArray('users', array('id' => 4), 'trumpets');
  81. }
  82. public function testSeeElementIsArrayThrowsError()
  83. {
  84. $this->setExpectedException('PHPUnit\Framework\ExpectationFailedException');
  85. $this->userCollection->insert(array('id' => 5, 'trumpets' => array('piccolo', 'bass', 'slide')));
  86. $this->userCollection->insert(array('id' => 6, 'trumpets' => array('piccolo', 'bass', 'slide')));
  87. $this->module->seeElementIsArray('users', array(), 'trumpets');
  88. }
  89. public function testSeeElementIsObject()
  90. {
  91. $trumpet = new \StdClass;
  92. $trumpet->name = 'Trumpet 1';
  93. $trumpet->pitch = 'B♭';
  94. $trumpet->price = array('min' => 458, 'max' => 891);
  95. $this->userCollection->insert(array('id' => 6, 'trumpet' => $trumpet));
  96. $this->module->seeElementIsObject('users', array('id' => 6), 'trumpet');
  97. }
  98. public function testSeeElementIsObjectThrowsError()
  99. {
  100. $trumpet = new \StdClass;
  101. $trumpet->name = 'Trumpet 1';
  102. $trumpet->pitch = 'B♭';
  103. $trumpet->price = array('min' => 458, 'max' => 891);
  104. $this->setExpectedException('PHPUnit\Framework\ExpectationFailedException');
  105. $this->userCollection->insert(array('id' => 5, 'trumpet' => $trumpet));
  106. $this->userCollection->insert(array('id' => 6, 'trumpet' => $trumpet));
  107. $this->module->seeElementIsObject('users', array(), 'trumpet');
  108. }
  109. public function testUseDatabase()
  110. {
  111. $this->module->useDatabase('example');
  112. $this->module->haveInCollection('stuff', array('name' => 'Ashley', 'email' => 'me@ashleyclarke.me'));
  113. $this->module->seeInCollection('stuff', array('name' => 'Ashley', 'email' => 'me@ashleyclarke.me'));
  114. $this->module->dontSeeInCollection('users', array('email' => 'miles@davis.com'));
  115. }
  116. }