1+ <?php namespace com \mongodb \unittest ;
2+
3+ use com \mongodb \{Collection , Document };
4+ use test \{Assert , Test };
5+
6+ class CollectionQueryTest {
7+ use WireTesting;
8+
9+ const FIRST = ['_id ' => 'one ' , 'name ' => 'A ' ];
10+ const SECOND = ['_id ' => 'one ' , 'name ' => 'A ' ];
11+ const DOCUMENTS = [self ::FIRST , self ::SECOND ];
12+ const QUERY = ['$db ' => 'testing ' , '$readPreference ' => ['mode ' => 'primary ' ]];
13+
14+ /**
15+ * Returns a new fixture
16+ *
17+ * @param var... $messages
18+ * @return com.mongodb.io.Protocol
19+ */
20+ private function newFixture (... $ messages ) {
21+ return $ this ->protocol ([self ::$ PRIMARY => [$ this ->hello (self ::$ PRIMARY ), ...$ messages ]], 'primary ' )->connect ();
22+ }
23+
24+ #[Test]
25+ public function query () {
26+ $ protocol = $ this ->newFixture ($ this ->cursor (self ::DOCUMENTS ));
27+ Assert::equals (
28+ [new Document (self ::FIRST ), new Document (self ::SECOND )],
29+ (new Collection ($ protocol , 'testing ' , 'tests ' ))->query ()->all ()
30+ );
31+ Assert::equals (
32+ ['find ' => 'tests ' , 'filter ' => (object )[]] + self ::QUERY ,
33+ current ($ protocol ->connections ())->command (1 )
34+ );
35+ }
36+
37+ #[Test]
38+ public function first () {
39+ $ protocol = $ this ->newFixture ($ this ->cursor (self ::DOCUMENTS ));
40+ Assert::equals (
41+ new Document (self ::FIRST ),
42+ (new Collection ($ protocol , 'testing ' , 'tests ' ))->first ()
43+ );
44+ Assert::equals (
45+ ['find ' => 'tests ' , 'filter ' => (object )[]] + self ::QUERY ,
46+ current ($ protocol ->connections ())->command (1 )
47+ );
48+ }
49+
50+ #[Test]
51+ public function first_with_empty () {
52+ $ protocol = $ this ->newFixture ($ this ->cursor (self ::DOCUMENTS ));
53+ Assert::equals (
54+ new Document (self ::FIRST ),
55+ (new Collection ($ protocol , 'testing ' , 'tests ' ))->first ([])
56+ );
57+ Assert::equals (
58+ ['find ' => 'tests ' , 'filter ' => (object )[]] + self ::QUERY ,
59+ current ($ protocol ->connections ())->command (1 )
60+ );
61+ }
62+
63+ #[Test]
64+ public function first_with_id () {
65+ $ protocol = $ this ->newFixture ($ this ->cursor (self ::DOCUMENTS ));
66+ Assert::equals (
67+ new Document (self ::FIRST ),
68+ (new Collection ($ protocol , 'testing ' , 'tests ' ))->first ('one ' )
69+ );
70+ Assert::equals (
71+ ['find ' => 'tests ' , 'filter ' => ['_id ' => 'one ' ]] + self ::QUERY ,
72+ current ($ protocol ->connections ())->command (1 )
73+ );
74+ }
75+
76+ #[Test]
77+ public function first_with_query () {
78+ $ protocol = $ this ->newFixture ($ this ->cursor (self ::DOCUMENTS ));
79+ Assert::equals (
80+ new Document (self ::FIRST ),
81+ (new Collection ($ protocol , 'testing ' , 'tests ' ))->first (['_id ' => 'one ' ])
82+ );
83+ Assert::equals (
84+ ['find ' => 'tests ' , 'filter ' => ['_id ' => 'one ' ]] + self ::QUERY ,
85+ current ($ protocol ->connections ())->command (1 )
86+ );
87+ }
88+
89+ #[Test]
90+ public function first_with_pipeline () {
91+ $ pipeline = [['$match ' => ['_id ' => 'one ' ]]];
92+ $ protocol = $ this ->newFixture ($ this ->cursor (self ::DOCUMENTS ));
93+ Assert::equals (
94+ new Document (self ::FIRST ),
95+ (new Collection ($ protocol , 'testing ' , 'tests ' ))->first ($ pipeline )
96+ );
97+ Assert::equals (
98+ ['aggregate ' => 'tests ' , 'pipeline ' => $ pipeline , 'cursor ' => (object )[]] + self ::QUERY ,
99+ current ($ protocol ->connections ())->command (1 )
100+ );
101+ }
102+
103+ #[Test]
104+ public function first_without_result () {
105+ $ protocol = $ this ->newFixture ($ this ->cursor ([]));
106+ Assert::null ((new Collection ($ protocol , 'testing ' , 'tests ' ))->first ());
107+ }
108+ }
0 commit comments