PHP Classes

File: tests/Phabstractic/Patterns/Resource/SingletonTrait.php

Recommend this page to a friend!
  Classes of Asher Wolfstein   Singleton Trait   tests/Phabstractic/Patterns/Resource/SingletonTrait.php   Download  
File: tests/Phabstractic/Patterns/Resource/SingletonTrait.php
Role: Unit test script
Content type: text/plain
Description: unit testing
Class: Singleton Trait
Trait that adds singleton pattern to a class
Author: By
Last change:
Date: 8 years ago
Size: 1,381 bytes
 

Contents

Class file image Download
<?php
require_once('src/Phabstractic/Patterns/Resource/SingletonTrait.php');

use
PHPUnit\Framework\TestCase;
use
Phabstractic\Patterns\Resource as Patterns;

class
TestSingleton {
    use
Patterns\SingletonTrait;
   
    public
$configuration;
   
    protected function
init() {
       
$args = func_get_args();
       
$this->configuration = $args[0];
    }
}

class
SingletonTraitTest extends TestCase
{
    public function
testInstantiation() {
       
$this->assertFalse(TestSingleton::hardened());
       
       
$singleton = TestSingleton::instantiate('conf');
       
       
$this->assertInstanceOf(TestSingleton::class, $singleton);
       
$this->assertEquals($singleton->configuration, 'conf');
       
       
$this->assertTrue(TestSingleton::hardened());
    }
   
   
/**
     * @depends testInstantiation
     * @expectedException \Phabstractic\Patterns\Exception\RuntimeException
     *
     */
   
public function testDoubleInstantiation() {
       
$singleton = TestSingleton::instantiate('error');
    }
   
   
/**
     * @expectedException Error
     *
     */
   
public function testConstruction()
    {
       
$singleton = new TestSingleton();
    }
   
   
/**
     * @expectedException Error
     */
   
public function testClone()
    {
       
$singleton = TestSingleton::instantiate();
       
$singletonclone = clone $singleton;
    }
}