Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
midnite81 committed Jul 14, 2018
1 parent d93118f commit 9f0b1f3
Show file tree
Hide file tree
Showing 14 changed files with 722 additions and 9 deletions.
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -19,8 +19,8 @@
},
"autoload": {
"psr-4": {
"Midnite81\\Badges\\": "src",
"Midnite81\\Badges\\Tests\\": "tests"
"Midnite81\\Badges\\": "src/",
"Midnite81\\Badges\\Tests\\": "tests/"
}
}
}
70 changes: 70 additions & 0 deletions readme.md
@@ -0,0 +1,70 @@
# Badges [![Latest Stable Version](https://poser.pugx.org/midnite81/badges/version)](https://packagist.org/packages/midnite81/badges) [![Total Downloads](https://poser.pugx.org/midnite81/badges/downloads)](https://packagist.org/packages/midnite81/badges) [![Latest Unstable Version](https://poser.pugx.org/midnite81/badges/v/unstable)](https://packagist.org/packages/midnite81/badges) [![License](https://poser.pugx.org/midnite81/badges/license.svg)](https://packagist.org/packages/midnite81/badges) [![Build](https://travis-ci.org/midnite81/badges.svg?branch=master)](https://travis-ci.org/midnite81/badges) [![Coverage Status](https://coveralls.io/repos/github/midnite81/badges/badge.svg?branch=master)](https://coveralls.io/github/midnite81/badges?branch=master)
_A PHP package to render out package badges_

# Installation

This package requires PHP 5.6+

To install through composer include the package in your `composer.json`.

"midnite81/badges": "^1.0.0"

Run `composer install` or `composer update` to download the dependencies or you can
run `composer require midnite81/badges`.

## Badges Supported

