|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Unit tests covering AtomParser functionality. |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + * @subpackage AtomLib |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * Test Atom Syndication Format. |
| 11 | + * |
| 12 | + * @requires extension xml |
| 13 | + * |
| 14 | + * @covers AtomParser::parse |
| 15 | + */ |
| 16 | +final class AtomParser_Parse_Test extends WP_UnitTestCase { |
| 17 | + |
| 18 | + /** |
| 19 | + * Ensure the class being tested is loaded. |
| 20 | + */ |
| 21 | + public function set_up() { |
| 22 | + require_once dirname( __DIR__, 4 ) . '/src/wp-includes/atomlib.php'; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Test that the `AtomParser::parse()` method correctly sets callback functions to handle certain parts of the XML. |
| 27 | + * |
| 28 | + * Safeguards handling of the PHP 8.4 deprecation of `xml_set_object()`. |
| 29 | + */ |
| 30 | + public function test_parse_sets_handlers() { |
| 31 | + $atom = new class() extends AtomParser { |
| 32 | + public $start_element_call_counter = 0; |
| 33 | + public $end_element_call_counter = 0; |
| 34 | + public $start_ns_call_counter = 0; |
| 35 | + public $end_ns_call_counter = 0; |
| 36 | + public $cdata_call_counter = 0; |
| 37 | + public $default_call_counter = 0; |
| 38 | + |
| 39 | + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase -- Overloading property of upstream class. |
| 40 | + public $FILE = __DIR__ . '/../../data/feed/AtomParser_Parse_Test.xml'; |
| 41 | + |
| 42 | + public function start_element( $parser, $name, $attrs ) { |
| 43 | + ++$this->start_element_call_counter; |
| 44 | + } |
| 45 | + public function end_element( $parser, $name ) { |
| 46 | + ++$this->end_element_call_counter; |
| 47 | + } |
| 48 | + public function start_ns( $parser, $prefix, $uri ) { |
| 49 | + ++$this->start_ns_call_counter; |
| 50 | + } |
| 51 | + public function end_ns( $parser, $prefix ) { |
| 52 | + ++$this->end_ns_call_counter; |
| 53 | + } |
| 54 | + public function cdata( $parser, $data ) { |
| 55 | + ++$this->cdata_call_counter; |
| 56 | + } |
| 57 | + public function _default( $parser, $data ) { |
| 58 | + ++$this->default_call_counter; |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + $this->assertTrue( $atom->parse(), 'Parsing of XML file failed' ); |
| 63 | + |
| 64 | + // Ensure no errors were logged. |
| 65 | + $this->assertNull( $atom->error, 'Unexpected errors encountered' ); |
| 66 | + |
| 67 | + $msg = '%s() handler did not get called expected nr of times'; |
| 68 | + $this->assertSame( 28, $atom->start_element_call_counter, sprintf( $msg, 'start_element' ) ); |
| 69 | + $this->assertSame( 28, $atom->end_element_call_counter, sprintf( $msg, 'end_element' ) ); |
| 70 | + $this->assertSame( 2, $atom->start_ns_call_counter, sprintf( $msg, 'start_ns' ) ); |
| 71 | + $this->assertSame( 0, $atom->end_ns_call_counter, sprintf( $msg, 'end_ns' ) ); |
| 72 | + $this->assertSame( 57, $atom->cdata_call_counter, sprintf( $msg, 'cdata' ) ); |
| 73 | + $this->assertSame( 2, $atom->default_call_counter, sprintf( $msg, '_default' ) ); |
| 74 | + } |
| 75 | +} |
0 commit comments