MongoDbLegacyTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. $mongo = new \MongoClient();
  29. $this->module = new MongoDb(make_container());
  30. $this->module->_setConfig($this->mongoConfig);
  31. $this->module->_initialize();
  32. $this->db = $mongo->selectDB('test');
  33. $this->userCollection = $this->db->createCollection('users');
  34. $this->userCollection->insert(array('id' => 1, 'email' => 'miles@davis.com'));
  35. }
  36. protected function tearDown()
  37. {
  38. if (!is_null($this->userCollection)) {
  39. $this->userCollection->drop();
  40. }
  41. }
  42. public function testSeeInCollection()
  43. {
  44. $this->module->seeInCollection('users', array('email' => 'miles@davis.com'));
  45. }
  46. public function testDontSeeInCollection()
  47. {
  48. $this->module->dontSeeInCollection('users', array('email' => 'davert@davert.com'));
  49. }
  50. public function testHaveAndSeeInCollection()
  51. {
  52. $this->module->haveInCollection('users', array('name' => 'John', 'email' => 'john@coltrane.com'));
  53. $this->module->seeInCollection('users', array('name' => 'John', 'email' => 'john@coltrane.com'));
  54. }
  55. public function testGrabFromCollection()
  56. {
  57. $user = $this->module->grabFromCollection('users', array('id' => 1));
  58. $this->assertTrue(isset($user['email']));
  59. $this->assertEquals('miles@davis.com', $user['email']);
  60. }
  61. public function testSeeNumElementsInCollection()
  62. {
  63. $this->module->seeNumElementsInCollection('users', 1);
  64. $this->module->seeNumElementsInCollection('users', 1, array('email' => 'miles@davis.com'));
  65. $this->module->seeNumElementsInCollection('users', 0, array('name' => 'Doe'));
  66. }
  67. public function testGrabCollectionCount()
  68. {
  69. $this->userCollection->insert(array('id' => 2, 'email' => 'louis@armstrong.com'));
  70. $this->userCollection->insert(array('id' => 3, 'email' => 'dizzy@gillespie.com'));
  71. $this->assertEquals(1, $this->module->grabCollectionCount('users', array('id' => 3)));
  72. $this->assertEquals(3, $this->module->grabCollectionCount('users'));
  73. }
  74. public function testSeeElementIsArray()
  75. {
  76. $this->userCollection->insert(array('id' => 4, 'trumpets' => array('piccolo', 'bass', 'slide')));
  77. $this->module->seeElementIsArray('users', array('id' => 4), 'trumpets');
  78. }
  79. public function testSeeElementIsArrayThrowsError()
  80. {
  81. $this->setExpectedException('PHPUnit_Framework_ExpectationFailedException');
  82. $this->userCollection->insert(array('id' => 5, 'trumpets' => array('piccolo', 'bass', 'slide')));
  83. $this->userCollection->insert(array('id' => 6, 'trumpets' => array('piccolo', 'bass', 'slide')));
  84. $this->module->seeElementIsArray('users', array(), 'trumpets');
  85. }
  86. public function testSeeElementIsObject()
  87. {
  88. $trumpet = new \StdClass;
  89. $trumpet->name = 'Trumpet 1';
  90. $trumpet->pitch = 'B♭';
  91. $trumpet->price = array('min' => 458, 'max' => 891);
  92. $this->userCollection->insert(array('id' => 6, 'trumpet' => $trumpet));
  93. $this->module->seeElementIsObject('users', array('id' => 6), 'trumpet');
  94. }
  95. public function testSeeElementIsObjectThrowsError()
  96. {
  97. $trumpet = new \StdClass;
  98. $trumpet->name = 'Trumpet 1';
  99. $trumpet->pitch = 'B♭';
  100. $trumpet->price = array('min' => 458, 'max' => 891);
  101. $this->setExpectedException('PHPUnit_Framework_ExpectationFailedException');
  102. $this->userCollection->insert(array('id' => 5, 'trumpet' => $trumpet));
  103. $this->userCollection->insert(array('id' => 6, 'trumpet' => $trumpet));
  104. $this->module->seeElementIsObject('users', array(), 'trumpet');
  105. }
  106. public function testUseDatabase()
  107. {
  108. $this->module->useDatabase('example');
  109. $this->module->haveInCollection('stuff', array('name' => 'Ashley', 'email' => 'me@ashleyclarke.me'));
  110. $this->module->seeInCollection('stuff', array('name' => 'Ashley', 'email' => 'me@ashleyclarke.me'));
  111. $this->module->dontSeeInCollection('users', array('email' => 'miles@davis.com'));
  112. }
  113. }