typo3 congres 2012 - test-driven development binnen typo3 flow en neos
DESCRIPTION
Test-Driven Development is een kernwaarde van TYPO3 Flow en Neos development - zie hoe, en waarom, we dit doen. De complexiteit van frameworks zoals TYPO3 Flow en applicaties als TYPO3 Neos is een goede test coverage essentieel, of de kwaliteit en ontwikkeling zal langzaam stil komen te liggen. Deze sessie laat zien hoe we unit en functionele tests maken voor PHP en JavaScript code. En aangezien het maken van tests zonder het uitvoeren niet veel helpt zal ook Continues integrations (CI) zijdelings genoemd worden. Rens Admiraal Rens Admiraal is een freelance webdeveloper / coach en Senior Software Architect bij Beech Applications uit Venlo. Rond zijn 14e is Rens begonnen met 'programmeren', toen nog door het analizeren van code snippets op prehistorische computers. Door telkens opnieuw nieuwe uitdagingen aan te gaan heeft Rens meerdere talen geleerd, en heeft hij meegewerkt aan een breed scala aan web projecten. Nu is Rens TYPO3 Flow / TYPO3 Neos core lid en werkt hier vooral aan de Javascript kant van TYPO3 Neos. Bij verschillende bedrijven heeft Rens gezien hoe ontwikkeling in verschillende mates van structuur gedaan kan worden. Langzaamaan heeft dit geleid tot een grote interesse voor ontwikkel structuur, continues integration, automatisch testen en code kwaliteit. Nu helpt Rens bij het opzetten van ontwikkelomgevingen en het trainen van ontwikkelaars om in zo'n omgeving te werken. Rens woont in Ede met zijn vrouw en 3 zoons, speelt basgitaar en houdt ervan om af en toe wat druk van de ketel te halen op een (beach)volleybalveld.TRANSCRIPT
Test Driven Developmentmet Flow en Neos
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Rens Admiraal
Flow / Neos core developer
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
git clone --recursive git://github.com/radmiraal/Flow-and-Neos-Testing.git cd Flow-and-Neos-Testingcurl -s https://getcomposer.org/installer | phpphp composer.phar install --dev
Demo package
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Bekendheid met testen
100% coverage en CI!
in control, goede coverage
starten goed, houden niet bij
te weinig, helaas...
... tests?
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Crap in === crap out
Ontwerp
Developers
Klant
Tools
...
Eindproduct
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Team Profiel - De enthousiaste developer
Yeah, nieuw project!
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Team Profiel - De enthousiaste developer
Lekker vaartje!
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Team Profiel - De enthousiaste developer
Eeeh, waar ben ik?
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Team Profiel - De enthousiaste developer
Ooeeeeeei!
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Team Profiel - De enthousiaste developer
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Team Profiel - De bouwer
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Team Profiel - De bouwer
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Team Profiel - De bouwer
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Team Profiel - De bouwer
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Klant - De vrek
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Klant - De vrek
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Test Driven Development?
Schrijf eerst test === denk eerst
Snelste weg naar slagende test
Refactor later
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
“Cease dependence on mass inspection to achieve quality.
Improve the process and build quality into the product in
the first place.”
Test Driven Development Quotes
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
PHP Code Testen
Flow testing framework gebruikt PHPUnit
BaseTest class biedt helpers
Configuratie standaard aanwezig
Unit & Functionele tests
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Tests Uitvoeren
Unit tests:phpunit -c Build/buildessentials/PhpUnit/UnitTests.xml
Functionele tests:phpunit -c Build/buildessentials/PhpUnit/FunctionalTests.xml
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Unit / Functional tests
Een unit test...
test een class buiten zijn contexttest kleine ‘units’ code
Een functionele test...
test de class in zijn contexttests dekken het volledige systeem
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
UnitTestCase Helpers
Mock objecten
getAccessibleMock()getAccessibleMockForAbstractClass()
Dependency Injection
inject()Protected / private
_set()_call()
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Dependency Injection
Gebruik injection method indien mogelijk $mockFoo->injectBar($mockBar);
inject() $this->inject($mockFoo, ‘bar’, $mockBar);
AccessibleMock $mockFoo->_set(‘bar’, $mockBar);
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
FunctionalTestCase
$testableSecurityEnabled
$testableHttpEnabled
$router
$testablePersistenceEnabled
$browser
$objectManager
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
/** * @test */public function aBlogCanBeCreatedAndPersisted() { $blog = new \TYPO3\T3CON\Test\Domain\Model\Blog(); $blog->setTitle('foo'); $this->blogRepository->add($blog);
$this->persistenceManager->persistAll(); $this->persistenceManager->clearState();
$this->assertEquals(1, $this->blogRepository->countAll());}
Persistence Test
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
/** * @test */public function getControllerObjectNameIsEmptyIfNoRouteMatchesCurrentRequest() { $this->router = new \TYPO3\Flow\Mvc\Routing\Router(); $request = \TYPO3\Flow\Http\Request::create(
new \TYPO3\Flow\Http\Uri('http://localhost') );
$actionRequest = $this->router->route($request); $this->assertEquals('', $actionRequest->getControllerObjectName());}
HTTP Test
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
/** * @test */public function administratorsAreAllowedToSeeHiddenRestrictableEntities() { $this->authenticateRoles(array('Administrator'));
$defaultEntity = new Fixtures\RestrictableEntity('default'); $hiddenEntity = new Fixtures\RestrictableEntity('hiddenEntity'); $hiddenEntity->setHidden(TRUE);
// ...
$result = $this->restrictableEntityRepository->findAll(); $this->assertTrue(count($result) === 2);
$this->assertNotNull($this->persistenceManager->getObjectByIdentifier($defaultEntityIdentifier, 'TYPO3\T3CON\Test\Tests\Functional\Security\Fixtures\RestrictableEntity'));
$this->assertNotNull($this->persistenceManager->getObjectByIdentifier($hiddenEntityIdentifier, 'TYPO3\T3CON\Test\Tests\Functional\Security\Fixtures\RestrictableEntity'));
$this->restrictableEntityRepository->removeAll(); $this->persistenceManager->persistAll(); $this->persistenceManager->clearState();}
Security Test
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
/** * @test */public function formIsRedisplayedIfValidationErrorsOccur() { $this->browser->request('http://localhost/test/fluid/formobjects’); $form = $this->browser->getForm();
$form['post']['email']->setValue('test_noValidEmail');
$this->browser->submit($form); $form = $this->browser->getForm(); $this->assertSame('test_noValidEmail', $form['post']['email']->getValue()); $this->assertSame('f3-form-error', $this->browser->getCrawler()
->filterXPath('//*[@id="email"]')->attr('class'));
$form['post']['email']->setValue('[email protected]');
$response = $this->browser->submit($form); $this->assertSame('Egon Olsen|[email protected]', $response->getContent());}
“Browser” Test
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
TODO
Test runner demo movie
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
“During writing my tests, I recognize most of the
flaws in my initial design.”
Test Driven Development Quotes
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
JavaScript Testen
JSTestDriver
JasmineQUnit
YUI Test
JsUnit...
Behat
...
...
...
...
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Vereisten
Compatible met Flow folder structuur
Compatible met requirejs
Headless testrunner
XML rapporten
‘Multi browser’ tests
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Buster.js
Browser attaching
Resource publishing
AMD Support
Meerdere ‘reporters’
Headless testing (Phantomjs)
Nog beta, wel serieuze activiteit
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
var requireConfiguration = requireConfiguration || {};requireConfiguration['TYPO3.T3CON.Test'] = { baseUrl: buster.env.contextPath + '/Web/_Resources/Static/Packages/', paths: { 'emberjs-lib': 'TYPO3.T3CON.Test/Library/emberjs/ember-0.9.7.min', 'jquery': 'TYPO3.T3CON.Test/JavaScript/jquery', 'emberjs': 'TYPO3.T3CON.Test/JavaScript/emberjs', 'queue': 'TYPO3.T3CON.Test/JavaScript/queue' }, shim: { 'emberjs-lib': ['jquery'], 'queue': ['jquery'] }};
Requirejs
@see Tests/JavaScript/Unit/testcase.js
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Test Syntax
Testcases
assertionsexpectations
BDD
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Successfully connected PhantomjsPhantomJS 1.7.0, OS X: ........ Opera 12.02, OS X: ........ Safari 6.0.1, OS X: ........ Firefox 15.0.1, OS X: ........ 12 test cases, 32 tests, 140 assertions, 0 failures, 0 errors, 0 timeoutsFinished in 0.047s
Meerdere browsers tegelijkertijd testen
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
TODO
Test runner demo movie
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
“Mijn bug reproduceren met een test? Kan dat? Maar... dan
kan ik dus regressions voorkomen!”
Test Driven Development Quotes
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Continues Integration
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
Tests uitvoeren met Jenkins
Gerrit trigger op patches
Unit tests (evt. functioneel)
Resultaat post op IRC
zondag 4 november 12
Inspiring people tosharehttp://tinyurl.com/9j4uhj3
presentationShouldBePrepared()
presentationShouldBeGiven()
questionsShouldBeAnswered()
zondag 4 november 12