You're right, it did happen. King Sebastiano of Portugal led a disastrous expedition to Morocco in 1578, which was crushed by the Moroccans at Alcazar. The King, a religious mystic who never married, was killed along with much of his nobility. King Sebatiano's uncle Henry inherited the throne, but Henry was a Cardinal in the Catholic Church; he also had no descendents, and upon his death Sebastiano's uncle, King Philip of Spain, was able to take the throne. There was scant resistance, and the Spanish Army, led for the last time by the famed Duke of Alba, marched into Madrid.
Philip and his son ruled fairly, but the link to Spain proved a disaster for Portugal. The damage was caused by Spain's incessant war with the Protestant sea powers, Holland and England. The two nations savaged Portugal's overseas empire and trade, ruining the Portuguese economy. When Philip's grandson tried to revoke the liberties of the old Portuguese kingdom and impose high taxes, the Portuguese rebelled in 1640 and threw the Spanish out.
There's not much info I can find, but here's a link, from my old standby the Catholic Encyclopedia:
http://www.newadvent.org/cathen/12297a.htm