- Recent
- Popular
- Tags (1)
- Subscribers (7)
- Source code of the cookbook application has been releasedNovember 20
-
Today, Andy Dawson (aka AD7six) announced in his blog that the source code of the cookbook application (which powers http://book.cakephp.org) is now available for everyone: http://thechaw.com/cakebook.
As you can see from the url, this project is hosted in a different place than other projects from the Cake team. It would be good if all such projects would be consolidated in one single place.
Anyway, here a list with places from where you can get the source code of those projects:
Update 2008-11-21: See also the official announcement of The Chaw.
- Life is short, so use it wiselyNovember 19
-
Sometimes you get reminded that life is quite short. Each day could be your last day. And each day could be the last day you are the person you are right now. That’s life, it’s something we can’t influence. It just happens.
But what we can influence is the way we live our life each day. Do you do what’s important to you? Or do you waste your life doing meaningless things? Or do you postpone, for whatever reason, important things you should do now?
Whatever your answers are: Life is short, use it wisely.
- Testing protected methodsNovember 13
-
Sometimes you are in the situation that you want/have to test a protected method. How do you do that? Well, it is quite simple. You have to create a subclass of the class you want to test and then you add a public method which delegates to the protected method. This subclass is then instantiated in the test and instead of calling the protected method, the newly added public method is used.
Let’s have a look at a very trivial code example. First the class we want to test:
class Example { protected function getHelloWorld() { return 'hello world'; } }And now the test for the getHelloWorld() method:
class ExampleTest extends CakeTestCase { private $example = null; public function setUp() { $this->example = new MyExample(); } public function testGetHelloWorld() { $this->assertEqual('hello world', $this->example->publicGetHelloWorld()); } } class MyExample extends Example { public function publicGetHelloWorld() { return $this->getHelloWorld(); } }You can use the same strategy if you have to modify protected properties for your tests.
Happy testing!
PS: If you are a Munich baker you might be interested in the first cakeBar, which takes place on November 20.
- An idea for hacking core helpersNovember 7
-
In a recent comment by John I got asked whether there is a way to override a method of a core helper like HtmlHelper::link() without affecting the variables in the views (i.e. you can still use $html->link() instead of $myHtml->link()). There are at least two approaches to accomplish this (without touching core files).
The first approach is to copy the HtmlHelper from the core (cake/libs/view/helpers/html.php) to the helpers folder of your application (app/views/helpers). And then you can modify this file as you like. Easy. The problem of this approach is the code duplication. If something in the core HtmlHelper is changed by the developers you have to reapply the changes in your own HtmlHelper…
The second approach is to replace the HtmlHelper with your own helper that extends the HtmlHelper. For this purpose you have to create a custom view class which overrides the _loadHelpers() method of the View. It’s probably best if I do a simple example to show the idea.
First we create our own helper:
// app/views/helpers/example.php App::import('Helper', 'Html'); class ExampleHelper extends HtmlHelper { public function link($title, $url = null, $htmlAttributes = array(), $confirmMessage = false, $escapeTitle = true) { return '<a href="http://example.com">example</a> - The Model::validates() trapNovember 5
-
One mistake I make from time to time is to try to validate my data in the following way:
public function example() { $data = array('Project' => array('name' => 'Testproject')); if ($this->Project->validates($data)) { echo 'valid'; } else { echo 'invalid'; } }If you look at this snippet, it probably looks fine to you. And it is fine if you are using CakePHP 1.1. Unfortunately, in CakePHP 1.2 the behavior of this method has changed, and so the example above doesn’t work as you would expect (depending on the validation rules). For a while the parameter of Model::validates() was deprecated (see Parameter for Model::validates() is now deprecated) and caused a warning if you used it. In the meantime the parameter is back with a new meaning…
Compare the API docs for the validates() methods. First the CakePHP 1.1 docs:
/** * Returns true if all fields pass validation, otherwise false. * * @param array $data POST data * @return boolean True if there are no errors * @access public */And now the docs for CakePHP 1.2:
/** * Returns true if all fields pass validation. * * @param string $options An optional array of custom opti
