*/ private array $shortcodes = []; protected function setUp(): void { parent::setUp(); $this->bookingPage = Mockery::mock(BookingPage::class); $this->loginPage = Mockery::mock(LoginPage::class); $this->registrationPage = Mockery::mock(RegistrationPage::class); $this->groupClassPage = Mockery::mock(GroupClassPage::class); $this->registrar = new ShortcodeRegistrar( $this->bookingPage, $this->loginPage, $this->registrationPage, $this->groupClassPage, ); $shortcodes = &$this->shortcodes; Functions\when('add_shortcode')->alias( static function (string $tag, callable $callback) use (&$shortcodes): void { $shortcodes[$tag] = $callback; } ); } public function testRegisterAddsAllFourShortcodesAndHooks(): void { Actions\expectAdded('template_redirect') ->once() ->with([$this->registrationPage, 'maybeRedirectToRegistrationPage']); Actions\expectAdded('wp_enqueue_scripts') ->once() ->with([$this->registrar, 'enqueueAssets']); $this->registrar->register(); self::assertSame( ['us_booking', 'us_student_login', 'us_student_register', 'us_group_classes'], array_keys($this->shortcodes) ); } /** * WordPress passes an empty string (not an array) to a shortcode callback * when the shortcode is used without attributes, e.g. `[us_booking]` — * the wrapper must normalize it before the typed render methods. */ public function testBareShortcodeUsageIsNormalizedToAnEmptyAttributeArray(): void { $this->registrar->register(); $this->bookingPage->shouldReceive('render')->once()->with([])->andReturn('booking'); $this->loginPage->shouldReceive('render')->once()->with([])->andReturn('login'); $this->registrationPage->shouldReceive('render')->once()->with([])->andReturn('register'); $this->groupClassPage->shouldReceive('render')->once()->with([])->andReturn('group'); self::assertSame('booking', $this->shortcodes['us_booking']('')); self::assertSame('login', $this->shortcodes['us_student_login']('')); self::assertSame('register', $this->shortcodes['us_student_register']('')); self::assertSame('group', $this->shortcodes['us_group_classes']('')); } public function testShortcodeAttributesArePassedThroughUnchanged(): void { $this->registrar->register(); $this->bookingPage->shouldReceive('render') ->once()->with(['login_page_id' => '5'])->andReturn('booking'); $this->loginPage->shouldReceive('render') ->once()->with(['booking_page_id' => '9'])->andReturn('login'); self::assertSame('booking', $this->shortcodes['us_booking'](['login_page_id' => '5'])); self::assertSame('login', $this->shortcodes['us_student_login'](['booking_page_id' => '9'])); } }