The canonical way to construct a User instance representing an anonymous user is currently to directly call the constructor (and optionally calling setName() to set the IP address). To allow the construction of such User instances to be mocked for testing, UserFactory needs to get a method for constructing them.
Quick draft:
public function newAnonymous( $ip = null ) : User { $user = new User(); if ( $ip ) { $user->setName( $ip ); } return $user; }