Валерий Рабиевский, веб-студия stfalcon.com
./console generate:bundle
Bundle namespace: Acme/Bundle/MyBundle
Bundle name [AcmeMyBundle]:
Target directory [/path/to/project/src]:
Configuration format (yml, xml, php) [annotation]:
Confirm automatic update of your Kernel [yes]?:
Confirm automatic update of the Routing [yes]?:
src/
Bundle/
Acme/
Command/
MyCommand.php
use Symfony\Component\Console\Command\Command;
class MyCommand extends Command {
protected function configure() {
$this
->setName('my:test')
->setDescription('My command');}}
protected function execute(
InputInterface $input,
OutputInterface $output)
{
// the business logic goes here...
}
class NewsletterManager
{
protected $mailer;
public function setMailer(Mailer $mailer)
{
$this->mailer = $mailer;
}
/* ... */}
# src/Acme/HelloBundle/Resources/config/services.yml
services:
my_mailer:
# ...
newsletter_manager:
class: NewsletterManager
calls:
- [ setMailer, [ @my_mailer ] ]
Где то в котроллере:
$this->container->get('newsletter_manager ');
namespace Acme\StoreBundle\Entity;
class Product
{
protected $name;
protected $price;
protected $description;
}
/**
* @ORM\Entity
* @ORM\Table(name="product")
*/
class Product
{ /* ... */ }
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\PrePersist
*/
public function setCreatedValue()
{
$this->created = new \DateTime();
}
/**
* @ORM\HasLifecycleCallbacks()
*/
class Product
{
// ...
}
class HelloControllerTest extends WebTestCase {
public function testIndex() {
$client = $this->createClient();
$crawler = $client->request('GET', '/hello/Fabien');
$this->assertTrue(
$crawler->filter('html:contains("Hello Fabien"))
->count()
);
Клиент делает запрос к Symfony2 приложению
$client = $this->createClient();
$client->request('GET', '/hello/Fabien');
Crawler парсит ответ
$crawler->filter('html:contains("Hello Fabien"))
и используется для навигации по странице
$link = $crawler->selectLink("Read more");
$client->click($link);
И, наконец-то, PHPUnit проверяет наш ответ
$this->assertEquals(10,
$crawler->filter('.test')->count()
);
$this->assertTrue(
$client->getResponse()->isSuccessful()
);
$input = new ArrayInput(array('name' => 'Hugo'));
$input->setInteractive(false);
$output = new StreamOutput();
$command = new SayHelloCommand();
$command->run($input, $output);
$this->assertEquals('Your name is
Hugo ',$output->getStream()
);
Валерий Рабиевский, веб-студия stfalcon.com