At the moment, a Bookmark can be stored in a database and can be (re)created by retrieving the database row that matches the bookmark’s ID. But what happens—as is usually the case—when the ID is not known or you want to search the database for a more pertinent value, such as a partial name or a URL. A common solution is to add “finder” methods. For example, you might want a findByUrl() method to find Bookmarks similar to the parameter passed to the method. Here’s that intention expressed as a test: class ActiveRecordTestCase extends UnitTestCase { The test creates some data, searches for rows that contain “php” somewhere in the URL, and then verifies characteristics of the returned array of Bookmark objects. FindByUrl() is a static method, because you want Bookmark objects, but do not yet have an instance of the Bookmark class to work with. (Alternatively, you could move these “finder” methods to an object of their own, but for now the finder methods are a part of the Active Record Bookmark class.) Here’s some code to realize the requirements expressed by the test: class Bookmark {
blog comments powered by Disqus |