|
5 | 5 | * @package WP-Background-Processing |
6 | 6 | */ |
7 | 7 |
|
| 8 | + require_once __DIR__ . '/fixtures/Test_Batch_Data.php'; |
| 9 | + |
8 | 10 | use PHPUnit\Framework\MockObject\MockObject; |
9 | 11 |
|
10 | 12 | /** |
@@ -51,6 +53,25 @@ private function getWPBPProperty( string $name ) { |
51 | 53 | return $property->getValue( $this->wpbp ); |
52 | 54 | } |
53 | 55 |
|
| 56 | + /** |
| 57 | + * Set a property value on WPBP regardless of accessibility. |
| 58 | + * |
| 59 | + * @param string $name |
| 60 | + * @param mixed $value |
| 61 | + * |
| 62 | + * @return mixed |
| 63 | + */ |
| 64 | + private function setWPBPProperty( string $name, $value ) { |
| 65 | + try { |
| 66 | + $property = new ReflectionProperty( 'WP_Background_Process', $name ); |
| 67 | + } catch ( Exception $e ) { |
| 68 | + return new WP_Error( $e->getCode(), $e->getMessage() ); |
| 69 | + } |
| 70 | + $property->setAccessible( true ); |
| 71 | + |
| 72 | + return $property->setValue( $this->wpbp, $value ); |
| 73 | + } |
| 74 | + |
54 | 75 | /** |
55 | 76 | * Execute a method of WPBP regardless of accessibility. |
56 | 77 | * |
@@ -178,6 +199,41 @@ public function test_get_batch() { |
178 | 199 | $this->assertInstanceOf( 'stdClass', $second_batch ); |
179 | 200 | $this->assertNotEquals( $first_batch, $second_batch, '2nd batch returned as 1st deleted' ); |
180 | 201 | $this->assertEquals( array( 'more wibble' ), $second_batch->data ); |
| 202 | + |
| 203 | + // Tests using a custom class for the $item. |
| 204 | + $this->wpbp->delete( $second_batch->key ); |
| 205 | + $batch_data_object = new Test_Batch_Data(); |
| 206 | + $this->wpbp->push_to_queue( $batch_data_object ); |
| 207 | + $this->assertNotEmpty( $this->getWPBPProperty( 'data' ) ); |
| 208 | + $this->assertEquals( array( $batch_data_object ), $this->getWPBPProperty( 'data' ) ); |
| 209 | + $this->wpbp->save(); |
| 210 | + $third_batch = $this->executeWPBPMethod( 'get_batch' ); |
| 211 | + $this->assertCount( 1, $third_batch->data ); |
| 212 | + $this->assertInstanceOf( Test_Batch_Data::class, $third_batch->data[0] ); |
| 213 | + |
| 214 | + // Explicitly set allowed classes to Test_Batch_Data. |
| 215 | + $this->setWPBPProperty( 'allowed_batch_data_classes', array( Test_Batch_Data::class ) ); |
| 216 | + $third_batch = $this->executeWPBPMethod( 'get_batch' ); |
| 217 | + $this->assertCount( 1, $third_batch->data ); |
| 218 | + $this->assertInstanceOf( Test_Batch_Data::class, $third_batch->data[0] ); |
| 219 | + |
| 220 | + // Allow a different class. |
| 221 | + $this->setWPBPProperty( 'allowed_batch_data_classes', array( stdClass::class ) ); |
| 222 | + $third_batch = $this->executeWPBPMethod( 'get_batch' ); |
| 223 | + $this->assertCount( 1, $third_batch->data ); |
| 224 | + $this->assertInstanceOf( __PHP_Incomplete_Class::class, $third_batch->data[0] ); |
| 225 | + |
| 226 | + // Disallow all classes. |
| 227 | + $this->setWPBPProperty( 'allowed_batch_data_classes', false ); |
| 228 | + $third_batch = $this->executeWPBPMethod( 'get_batch' ); |
| 229 | + $this->assertCount( 1, $third_batch->data ); |
| 230 | + $this->assertInstanceOf( __PHP_Incomplete_Class::class, $third_batch->data[0] ); |
| 231 | + |
| 232 | + // Allow everything. |
| 233 | + $this->setWPBPProperty( 'allowed_batch_data_classes', true ); |
| 234 | + $third_batch = $this->executeWPBPMethod( 'get_batch' ); |
| 235 | + $this->assertCount( 1, $third_batch->data ); |
| 236 | + $this->assertInstanceOf( Test_Batch_Data::class, $third_batch->data[0] ); |
181 | 237 | } |
182 | 238 |
|
183 | 239 | /** |
|
0 commit comments