Skip to content

Commit

Permalink
Merge branch 'master' into seed
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Feb 17, 2020
2 parents 0d2d2e5 + bfb82fe commit 452242e
Show file tree
Hide file tree
Showing 15 changed files with 198 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

/tests export-ignore
/.gitattributes export-ignore
/.github export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/CONTRIBUTING.md export-ignore
/README.md export-ignore
/UPGRADING.md export-ignore
76 changes: 76 additions & 0 deletions .github/CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# CONTRIBUTOR COVENANT CODE OF CONDUCT

## Our Pledge

In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, sex characteristics, gender identity and expression,
level of experience, education, socio-economic status, nationality, personal
appearance, race, religion, or sexual identity and orientation.

## Our Standards

Examples of behavior that contributes to creating a positive environment
include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting

## Our Responsibilities

Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.

Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.

## Scope

This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at graham@alt-three.com. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.

Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html

[homepage]: https://www.contributor-covenant.org

For answers to common questions about this code of conduct, see
https://www.contributor-covenant.org/faq
31 changes: 31 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# CONTRIBUTION GUIDELINES

Contributions are **welcome** and will be fully **credited**.

We accept contributions via pull requests on Github. Please review these guidelines before continuing.

## Guidelines

* Please follow the [PSR-2 Coding Style Guide](https://www.php-fig.org/psr/psr-2/), enforced by [StyleCI](https://styleci.io/).
* Ensure that the current tests pass, and if you've added something new, add the tests where relevant.
* Send a coherent commit history, making sure each individual commit in your pull request is meaningful.
* You may need to [rebase](https://git-scm.com/book/en/v2/Git-Branching-Rebasing) to avoid merge conflicts.
* If you are changing or adding to the behaviour or public api, you may need to update the docs.
* Please remember that we follow [SemVer](https://semver.org/).

## Running Tests

First, install the dependencies using [Composer](https://getcomposer.org/):

```bash
$ composer install
```

Then run [PHPUnit](https://phpunit.de/):

```bash
$ vendor/bin/phpunit
```

* The tests will be automatically run by [Travis CI](https://travis-ci.org/) against pull requests.
* We also have [StyleCI](https://styleci.io/) setup to automatically fix any code style issues.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tidelift: "packagist/league/factory-muffin"
14 changes: 14 additions & 0 deletions .github/SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# SECURITY POLICY

## Supported Versions

After each new major release, the previous release will be supported for no
less than 6 months, unless explictly stated otherwise. This may mean that there
are multiple supported versions at any given time.

## Reporting a Vulnerability

If you discover a security vulnerability within this package, please send an
email to Graham Campbell at graham@alt-three.com. All security vulnerabilities
will be promptly addressed. Please do not disclose security-related issues
publicly until a fix has been announced.
31 changes: 22 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- hhvm

sudo: false

matrix:
include:
- php: hhvm-3.18
dist: trusty
- php: hhvm-3.30
dist: trusty
- php: 5.4
dist: trusty
- php: 5.5
dist: trusty
- php: 5.6
dist: xenial
- php: 7.0
dist: xenial
- php: 7.1
dist: bionic
- php: 7.2
dist: bionic
- php: 7.3
dist: bionic

install:
- travis_retry composer install --no-interaction --prefer-source
- travis_retry composer install --no-suggest --prefer-dist -n -o

script:
- if [ "$TRAVIS_PHP_VERSION" != "5.4" ] && [ "$TRAVIS_PHP_VERSION" != "5.5" ] && [ "$TRAVIS_PHP_VERSION" != "5.6" ]; then vendor/bin/phpunit; fi
Expand Down
37 changes: 0 additions & 37 deletions CONTRIBUTING.md

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014-2016 Graham Campbell <graham@alt-three.com>
Copyright (c) 2014-2019 Graham Campbell <graham@alt-three.com>
Copyright (c) 2014-2015 Scott Robertson <scottymeuk@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,19 @@ You can define model factories using the `define` function. You may call it like

You can also define multiple different model definitions for your models. You can do this by prefixing the model class name with your "group" followed by a colon. This results in you defining your model like this: `$fm->define('myGroup:Fully\Qualifed\ModelName')->addDefinitions('bar', 'baz')`. You don't have to entirely define your model here because we will first look for a definition without the group prefix, then apply your group definition on top of that definition, overriding attribute definitions where required.

We have provided a nifty way for you to do this in your tests. PHPUnit provides a `setupBeforeClass` function. Within that function you can call `$fm->loadFactories(__DIR__ . '/factories');`, and it will include all files in the factories folder. Within those php files, you can put your definitions (all your code that calls the define function). The `loadFactories` function will throw a `League\FactoryMuffin\Exceptions\DirectoryNotFoundException` exception if the directory you're loading is not found.
We have provided a nifty way for you to do this in your tests. PHPUnit provides a `setupBeforeClass` function. Within that function you can call `$fm->loadFactories(__DIR__ . '/factories');`, and it will include all files in the factories folder. Within those PHP files, you can put your definitions (all your code that calls the define function).

The `loadFactories` function will recurse through all sub-folders of the path you specify when searching for factory files, except for hidden folders (i.e. starting with a .) which will be ignored. It will also throw a `League\FactoryMuffin\Exceptions\DirectoryNotFoundException` exception if the factories directory you specify is not found.

### Creation/Instantiation Callbacks

You may optionally specify a callback to be executed on model creation/instantiation as a third parameter when defining a definition. We will pass your model instance as the first parameter to the closure if you specify one. We additionally pass a boolean as the second parameter that will be `true` if the model is being persisted to the database (the create function was used), and `false` if it's not being persisted (the instance function was used). We're using the `isPendingOrSaved` function under the hood here. Note that if you specify a callback and use the create function, we will try to save your model to the database both before and after we execute the callback.
You may optionally specify a callback to be executed on model creation/instantiation using the `setCallback` function, like this: `$fm->define('MyModel')->setCallback(function ($object, $saved) {})`. We will pass your model instance as the first parameter to the callback and a boolean as the second parameter. The boolean will be `true` if the model is being persisted to the database (the create function was used), and `false` if it's not being persisted (the instance function was used). We're using the `isPendingOrSaved` function under the hood here. Note that if you specify a callback and use the `create` function, we will try to save your model to the database both before and after we execute the callback.

### Generators

#### Callable

The callable generator can be used if you want a more custom solution. Whatever you return from your closure, or valid callable, will be set as the attribute. Note that we pass an instance of your model as the first parameter of the closure/callable to give you even more flexibility to modify it as you wish. We additionally pass a boolean as the second parameter that will be `true` if the model is being persisted to the database (the create function was used), and `false` if it's not being persisted (the instance function was used). We're using the `isPendingOrSaved` function under the hood here. In the following examples, we will go through using a closure, or callable, and then how to use faker to generate attributes.
The callable generator can be used if you want a more custom solution. Whatever you return from your closure, or valid callable, will be set as the attribute. The closure/callable will be called with the same parameters as creation/instantiation callbacks described above: an instance of your model as the first parameter (to give you even more flexibility to modify it as you wish) and a boolean indicating if the model is being persisted to the database. In the following examples, we will go through using a closure, or callable, and then how to use faker to generate attributes.

##### Example 1

Expand Down Expand Up @@ -280,14 +282,32 @@ If you want more information, the following resources are available to you:

## Contributing

Please check out our [contribution guidelines](CONTRIBUTING.md) for details.
Please check out our [contribution guidelines](.github/CONTRIBUTING.md) for details.


## Credits

Factory Muffin is based on [Zizaco Zizuini](https://github.com/Zizaco)'s original work on "Factory Muff", and is currently maintained by [Graham Campbell](https://github.com/GrahamCampbell). [Scott Robertson](https://github.com/scottrobertson) was also a co-maintainer before the 3.0 release. Thank you to all our wonderful [contributors](https://github.com/thephpleague/factory-muffin/contributors) too.


## Security

If you discover a security vulnerability within this package, please send an email to Graham Campbell at graham@alt-three.com. All security vulnerabilities will be promptly addressed. You may view our full security policy [here](https://github.com/thephpleague/factory-muffin/security/policy).


## License

Factory Muffin is licensed under [The MIT License (MIT)](LICENSE).


---

<div align="center">
<b>
<a href="https://tidelift.com/subscription/pkg/packagist-league-factory-muffin?utm_source=packagist-league-factory-muffin&utm_medium=referral&utm_campaign=readme">Get professional support for Factory Muffin with a Tidelift subscription</a>
</b>
<br>
<sub>
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
</sub>
</div>
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
"dev-master": "3.1-dev"
}
}
}
12 changes: 9 additions & 3 deletions src/FactoryMuffin.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function seed($times, $name, array $attr = [], $save = true)

$method = $save ? 'create' : 'instance';

for ($i = 0; $i < $times; ++$i) {
for ($i = 0; $i < $times; $i++) {
$seeds[] = $this->{$method}($name, $attr);
}

Expand Down Expand Up @@ -344,8 +344,9 @@ public function loadFactories($paths)
}

/**
* Load all the files in a directory.
* Load all the files in a directory or sub-directory.
*
* Files that start with a . or do not have a .php extension are ignored.
* Each required file will have this instance available as "$fm".
*
* @param string $path The directory path to load.
Expand All @@ -356,11 +357,16 @@ private function loadDirectory($path)
{
$directory = new RecursiveDirectoryIterator($path);
$iterator = new RecursiveIteratorIterator($directory);
$files = new RegexIterator($iterator, '/^[^\.](?:(?!\/\.).)+?\.php$/i');
$files = new RegexIterator($iterator, '/^.+\.php$/i');

$fm = $this;

foreach ($files as $file) {
// Ignore factories in hidden subdirectories
if ('.' === substr($file->getPathInfo()->getFilename(), 0, 1)) {
continue;
}

require $file->getPathName();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Generators/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class FactoryGenerator extends EntityGenerator
*
* @var string[]
*/
private static $properties = ['id', '_id'];
private static $properties = ['id', '_id', 'Id'];

/**
* Generate, and return the attribute.
Expand Down
5 changes: 5 additions & 0 deletions tests/DefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public function testModelNotFound()
} catch (ModelNotFoundException $e) {
$this->assertSame("The model class '$model' is undefined.", $e->getMessage());
$this->assertSame($model, $e->getModelClass());

throw $e;
}
}
Expand Down Expand Up @@ -210,6 +211,7 @@ public function testShouldThrowExceptionWhenLoadingANonExistentGroup()
} catch (DefinitionNotFoundException $e) {
$this->assertSame("The model definition '$model' is undefined.", $e->getMessage());
$this->assertSame($model, $e->getDefinitionName());

throw $e;
}
}
Expand All @@ -227,6 +229,7 @@ public function testGroupDefineNoBaseModel()
} catch (DefinitionNotFoundException $e) {
$this->assertSame("The model definition 'DogModelStub' is undefined.", $e->getMessage());
$this->assertSame('DogModelStub', $e->getDefinitionName());

throw $e;
}
}
Expand All @@ -241,6 +244,7 @@ public function testCannotDefineAgain()
} catch (DefinitionAlreadyDefinedException $e) {
$this->assertSame("The model definition 'UserModelStub' has already been defined.", $e->getMessage());
$this->assertSame('UserModelStub', $e->getDefinitionName());

throw $e;
}
}
Expand Down Expand Up @@ -311,6 +315,7 @@ public function testShouldThrowExceptionWhenLoadingANonExistentDirectory()
} catch (DirectoryNotFoundException $e) {
$this->assertSame("The directory '$path' was not found.", $e->getMessage());
$this->assertSame($path, $e->getPath());

throw $e;
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/FactoryMuffinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testDefaultingToFaker()
$this->assertArrayHasKey('name', $obj->card);
$this->assertArrayHasKey('expirationDate', $obj->card);

$this->assertSame('http://lorempixel.com/400/600/', substr($obj->image, 0, 30));
$this->assertSame('https://lorempixel.com/400/600', substr($obj->image, 0, 30));
$this->assertNotEquals('unique::text', $obj->unique_text);
$this->assertNotEquals('optional::text', $obj->optional_text);
}
Expand Down Expand Up @@ -87,6 +87,7 @@ public function testShouldThrowDefinitionNotFoundException()
} catch (DefinitionNotFoundException $e) {
$this->assertSame("The model definition '$model' is undefined.", $e->getMessage());
$this->assertSame($model, $e->getDefinitionName());

throw $e;
}
}
Expand Down
Loading

0 comments on commit 452242e

Please sign in to comment.