| Type | Example |
|------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------|
|Latest Stable |[![Latest Stable Version](https://poser.pugx.org/midnite81/badges/version)](https://packagist.org/packages/midnite81/badges) |
|Total Downloads |[![Total Downloads](https://poser.pugx.org/midnite81/badges/downloads)](https://packagist.org/packages/midnite81/badges) |
|Latest Unstable |[![Latest Unstable Version](https://poser.pugx.org/midnite81/badges/v/unstable)](https://packagist.org/packages/midnite81/badges) |
|Licence |[![License](https://poser.pugx.org/midnite81/badges/license.svg)](https://packagist.org/packages/midnite81/badges) |
|Build |[![Build](https://travis-ci.org/midnite81/badges.svg?branch=master)](https://travis-ci.org/midnite81/badges) |
|Coverage |[![Coverage Status](https://coveralls.io/repos/github/midnite81/badges/badge.svg?branch=master)](https://coveralls.io/github/midnite81/badges?branch=master)|

Over time support for other badges will be added. You can create your own templates which extend
`Midnite81\Badges\Type\BadgeType` and can be passed through the `$badges->get(MyClass::class)` method. More
documentation on adding your own templates will be added later.

## Example Usage

Firstly, you need to create an instance of badges.

```php
use Midnite81\Badges\Badges;
$badges = new Badges($attributes);
// or
$badges = Badges::create($attributes);
```

You'll notice that a variable of $attributes is passed on construction. The attributes are what the package will use
to translate the template. By default we're only going to pass the following attributes as they are needed for the
supported badges. Obviously update the above attributes to suit your own needs.

```php
$attributes = ['$PACKAGE_NAME$' => 'midnite81/badges'];
```

Once the class is instantiated, you then need to select the type of badge you want.

```php
$myBadge = $this->latestStableVersion();
```

This will return you a `Writer` object, which you can call `->toHtml()` or `->toMarkdown` on for the final rendering
of the badge. The default `_toString` method will return the html version.

So all in all;

```
use Midnite81\Badges\Badges;
$badges = Badges::create(['$PACKAGE_NAME$' => 'midnite81/badges']);
echo $badges->latestStableVersion()->toMarkdown();
```
will output

[![Latest Stable Version](https://poser.pugx.org/midnite81/badges/version)](https://packagist.org/packages/midnite81/badges)

`[![Latest Stable Version](https://poser.pugx.org/midnite81/badges/version)](https://packagist.org/packages/midnite81/badges)`
9 changes: 5 additions & 4 deletions src/Badges.php
Expand Up @@ -109,14 +109,15 @@ public function coverageStatus()
*
* @param $badgeClass
* @return Writer
* @throws Exceptions\RequiredAttributeNotPassed
* @throws ClassShouldBeInstanceOfBadgeType
*/
public function get($badgeClass)
public function get($badgeClassName)
{
$badgeClass = $badgeClassName::create($this->attributes);

if ($badgeClass instanceof BadgeType) {
return new Writer($badgeClass::create($this->attributes));
return new Writer($badgeClass);
}
throw new ClassShouldBeInstanceOfBadgeType("$badgeClass is not an instance of BadgeType");
throw new ClassShouldBeInstanceOfBadgeType("$badgeClassName is not an instance of BadgeType");
}
}
6 changes: 3 additions & 3 deletions src/Type/BadgeType.php
Expand Up @@ -66,7 +66,7 @@ abstract public function alternativeText();
*
* @return string
*/
public function getUrl()
public function getUrlTemplate()
{
return $this->convertString($this->urlTemplate());
}
Expand Down Expand Up @@ -105,7 +105,7 @@ abstract public function requiredAttributes();
*/
public function getHtmlTemplate()
{
return "<a href=\"{$this->getUrl()}\"><img src=\"{$this->getImageUrl()}\" alt=\"{$this->getAlternativeText()}\"></a>";
return "<a href=\"{$this->getUrlTemplate()}\"><img src=\"{$this->getImageUrl()}\" alt=\"{$this->getAlternativeText()}\"></a>";
}


Expand All @@ -116,7 +116,7 @@ public function getHtmlTemplate()
*/
public function getMarkdownTemplate()
{
return "[![{$this->getAlternativeText()}]({$this->getImageUrl()})]({$this->getUrl()})";
return "[![{$this->getAlternativeText()}]({$this->getImageUrl()})]({$this->getUrlTemplate()})";
}

/**
Expand Down
164 changes: 164 additions & 0 deletions tests/BadgesTest.php
@@ -0,0 +1,164 @@
<?php

namespace Midnite81\Badges\Tests;

use Midnite81\Badges\Badges;
use Midnite81\Badges\Type\Licence;
use Midnite81\Badges\Writer;
use PHPUnit\Framework\TestCase;

class BadgesTest extends TestCase
{
/**
* @test
*/
public function it_instantiates()
{
$badge = $this->getInstance();

$this->assertInstanceOf(Badges::class, $badge);
}

/**
* @test
*/
public function it_factory_instantiates()
{
$badge = $this->getFactoryInstance();

$this->assertInstanceOf(Badges::class, $badge);
}

/**
* @test
*/
public function it_returns_latest_stable_version()
{
$badge = $this->getInstance()->latestStableVersion();

$this->assertInstanceOf(Writer::class, $badge);
$this->assertInternalType('string', $badge->toHtml());
$this->assertInternalType('string', $badge->toMarkdown());
$this->assertContains('<a href', $badge->toHtml());
$this->assertContains('[![', $badge->toMarkdown());
}

/**
* @test
*/
public function it_returns_total_download()
{
$badge = $this->getInstance()->totalDownloads();

$this->assertInstanceOf(Writer::class, $badge);
$this->assertInternalType('string', $badge->toHtml());
$this->assertInternalType('string', $badge->toMarkdown());
$this->assertContains('<a href', $badge->toHtml());
$this->assertContains('[![', $badge->toMarkdown());
}

/**
* @test
*/
public function it_returns_latest_unstable_version()
{
$badge = $this->getInstance()->latestUnstableVersion();

$this->assertInstanceOf(Writer::class, $badge);
$this->assertInternalType('string', $badge->toHtml());
$this->assertInternalType('string', $badge->toMarkdown());
$this->assertContains('<a href', $badge->toHtml());
$this->assertContains('[![', $badge->toMarkdown());
}

/**
* @test
*/
public function it_returns_licence()
{
$badge = $this->getInstance()->licence();

$this->assertInstanceOf(Writer::class, $badge);
$this->assertInternalType('string', $badge->toHtml());
$this->assertInternalType('string', $badge->toMarkdown());
$this->assertContains('<a href', $badge->toHtml());
$this->assertContains('[![', $badge->toMarkdown());
}

/**
* @test
*/
public function it_returns_build()
{
$badge = $this->getInstance()->build();

$this->assertInstanceOf(Writer::class, $badge);
$this->assertInternalType('string', $badge->toHtml());
$this->assertInternalType('string', $badge->toMarkdown());
$this->assertContains('<a href', $badge->toHtml());
$this->assertContains('[![', $badge->toMarkdown());
}

/**
* @test
*/
public function it_returns_coverage_status()
{
$badge = $this->getInstance()->coverageStatus();

$this->assertInstanceOf(Writer::class, $badge);
$this->assertInternalType('string', $badge->toHtml());
$this->assertInternalType('string', $badge->toMarkdown());
$this->assertContains('<a href', $badge->toHtml());
$this->assertContains('[![', $badge->toMarkdown());
}

/**
* @test
*/
public function it_returns_writer_from_get_method()
{
$badge = $this->getInstance()->get(Licence::class);

$this->assertInstanceOf(Writer::class, $badge);
$this->assertInternalType('string', $badge->toHtml());
$this->assertInternalType('string', $badge->toMarkdown());
$this->assertContains('<a href', $badge->toHtml());
$this->assertContains('[![', $badge->toMarkdown());
}

/**
* @test
* @expectedException \Midnite81\Badges\Exceptions\ClassShouldBeInstanceOfBadgeType
*/
public function it_throws_exception_if_class_is_not_type_of_badge_type()
{
$badge = $this->getInstance()->get(TestClass::class);
}


/**
* Get Instance
*
* @param array $attributes
* @return Badges
*/
protected function getInstance($attributes = [])
{
$attributes = (! empty($attributes)) ? $attributes : ['$PACKAGE_NAME$' => 'test/test'];
return new Badges($attributes);
}

/**
* Get Factory Instance
*
* @param array $attributes
* @return static
*/
public function getFactoryInstance($attributes = [])
{
$attributes = (! empty($attributes)) ? $attributes : ['$PACKAGE_NAME$' => 'test/test'];
return Badges::create($attributes);
}

}
18 changes: 18 additions & 0 deletions tests/TestClass.php
@@ -0,0 +1,18 @@
<?php

namespace Midnite81\Badges\Tests;

class TestClass
{
// empty class to us in tests

public function __construct($attributes)
{

}

public static function create($attributes)
{
return new static($attributes);
}
}

0 comments on commit 9f0b1f3

Please sign in to comment.