Giter VIP home page Giter VIP logo

dto-tester's Introduction

skrepr

Data Transfer Object - Tester

This is a simple library to make unit tests for DTO's and Entity objects easier. By using this DtoTestCase it will automatically test all public properties and get/set combinations.

Of course this is not just to satisfy the "coverage", but it is to test the basics and makes sure that your DTO's are always behave the same as you expect.

Note that you always must create your own extra tests for specific tasks and any business logic.

Installation

You can install the package using the Composer package manager. It is recommended that you install this only as a development package. You can install it by running this command in your project root:

composer require --dev skrepr/dto-tester

Usage

The abstract class DtoTestCase has 3 required methods for you to implement:

  • getInstance
  • getTestValuesForProperty
  • getTestValuesForMethod

And at this moment one optional setting: - $markEmptyAsSkipped

The getInstance method should return a testable object. This object is only requested twice during all the tests. First for the properties and the second time when the methods are tested.

Next the getTestValuesForProperty and getTestValuesForMethod methods should return an array with possible values that should be tested.

The getTestValuesForProperty method gets the property name and type, here you can check and give values you want to test. You can also give a null value instead of an array, then the tests for that property are skipped (unless it is a nullable-property). The nullable variant will always be tested, it is not possible to use null as a value within the array.

The getTestValuesForMethod method works in the same way, it only gives back the method name extra.

For union types, the property/parameter will be tested for every type in the union.

When you don't have special needs, you can use "match" like this:

    protected function getTestValuesForMethod(string $methodName, string $parameterName, string $parameterType): ?array
    {
        return match ($parameterType) {
            'int' => [1, 42, 1337],
            'string' => ['test', 'skrper'],
            'bool' => [true, false],
            SomeClass::class => [new SomeClass(), $this->createMock(SomeClass::class)]
        };
    }

It is not recommended to use a "default" when using a match like this. In fact, you will get a nice readable exception, so you know what you missed.

Known issues

  • Readonly properties are not supported
  • Not yet possible to test for other result value from getter

dto-tester's People

Contributors

erkens avatar

Stargazers

Evert Jan Hakvoort avatar  avatar Jeroen van der Meulen avatar

Watchers

 avatar

dto-tester's Issues

Object with only one get-method will get a risky test

When having just one get method in the data object (next to public properties) with no setter, will result in a message of a risky test.
To fix this, it should be skipped when there is no set method for the get method.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.