Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Mockery & mocking basics

Mockery & mocking basics

Jan Škrášek

February 28, 2015
Tweet

Other Decks in Programming

Transcript

  1. Test types • Unit tests • Ingtegrations tests o Regression

    tests • Acceptance tests • … Mock objects
  2. Mock object Mock object is simulated object that mimic the

    behavior of real object in a controlled way.
  3. Unit test $driver = Mockery::mock(IDriver::class); $parser = new SqlProcessor($driver); $driver->shouldReceive('delimite')

    ->with('a.b')->andReturn('`a`.`b`'); Assert::same( 'SELECT `a`.`b`', $parser->process('SELECT [a.b]') );
  4. $this->mapper->shouldReceive('getRepository')->once()->andReturn($this->mapper); $this->mapper->shouldReceive('getEntityClassNames')->once()->andReturn(['EntityClass']); $this->metadataStorage->shouldReceive('get')->once()->with('EntityClass')->andReturn($this->entityM $this->queryBuilder->shouldReceive('getFromAlias')->once()->andReturn('books'); $this->mapper->shouldReceive('getStorageReflection')->once()->andReturn($this->reflection); $propertyMetadata = Mockery::mock('Nextras\Orm\Entity\Reflection\PropertyMetadata'); $propertyMetadata->relationshipRepository =

    'AuthorsRepository'; $propertyMetadata->relationshipType = PropertyMetadata::RELATIONSHIP_MANY_HAS_ONE; // translator $this->entityMetadata->shouldReceive('getProperty')->once()->with('translator')->andReturn($property $this->model->shouldReceive('getRepository')->once()->with('AuthorsRepository')->andReturn($this-> $this->model->shouldReceive('getMapper')->once()->andReturn($this->mapper); $this->mapper->shouldReceive('getStorageReflection')->once()->andReturn($this->reflection); $this->reflection->shouldReceive('getStoragePrimaryKey')->once()->andReturn(['id']); $this->reflection->shouldReceive('convertEntityToStorageKey')->once()->with('translator')->andReturn( $this->mapper->shouldReceive('getTableName')->once()->andReturn('authors'); $this->mapper->shouldReceive('getRepository')->once()->andReturn($this->mapper); $this->mapper->shouldReceive('getEntityClassNames')->once()->andReturn(['EntityClass2']); $this->metadataStorage->shouldReceive('get')->once()->with('EntityClass2')->andReturn($this->entityM // name $this->entityMetadata->shouldReceive('getProperty')->once()->with('name'); $this->reflection->shouldReceive('convertEntityToStorageKey')->once()->with('name')->andReturn('nam $this->queryBuilder->shouldReceive('leftJoin')->once()->with('books', 'authors', 'authors', '[books.transl $this->queryBuilder->shouldReceive('addOrderBy')->once()->with('authors.name'); $this->builderHelper->processOrderByExpression('this->translator->name', ICollection::ASC, $this->qu
  5. Unit tests • To much work to mock? o Just

    SPLIT the class. • Should we mock?: o scalars/arrays/objects o entities/crates o model layer • Integration tests
  6. Integration tests • Are quite difficult • You need: o

    Setup database/queue o Prepare testing data (insert into database) o Working internet connection o … • Solution: PARTIAL MOCKS
  7. Partial mocks • Mock only something • Mock only some

    methods $mock = Mockery::mock(MysqlDriver::class) ->makePartial(); $mock->shouldReceive(…);
  8. Recap • Use composition!!! • Use interfaces. • Create small

    classes. • Choose your library carefully. • Write unit/integration tests.