The episode you're thinking of is called ["The Doctor's Wife"](https://www.imdb.com/title/tt1721226/). In this episode the TARDIS' energy is placed into a body of a female named Idris by a sentient planetoid (named House) that wants to leave the pocket universe it lives in. In the past it has lured other Timelords to itself and consumed them. Using a Timelord distress signal it lures our Doctor to itself. When the Doctor shows up "House" takes over the TARDIS and attempts to leave its pocket universe. While doing so it amuses itself by torturing Amy and Rory. Making them see things by manipulating time and space around them (the parts to which your question was referring). It is to the best of my knowledge the first time the Doctor has been able to directly interact with the TARDIS and literally has a heart wrenching ending. This episode is one of my favorites.