Goal: Focus on understanding the BDD process - how to break down a problem into smaller, more manageable chunks.
Each spec that you write should be describing input and output without any concern for how to do it. Remember to think of the interface before the implementation.
And then, once the problem is solved according to the requirements of your specs, it should be smoother when you actually build the interface in Silex.
Follow along with your weekend homework to create a Silex website which changes a sentence from the user into title case.
tests. Then create your
TitleCaseGenerator.phpclass declaration file inside of
srcand your test file
composer.jsonfile in the top level of your project folder as usual. You can start by just including PHPUnit for now. Run
composer install. Then while you're in the Terminal start your server in your
webfolder - now you've got that out of the way.
makeTitleCasemethod. Only work on one spec at a time.
TitleCaseGeneratorclass with its new method in a website. When you are finished, have a teacher look at your code before you continue to the next problem.
Let's do the Ping-Pong problem from your Epicodus applications again, but this time we will work through it using BDD. Then your goal is to use Silex and Twig to create a website where the user enters a number into a form on the first page. When they submit the form, they should be taken to a second page printing out all the numbers from 1 to the number they entered. Except: numbers divisible by 3 should be replaced by "ping", numbers divisible by 5 should be replaced by "pong" and numbers divisible by both should be replaced by "ping-pong".
PingPongGeneratorand it will only need one method called
generatePingPongArraymethod into a Twig template and then use a
foreachloop to display it as an unordered list.
Leetspeak uses an alternative alphabet of numbers and symbols to replace various letters in words.
Write a LeetspeakTranslator class with a method called
translate which converts a string into a simple version of Leetspeak using the following rules:
Here is a final sample input and output:
In: "Don't you love these 'String' exercises? I do!"
Out: "D0n't y0u l0ve th3z3 'String' 3x3rciz3z? 1 d0!"
There will be 2 arrays - an array of the letters from the original string and an array of letters after they have been checked and modified where appropriate.
Each letter in the input array of letters will need to be evaluated and then pushed to the output letter array.
Finally, the letters after being checked will need to be returned from the method as a joined string.
Here are your steps for this, and the following two problems:
In chess, a queen can move horizontally, vertically, and diagonally, making it the most powerful piece on the board. If another piece is within its line of sight along these three lines, then the queen can attack it.
Here is the end goal: We're going to make a website in Silex where the user can enter an X and a Y coordinate for the queen, and an X and a Y coordinate for another chess piece which the queen is attacking.
When the user submits the form, you should create an instance of a
Queen class, with properties for its current X and Y coordinates. This
Queen class should have a method called
canAttack, which takes an X and a Y coordinate as arguments.
The method should return
true if the queen can attack the given space and
false if it can't. A message should be displayed to show the result after the form for filling out the coordinates is submitted.
Remember to write your tests in plain english and get them checked first. Don't be tempted to write a single test for the
true case (e.g., it 'is true if it can attack horizontally or vertically or diagonally') - there are three separate behaviors here for horizontal, vertical, and diagonal. Also remember to try to refactor or simplify your code as soon as you get something working.
Time for something a little more complicated. Let's write a Silex site that tells us, given a certain time, the distance in degrees between the minute and hour hands on an analog clock. It should use a
Clock class with a method to take a time of day as input, and return the number of degrees for output. For example, 12 o'clock would return 0º and 6 o'clock would return 180º. Always return the smaller distance and be as precise as possible. As with the previous exercises, start with your tests and your class, then when you're sure your method works, build the rest of the website around it.