@@ -88,4 +88,102 @@ public function test_batch_is_deleted_from_storage_if_exception_thrown_during_ba
88
88
89
89
$ pendingBatch ->dispatch ();
90
90
}
91
+
92
+ public function test_batch_is_dispatched_when_dispatchif_is_true ()
93
+ {
94
+ $ container = new Container ;
95
+
96
+ $ eventDispatcher = m::mock (Dispatcher::class);
97
+ $ eventDispatcher ->shouldReceive ('dispatch ' )->once ();
98
+ $ container ->instance (Dispatcher::class, $ eventDispatcher );
99
+
100
+ $ job = new class
101
+ {
102
+ use Batchable;
103
+ };
104
+
105
+ $ pendingBatch = new PendingBatch ($ container , new Collection ([$ job ]));
106
+
107
+ $ repository = m::mock (BatchRepository::class);
108
+ $ repository ->shouldReceive ('store ' )->once ()->andReturn ($ batch = m::mock (stdClass::class));
109
+ $ batch ->shouldReceive ('add ' )->once ()->andReturn ($ batch = m::mock (Batch::class));
110
+
111
+ $ container ->instance (BatchRepository::class, $ repository );
112
+
113
+ $ result = $ pendingBatch ->dispatchIf (true );
114
+
115
+ $ this ->assertInstanceOf (Batch::class, $ result );
116
+ }
117
+
118
+ public function test_batch_is_not_dispatched_when_dispatchif_is_false ()
119
+ {
120
+ $ container = new Container ;
121
+
122
+ $ eventDispatcher = m::mock (Dispatcher::class);
123
+ $ eventDispatcher ->shouldNotReceive ('dispatch ' );
124
+ $ container ->instance (Dispatcher::class, $ eventDispatcher );
125
+
126
+ $ job = new class
127
+ {
128
+ use Batchable;
129
+ };
130
+
131
+ $ pendingBatch = new PendingBatch ($ container , new Collection ([$ job ]));
132
+
133
+ $ repository = m::mock (BatchRepository::class);
134
+ $ container ->instance (BatchRepository::class, $ repository );
135
+
136
+ $ result = $ pendingBatch ->dispatchIf (false );
137
+
138
+ $ this ->assertNull ($ result );
139
+ }
140
+
141
+ public function test_batch_is_dispatched_when_dispatchunless_is_false ()
142
+ {
143
+ $ container = new Container ;
144
+
145
+ $ eventDispatcher = m::mock (Dispatcher::class);
146
+ $ eventDispatcher ->shouldReceive ('dispatch ' )->once ();
147
+ $ container ->instance (Dispatcher::class, $ eventDispatcher );
148
+
149
+ $ job = new class
150
+ {
151
+ use Batchable;
152
+ };
153
+
154
+ $ pendingBatch = new PendingBatch ($ container , new Collection ([$ job ]));
155
+
156
+ $ repository = m::mock (BatchRepository::class);
157
+ $ repository ->shouldReceive ('store ' )->once ()->andReturn ($ batch = m::mock (stdClass::class));
158
+ $ batch ->shouldReceive ('add ' )->once ()->andReturn ($ batch = m::mock (Batch::class));
159
+
160
+ $ container ->instance (BatchRepository::class, $ repository );
161
+
162
+ $ result = $ pendingBatch ->dispatchUnless (false );
163
+
164
+ $ this ->assertInstanceOf (Batch::class, $ result );
165
+ }
166
+
167
+ public function test_batch_is_not_dispatched_when_dispatchunless_is_true ()
168
+ {
169
+ $ container = new Container ;
170
+
171
+ $ eventDispatcher = m::mock (Dispatcher::class);
172
+ $ eventDispatcher ->shouldNotReceive ('dispatch ' );
173
+ $ container ->instance (Dispatcher::class, $ eventDispatcher );
174
+
175
+ $ job = new class
176
+ {
177
+ use Batchable;
178
+ };
179
+
180
+ $ pendingBatch = new PendingBatch ($ container , new Collection ([$ job ]));
181
+
182
+ $ repository = m::mock (BatchRepository::class);
183
+ $ container ->instance (BatchRepository::class, $ repository );
184
+
185
+ $ result = $ pendingBatch ->dispatchUnless (true );
186
+
187
+ $ this ->assertNull ($ result );
188
+ }
91
189
}
0 commit comments