AthenaeumAthenaeum
Packages
  • next
  • current
  • v9.x
  • v8.x
  • v7.x
  • v6.x
  • v5.x
  • v4.x
  • v3.x
  • v2.x
  • v1.x
Changelog
GitHub
Packages
  • next
  • current
  • v9.x
  • v8.x
  • v7.x
  • v6.x
  • v5.x
  • v4.x
  • v3.x
  • v2.x
  • v1.x
Changelog
GitHub
  • Version 3.x

    • How to install
  • Config

    • Configuration Loader
  • Container

    • IoC Service Container
  • Dto

    • Data Transfer Object (DTO)
    • Create Interface
    • Implement DTO
    • Property overloading
    • Populate
    • Json
    • Nested DTOs
    • Array DTO
  • Http

    • Introduction
    • Http Clients
  • Properties

    • Properties Overload
  • Support

    • Support Introduction
    • Laravel Aware Of Helpers
    • Aware Of Properties
    • Aware-Of Component Generator
  • Testing

    • Testing Introduction
    • Laravel
    • Test Cases
    • Traits
  • Utils

    • Populate
    • Json
You are viewing documentation for an outdated version. It is no longer supported!

Traits

If you are working with "getter-setter-traits" (aware-of components), then you can easily test those using the GetterSetterTraitTester.

Getter-Setter Trait

The trait in question must have the following methods defined:

set[property-name](?[type] $property);
get[property-name](): ?[type] ;
has[property-name](): bool ;
getDefault[property-name](): ?[type] ;

Trait Example

class NameTrait
{
    protected $name = null;

    public function setName(?string $name)
    {
        $this->name = $name;

        return $this;
    }

    public function getName(): ?string
    {
        if (!$this->hasName()) {
            $this->setName($this->getDefaultName());
        }
        return $this->name;
    }

    public function hasName(): bool
    {
        return isset($this->name);
    }

    public function getDefaultName(): ?string
    {
        return null;
    }    
}

Testing the Trait

To test the trait, use the assertGetterSetterTraitMethods method inside your test.

use Aedart\Testing\TestCases\UnitTestCase;
use Aedart\Testing\GetterSetterTraitTester;

class NameTraitTest extends UnitTestCase
{
    use GetterSetterTraitTester;

    /**
     * @test
     */
    public function canAssertTraitMethods()
    {
        $this->assertGetterSetterTraitMethods(
            NameTrait::class,
            $this->faker->name,
            $this->faker->name
        );
    }
}

Auto Generate Argument Data

As an alternative, you can allow the tester to automatically detect and generate argument data, based on the argument's type. To do so, use the assertTraitMethods method.

use Aedart\Testing\TestCases\UnitTestCase;
use Aedart\Testing\GetterSetterTraitTester;

class NameTraitTest extends UnitTestCase
{
    use GetterSetterTraitTester;

    /**
     * @test
     */
    public function canAssertTraitMethods()
    {
        $this->assertTraitMethods(NameTrait::class);
    }
}

Warning

Method is only able to generate data for scalar-types and Mocks for objects. This feature should be considered experimental!

Edit page
Last Updated: 20/02/2020, 21:40
Contributors: Alin Eugen Deac
Prev
Test Cases