Download - TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Transcript
Page 1: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Test Driven Developmentmet Flow en Neos

zondag 4 november 12

Page 2: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

Rens Admiraal

Flow / Neos core developer

@[email protected]

zondag 4 november 12

Page 3: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

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

Page 4: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

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

Page 5: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

Crap in === crap out

Ontwerp

Developers

Klant

Tools

...

Eindproduct

zondag 4 november 12

Page 6: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

Team Profiel - De enthousiaste developer

Yeah, nieuw project!

zondag 4 november 12

Page 7: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

Team Profiel - De enthousiaste developer

Lekker vaartje!

zondag 4 november 12

Page 8: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

Team Profiel - De enthousiaste developer

Eeeh, waar ben ik?

zondag 4 november 12

Page 9: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

Team Profiel - De enthousiaste developer

Ooeeeeeei!

zondag 4 november 12

Page 10: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

Team Profiel - De enthousiaste developer

zondag 4 november 12

Page 11: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

Team Profiel - De bouwer

zondag 4 november 12

Page 12: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

Team Profiel - De bouwer

zondag 4 november 12

Page 13: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

Team Profiel - De bouwer

zondag 4 november 12

Page 14: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

Team Profiel - De bouwer

zondag 4 november 12

Page 15: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

Klant - De vrek

zondag 4 november 12

Page 16: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

Klant - De vrek

zondag 4 november 12

Page 17: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

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

Page 18: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

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

Page 19: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

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

Page 20: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

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

Page 21: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

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

Page 22: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

UnitTestCase Helpers

Mock objecten

getAccessibleMock()getAccessibleMockForAbstractClass()

Dependency Injection

inject()Protected / private

_set()_call()

zondag 4 november 12

Page 23: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

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

Page 24: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

FunctionalTestCase

$testableSecurityEnabled

$testableHttpEnabled

$router

$testablePersistenceEnabled

$browser

$objectManager

zondag 4 november 12

Page 25: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

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

Page 26: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

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

Page 27: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

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

Page 28: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

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

Page 29: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

TODO

Test runner demo movie

zondag 4 november 12

Page 30: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

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

Page 31: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

JavaScript Testen

JSTestDriver

JasmineQUnit

YUI Test

JsUnit...

Behat

...

...

...

...

zondag 4 november 12

Page 32: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

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

Page 33: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

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

Page 34: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

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

Page 35: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

Test Syntax

Testcases

assertionsexpectations

BDD

zondag 4 november 12

Page 36: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

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

Page 37: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

TODO

Test runner demo movie

zondag 4 november 12

Page 38: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

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

Page 39: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

Continues Integration

zondag 4 november 12

Page 40: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

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

Page 41: TYPO3 Congres 2012 - Test-Driven Development binnen TYPO3 Flow en Neos

Inspiring people tosharehttp://tinyurl.com/9j4uhj3

presentationShouldBePrepared()

presentationShouldBeGiven()

questionsShouldBeAnswered()

zondag 4 november 12