preview; } } class BlockRegistrarTest extends TestCase { private BookingPage&Mockery\MockInterface $bookingPage; private LoginPage&Mockery\MockInterface $loginPage; private RegistrationPage&Mockery\MockInterface $registrationPage; private GroupClassPage&Mockery\MockInterface $groupClassPage; private TestableBlockRegistrar $registrar; 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 TestableBlockRegistrar( $this->bookingPage, $this->loginPage, $this->registrationPage, $this->groupClassPage, ); } public function testRegisterHooksBlockRegistrationOntoInit(): void { Actions\expectAdded('init')->once()->with([$this->registrar, 'registerBlocks']); $this->registrar->register(); } public function testRegisterBlocksRegistersAllFourBlocksWithAssets(): void { Functions\expect('wp_register_script') ->once() ->with( BlockRegistrar::SCRIPT_HANDLE, Mockery::pattern('~assets/js/blocks\.js$~'), Mockery::type('array'), USC_VERSION, true ); Functions\when('wp_style_is')->justReturn(false); Functions\expect('wp_register_style') ->once() ->with( BlockRegistrar::STYLE_HANDLE, Mockery::pattern('~assets/css/frontend\.css$~'), [], USC_VERSION ); $registered = []; Functions\when('register_block_type')->alias( static function (string $name, array $args) use (&$registered): bool { $registered[$name] = $args; return true; } ); $this->registrar->registerBlocks(); self::assertSame( [ 'us-scheduler/booking', 'us-scheduler/student-login', 'us-scheduler/student-register', 'us-scheduler/group-classes', ], array_keys($registered) ); foreach ($registered as $args) { self::assertSame(BlockRegistrar::SCRIPT_HANDLE, $args['editor_script']); self::assertSame(BlockRegistrar::STYLE_HANDLE, $args['style']); self::assertIsCallable($args['render_callback']); } } public function testRegisterBlocksDoesNotReRegisterAnAlreadyRegisteredStyle(): void { Functions\when('wp_register_script')->justReturn(true); Functions\when('wp_style_is')->justReturn(true); Functions\expect('wp_register_style')->never(); Functions\when('register_block_type')->justReturn(true); $this->registrar->registerBlocks(); } public function testFrontEndRenderDelegatesToThePageObjects(): void { $this->registrar->preview = false; $this->bookingPage->shouldReceive('render')->once()->with([])->andReturn('booking-html'); $this->loginPage->shouldReceive('render')->once()->with([])->andReturn('login-html'); $this->registrationPage->shouldReceive('render')->once()->with([])->andReturn('register-html'); $this->groupClassPage->shouldReceive('render')->once()->with([])->andReturn('group-html'); self::assertSame('booking-html', $this->registrar->renderBooking()); self::assertSame('login-html', $this->registrar->renderLogin()); self::assertSame('register-html', $this->registrar->renderRegistration()); self::assertSame('group-html', $this->registrar->renderGroupClasses()); } public function testEditorPreviewRendersStaticMarkupWithoutTouchingThePages(): void { $this->registrar->preview = true; Functions\when('wp_nonce_field')->justReturn(''); $this->bookingPage->shouldNotReceive('render'); $this->loginPage->shouldNotReceive('render'); $this->registrationPage->shouldNotReceive('render'); $this->groupClassPage->shouldNotReceive('render'); self::assertStringContainsString('us-booking-app', $this->registrar->renderBooking()); self::assertStringContainsString('us-login-form', $this->registrar->renderLogin()); self::assertStringContainsString('us-register-form', $this->registrar->renderRegistration()); self::assertStringContainsString('us-group-app', $this->registrar->renderGroupClasses()); } public function testIsEditorPreviewIsFalseOutsideRestRequests(): void { // REST_REQUEST is undefined in the test process, so the real // registrar must take the front-end path. $registrar = new BlockRegistrar( $this->bookingPage, $this->loginPage, $this->registrationPage, $this->groupClassPage, ); $this->bookingPage->shouldReceive('render')->once()->with([])->andReturn('live'); self::assertSame('live', $registrar->renderBooking()); } }