PHP Classes

File: tests/NumericBitsTest.php

Recommend this page to a friend!
  Packages of Reinder Reinders   PHP Binary Flags   tests/NumericBitsTest.php   Download  
File: tests/NumericBitsTest.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Binary Flags
Manage a group of boolean flags using integers
Author: By
Last change: Binary Flags v3 (#16)

# Release Notes - v3.0.0

## Changed
- Numeric `BinaryFlags` APIs now accept `int` values only.
- Passing `float` values to numeric mask/flag methods now raises `TypeError` for `strict_types=1` callers.
- Non-strict callers should still cast or validate external values before calling the API because PHP scalar coercion can convert `float` to `int` at the call boundary.
- The numeric iterator and JSON serialization contracts are now documented as `int`-based.

## Removed
- `Bits::BIT_64`.
- The v2.x float-normalization/deprecation path in `Traits\InteractsWithNumericFlags`.

## BIT_64 Notice
`Bits::BIT_64` was removed because PHP numbers for bitwise flags are signed. The 64th bit is the sign bit, so it cannot be used reliably as a normal flag.

Using integer-compatible bits avoids these issues.
Date: 3 days ago
Size: 3,099 bytes
 

Contents

Class file image Download
<?php

declare(strict_types=1);

use
Reinder83\BinaryFlags\Tests\Stubs\ExampleFlags;
use
Reinder83\BinaryFlags\Tests\Stubs\ExampleFlagsWithNames;

beforeEach(function (): void {
   
$this->mask = ExampleFlags::FOO | ExampleFlags::BAR;
   
$this->callback = function (ExampleFlags $flags): void {
       
$this->mask = $flags->getMask();
    };
   
$this->test = new ExampleFlags($this->mask, $this->callback);
});

test('base mask', function (): void {
   
expect($this->test->checkFlag(ExampleFlags::FOO))->toBeTrue()
        ->
and($this->test->checkFlag(ExampleFlags::BAR))->toBeTrue()
        ->
and($this->test->checkFlag(ExampleFlags::BAZ))->toBeFalse()
        ->
and($this->test->checkFlag(ExampleFlags::QUX))->toBeFalse()
        ->
and($this->test->getMask())->toEqual(0x3);
});

test('multiple flags', function (): void {
   
expect($this->test->checkFlag(ExampleFlags::FOO | ExampleFlags::BAR))->toBeTrue()
        ->
and($this->test->checkFlag(ExampleFlags::BAR | ExampleFlags::BAZ))->toBeFalse()
        ->
and($this->test->checkFlag(ExampleFlags::BAR | ExampleFlags::BAZ, false))->toBeTrue()
        ->
and($this->test->checkFlag(ExampleFlags::BAZ | ExampleFlags::QUX, false))->toBeFalse()
        ->
and($this->test->checkAnyFlag(ExampleFlags::BAR | ExampleFlags::BAZ))->toBeTrue()
        ->
and($this->test->checkAnyFlag(ExampleFlags::BAZ | ExampleFlags::QUX))->toBeFalse();
});

test('callback', function (): void {
   
$this->test->addFlag(ExampleFlags::BAZ);

   
expect($this->mask)->toEqual(0x7);
});

test('add flag', function (): void {
   
$this->test->addFlag(ExampleFlags::BAZ);
   
$this->test->addFlag(ExampleFlags::FOO);

   
expect($this->test->checkFlag(ExampleFlags::FOO))->toBeTrue()
        ->
and($this->test->checkFlag(ExampleFlags::BAR))->toBeTrue()
        ->
and($this->test->checkFlag(ExampleFlags::BAZ))->toBeTrue()
        ->
and($this->test->checkFlag(ExampleFlags::QUX))->toBeFalse();
});

test('remove flag', function (): void {
   
$this->test->removeFlag(ExampleFlags::BAR);
   
$this->test->removeFlag(ExampleFlags::BAZ);

   
expect($this->test->checkFlag(ExampleFlags::FOO))->toBeTrue()
        ->
and($this->test->checkFlag(ExampleFlags::BAR))->toBeFalse()
        ->
and($this->test->checkFlag(ExampleFlags::BAZ))->toBeFalse()
        ->
and($this->test->checkFlag(ExampleFlags::QUX))->toBeFalse();
});

test('get all flags mask', function (): void {
   
expect(ExampleFlags::getAllFlagsMask())->toEqual(1 + 2 + 4 + 8);
});

test('countable', function (): void {
   
expect($this->test->count())->toEqual(2);
});

test('iterable', function (): void {
   
$test = new ExampleFlagsWithNames(ExampleFlagsWithNames::FOO | ExampleFlagsWithNames::BAZ);
   
$expectedFlags = $test->getFlagNames(ExampleFlagsWithNames::FOO | ExampleFlagsWithNames::BAZ, true);

   
$result = [];
    foreach (
$test as $flag => $description) {
       
$result[$flag] = $description;
    }

   
expect($result)->toEqual($expectedFlags);
});

test('json serializable', function (): void {
   
expect(json_encode($this->test))->toEqual(sprintf('{"mask":%d}', $this->test->getMask()));
});